2009-12-19 15:06:57 +01:00
|
|
|
/**
|
|
|
|
* ntfsfile.c - devoptab file routines for NTFS-based devices.
|
2010-09-16 21:59:41 +02:00
|
|
|
* Copyright (c) 2010 Miigotu
|
2009-12-19 15:06:57 +01:00
|
|
|
* Copyright (c) 2009 Rhys "Shareese" Koedijk
|
|
|
|
* Copyright (c) 2006 Michael "Chishm" Chisholm
|
|
|
|
*
|
|
|
|
* This program/include file is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License as published
|
2010-09-19 01:16:05 +02:00
|
|
|
* by the Free Software Foundation; either version 2 of the License, or
|
2009-12-19 15:06:57 +01:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program/include file is distributed in the hope that it will be
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STDLIB_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_SYS_STAT_H
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_FCNTL_H
|
|
|
|
#include <fcntl.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_ERRNO_H
|
|
|
|
#include <errno.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
#include <string.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "ntfsinternal.h"
|
|
|
|
#include "ntfsfile.h"
|
|
|
|
#include "ntfs.h"
|
|
|
|
|
|
|
|
#define STATE(x) ((ntfs_file_state*)x)
|
|
|
|
|
|
|
|
// for easier comparision of ntfsfile.c against ntfsfile_frag.c
|
|
|
|
// everything is included but ifdef-ed out
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
void ntfsCloseFile ( ntfs_file_state *file )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd )
|
2009-12-19 15:06:57 +01:00
|
|
|
return;
|
|
|
|
|
|
|
|
// Special case fix ups for compressed and/or encrypted files
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->compressed )
|
|
|
|
ntfs_attr_pclose( file->data_na );
|
2010-09-16 21:59:41 +02:00
|
|
|
#ifdef HAVE_SETXATTR
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->encrypted )
|
|
|
|
ntfs_efs_fixup_attribute( NULL, file->data_na );
|
2010-09-16 21:59:41 +02:00
|
|
|
#endif
|
2009-12-19 15:06:57 +01:00
|
|
|
// Close the file data attribute (if open)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->data_na )
|
|
|
|
ntfs_attr_close( file->data_na );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sync the file (and its attributes) to disc
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->write )
|
2010-09-16 21:59:41 +02:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUpdateTimes( file->vd, file->ni, NTFS_UPDATE_ATIME | NTFS_UPDATE_CTIME );
|
|
|
|
ntfsSync( file->vd, file->ni );
|
2010-09-16 21:59:41 +02:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->read )
|
|
|
|
ntfsUpdateTimes( file->vd, file->ni, NTFS_UPDATE_ATIME );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Close the file (if open)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->ni )
|
|
|
|
ntfsCloseEntry( file->vd, file->ni );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Reset the file state
|
|
|
|
file->ni = NULL;
|
|
|
|
file->data_na = NULL;
|
|
|
|
file->flags = 0;
|
|
|
|
file->read = false;
|
|
|
|
file->write = false;
|
|
|
|
file->append = false;
|
|
|
|
file->pos = 0;
|
|
|
|
file->len = 0;
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int ntfs_open_r ( struct _reent *r, void *fileStruct, const char *path, int flags, int mode )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "fileStruct %p, path %s, flags %i, mode %i\n", fileStruct, path, flags, mode );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_file_state* file = STATE( fileStruct );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the volume descriptor for this path
|
2010-09-19 01:16:05 +02:00
|
|
|
file->vd = ntfsGetVolume( path );
|
|
|
|
if ( !file->vd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = ENODEV;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsLock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// Determine which mode the file is opened for
|
|
|
|
file->flags = flags;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( flags & 0x03 ) == O_RDONLY )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
file->read = true;
|
|
|
|
file->write = false;
|
|
|
|
file->append = false;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else if ( ( flags & 0x03 ) == O_WRONLY )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
file->read = false;
|
|
|
|
file->write = true;
|
2010-09-19 01:16:05 +02:00
|
|
|
file->append = ( flags & O_APPEND );
|
|
|
|
}
|
|
|
|
else if ( ( flags & 0x03 ) == O_RDWR )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
file->read = true;
|
|
|
|
file->write = true;
|
2010-09-19 01:16:05 +02:00
|
|
|
file->append = ( flags & O_APPEND );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EACCES;
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Try and find the file and (if found) ensure that it is not a directory
|
2010-09-19 01:16:05 +02:00
|
|
|
file->ni = ntfsOpenEntry( file->vd, path );
|
|
|
|
if ( file->ni && ( file->ni->mrec->flags & MFT_RECORD_IS_DIRECTORY ) )
|
|
|
|
{
|
|
|
|
ntfsCloseEntry( file->vd, file->ni );
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EISDIR;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Are we creating this file?
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( flags & O_CREAT )
|
|
|
|
{
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// The file SHOULD NOT already exist
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->ni )
|
|
|
|
{
|
|
|
|
ntfsCloseEntry( file->vd, file->ni );
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EEXIST;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Create the file
|
2010-09-19 01:16:05 +02:00
|
|
|
file->ni = ntfsCreate( file->vd, path, S_IFREG, NULL );
|
|
|
|
if ( !file->ni )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check, the file should be open by now
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file->ni )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = ENOENT;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Open the files data attribute
|
2010-09-19 01:16:05 +02:00
|
|
|
file->data_na = ntfs_attr_open( file->ni, AT_DATA, AT_UNNAMED, 0 );
|
|
|
|
if ( !file->data_na )
|
|
|
|
{
|
|
|
|
ntfsCloseEntry( file->vd, file->ni );
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if this files data is compressed and/or encrypted
|
2010-09-19 01:16:05 +02:00
|
|
|
file->compressed = NAttrCompressed( file->data_na ) || ( file->ni->flags & FILE_ATTR_COMPRESSED );
|
|
|
|
file->encrypted = NAttrEncrypted( file->data_na ) || ( file->ni->flags & FILE_ATTR_ENCRYPTED );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// We cannot read/write encrypted files
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->encrypted )
|
|
|
|
{
|
|
|
|
ntfs_attr_close( file->data_na );
|
|
|
|
ntfsCloseEntry( file->vd, file->ni );
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Make sure we aren't trying to write to a read-only file
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( file->ni->flags & FILE_ATTR_READONLY ) && file->write )
|
|
|
|
{
|
|
|
|
ntfs_attr_close( file->data_na );
|
|
|
|
ntfsCloseEntry( file->vd, file->ni );
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EROFS;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Truncate the file if requested
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ( flags & O_TRUNC ) && file->write )
|
|
|
|
{
|
|
|
|
if ( ntfs_attr_truncate( file->data_na, 0 ) )
|
|
|
|
{
|
|
|
|
ntfs_attr_close( file->data_na );
|
|
|
|
ntfsCloseEntry( file->vd, file->ni );
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Set the files current position and length
|
|
|
|
file->pos = 0;
|
|
|
|
file->len = file->data_na->data_size;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "file->len %d\n", file->len );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Update file times
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUpdateTimes( file->vd, file->ni, NTFS_UPDATE_ATIME );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Insert the file into the double-linked FILO list of open files
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->vd->firstOpenFile )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
file->nextOpenFile = file->vd->firstOpenFile;
|
|
|
|
file->vd->firstOpenFile->prevOpenFile = file;
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
file->nextOpenFile = NULL;
|
|
|
|
}
|
|
|
|
file->prevOpenFile = NULL;
|
|
|
|
file->vd->firstOpenFile = file;
|
|
|
|
file->vd->openFileCount++;
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Unlock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUnlock( file->vd );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
return ( int )fileStruct;
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int ntfs_close_r ( struct _reent *r, int fd )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "fd %p\n", fd );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_file_state* file = STATE( fd );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EBADF;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Lock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsLock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// Close the file
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsCloseFile( file );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Remove the file from the double-linked FILO list of open files
|
|
|
|
file->vd->openFileCount--;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->nextOpenFile )
|
2009-12-19 15:06:57 +01:00
|
|
|
file->nextOpenFile->prevOpenFile = file->prevOpenFile;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->prevOpenFile )
|
2009-12-19 15:06:57 +01:00
|
|
|
file->prevOpenFile->nextOpenFile = file->nextOpenFile;
|
|
|
|
else
|
|
|
|
file->vd->firstOpenFile = file->nextOpenFile;
|
|
|
|
|
|
|
|
// Unlock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUnlock( file->vd );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
ssize_t ntfs_write_r ( struct _reent *r, int fd, const char *ptr, size_t len )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "fd %p, ptr %p, len %Li\n", fd, ptr, len );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_file_state* file = STATE( fd );
|
2009-12-19 15:06:57 +01:00
|
|
|
ssize_t written = 0;
|
|
|
|
off_t old_pos = 0;
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd || !file->ni || !file->data_na )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Short circuit cases where we don't actually have to do anything
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !ptr || len <= 0 )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Lock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsLock( file->vd );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that we are allowed to write to this file
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file->write )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// If we are in append mode, backup the current position and move to the end of the file
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->append )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
old_pos = file->pos;
|
|
|
|
file->pos = file->len;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Write to the files data atrribute
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( len )
|
|
|
|
{
|
|
|
|
ssize_t ret = ntfs_attr_pwrite( file->data_na, file->pos, len, ptr );
|
|
|
|
if ( ret <= 0 )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
len -= ret;
|
|
|
|
file->pos += ret;
|
|
|
|
written += ret;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// If we are in append mode, restore the current position to were it was prior to this write
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->append )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
file->pos = old_pos;
|
|
|
|
}
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Mark the file for archiving (if we actually wrote something)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( written )
|
2009-12-19 15:06:57 +01:00
|
|
|
file->ni->flags |= FILE_ATTR_ARCHIVE;
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Update the files data length
|
|
|
|
file->len = file->data_na->data_size;
|
2010-09-16 21:59:41 +02:00
|
|
|
|
|
|
|
// Unlock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUnlock( file->vd );
|
2010-09-16 21:59:41 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
s64 ntfs_attr_getfragments( ntfs_attr *na, const s64 pos, s64 count, u64 offset,
|
|
|
|
_ntfs_frag_append_t append_fragment, void *callback_data );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int _NTFS_get_fragments ( const char *path,
|
|
|
|
_ntfs_frag_append_t append_fragment, void *callback_data )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
struct _reent r;
|
|
|
|
ntfs_file_state file_st, *file = &file_st;
|
|
|
|
ssize_t read = 0;
|
|
|
|
int ret_val = -11;
|
|
|
|
|
|
|
|
// Open File
|
|
|
|
r._errno = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
int fd = ntfs_open_r( &r, file, path, O_RDONLY, 0 );
|
|
|
|
if ( fd != ( int )file ) return -12;
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
|
2010-09-16 21:59:41 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd || !file->ni || !file->data_na )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
//r->_errno = EINVAL;
|
|
|
|
return -13;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
// Short circuit cases where we don't actually have to do anything
|
|
|
|
if (!ptr || len <= 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*/
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Lock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsLock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
// Check that we are allowed to read from this file
|
|
|
|
if (!file->read) {
|
|
|
|
ntfsUnlock(file->vd);
|
|
|
|
r->_errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Don't read past the end of file
|
|
|
|
if (file->pos + len > file->len) {
|
|
|
|
r->_errno = EOVERFLOW;
|
|
|
|
len = file->len - file->pos;
|
2010-09-16 21:59:41 +02:00
|
|
|
ntfs_log_trace("EOVERFLOW");
|
2009-12-19 15:06:57 +01:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
u64 offset = 0;
|
|
|
|
u64 len = file->len;
|
|
|
|
|
|
|
|
// Read from the files data attribute
|
2010-09-19 01:16:05 +02:00
|
|
|
while ( len )
|
|
|
|
{
|
|
|
|
s64 ret = ntfs_attr_getfragments( file->data_na, file->pos,
|
|
|
|
len, offset, append_fragment, callback_data );
|
|
|
|
if ( ret <= 0 || ret > len )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
//r->_errno = errno;
|
|
|
|
ret_val = -14;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ret < 0 ) ret_val = ret;
|
2009-12-19 15:06:57 +01:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
offset += ret;
|
|
|
|
len -= ret;
|
|
|
|
file->pos += ret;
|
|
|
|
read += ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
// set file size
|
2010-09-19 01:16:05 +02:00
|
|
|
append_fragment( callback_data, file->len >> 9, 0, 0 );
|
2009-12-19 15:06:57 +01:00
|
|
|
// success
|
|
|
|
ret_val = 0;
|
|
|
|
|
|
|
|
/*
|
2010-09-16 21:59:41 +02:00
|
|
|
//ntfs_log_trace("file->pos: %d \n", (u32)file->pos);
|
2009-12-19 15:06:57 +01:00
|
|
|
// Update file times (if we actually read something)
|
|
|
|
if (read)
|
|
|
|
ntfsUpdateTimes(file->vd, file->ni, NTFS_UPDATE_ATIME);
|
|
|
|
*/
|
|
|
|
|
|
|
|
out:
|
|
|
|
// Unlock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
// Close the file
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_close_r ( &r, fd );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
return ret_val;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
off_t ntfs_seek_r ( struct _reent *r, int fd, off_t pos, int dir )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "fd %p, pos %Li, dir %i\n", fd, pos, dir );
|
|
|
|
|
|
|
|
ntfs_file_state* file = STATE( fd );
|
2009-12-19 15:06:57 +01:00
|
|
|
off_t position = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd || !file->ni || !file->data_na )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Lock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsLock( file->vd );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Set the files current position
|
2010-09-19 01:16:05 +02:00
|
|
|
switch ( dir )
|
|
|
|
{
|
|
|
|
case SEEK_SET: position = file->pos = MIN( MAX( pos, 0 ), file->len ); break;
|
|
|
|
case SEEK_CUR: position = file->pos = MIN( MAX( file->pos + pos, 0 ), file->len ); break;
|
|
|
|
case SEEK_END: position = file->pos = MIN( MAX( file->len + pos, 0 ), file->len ); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unlock
|
|
|
|
ntfsUnlock( file->vd );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return position;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
int ntfs_fstat_r ( struct _reent *r, int fd, struct stat *st )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "fd %p\n", fd );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_file_state* file = STATE( fd );
|
2009-12-19 15:06:57 +01:00
|
|
|
int ret = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd || !file->ni || !file->data_na )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Short circuit cases were we don't actually have to do anything
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !st )
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Get the file stats
|
2010-09-19 01:16:05 +02:00
|
|
|
ret = ntfsStat( file->vd, file->ni, st );
|
|
|
|
if ( ret )
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = errno;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int ntfs_ftruncate_r ( struct _reent *r, int fd, off_t len )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "fd %p, len %Li\n", fd, len );
|
|
|
|
|
|
|
|
ntfs_file_state* file = STATE( fd );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd || !file->ni || !file->data_na )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Lock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsLock( file->vd );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Check that we are allowed to write to this file
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file->write )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EACCES;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// For compressed files, only deleting and expanding contents are implemented
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->compressed &&
|
|
|
|
len > 0 &&
|
|
|
|
len < file->data_na->initialized_size )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EOPNOTSUPP;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Resize the files data attribute, either by expanding or truncating
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->compressed && len > file->data_na->initialized_size )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
char zero = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( ntfs_attr_pwrite( file->data_na, len - 1, 1, &zero ) <= 0 )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( ntfs_attr_truncate( file->data_na, len ) )
|
|
|
|
{
|
|
|
|
ntfsUnlock( file->vd );
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = errno;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark the file for archiving (if we actually changed something)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->len != file->data_na->data_size )
|
2009-12-19 15:06:57 +01:00
|
|
|
file->ni->flags |= FILE_ATTR_ARCHIVE;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Update file times (if we actually changed something)
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( file->len != file->data_na->data_size )
|
|
|
|
ntfsUpdateTimes( file->vd, file->ni, NTFS_UPDATE_AMCTIME );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Update the files data length
|
|
|
|
file->len = file->data_na->data_size;
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
// Sync the file (and its attributes) to disc
|
|
|
|
ntfsSync( file->vd, file->ni );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
|
|
|
// Unlock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUnlock( file->vd );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
int ntfs_fsync_r ( struct _reent *r, int fd )
|
2010-02-09 11:59:55 +01:00
|
|
|
{
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_log_trace( "fd %p\n", fd );
|
2009-12-19 15:06:57 +01:00
|
|
|
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfs_file_state* file = STATE( fd );
|
2009-12-19 15:06:57 +01:00
|
|
|
int ret = 0;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sanity check
|
2010-09-19 01:16:05 +02:00
|
|
|
if ( !file || !file->vd || !file->ni || !file->data_na )
|
|
|
|
{
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Lock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsLock( file->vd );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Sync the file (and its attributes) to disc
|
2010-09-19 01:16:05 +02:00
|
|
|
ret = ntfsSync( file->vd, file->ni );
|
|
|
|
if ( ret )
|
2009-12-19 15:06:57 +01:00
|
|
|
r->_errno = errno;
|
2010-09-19 01:16:05 +02:00
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
// Unlock
|
2010-09-19 01:16:05 +02:00
|
|
|
ntfsUnlock( file->vd );
|
|
|
|
|
2009-12-19 15:06:57 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|