Files
itgmania212121/stepmania/src/RageFileDriverDirect.cpp
T

419 lines
9.6 KiB
C++
Raw Normal View History

2003-12-04 08:25:59 +00:00
#include "global.h"
#include "RageFileDriverDirect.h"
2004-01-06 05:40:24 +00:00
#include "RageFileDriverDirectHelpers.h"
2003-12-04 08:25:59 +00:00
#include "RageUtil.h"
2003-12-07 06:13:44 +00:00
#include "RageUtil_FileDB.h"
#include "RageLog.h"
2003-12-04 08:25:59 +00:00
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
2003-12-07 04:14:52 +00:00
#if !defined(WIN32)
#include <dirent.h>
#include <fcntl.h>
#else
#include <windows.h>
2003-12-04 08:25:59 +00:00
#include <io.h>
#endif
/* This driver handles direct file access. */
2003-12-07 04:14:52 +00:00
class DirectFilenameDB: public FilenameDB
{
protected:
virtual void PopulateFileSet( FileSet &fs, const CString &sPath );
2003-12-07 22:22:27 +00:00
CString root;
2003-12-07 04:14:52 +00:00
public:
2003-12-07 22:22:27 +00:00
DirectFilenameDB( CString root_ )
{
ExpireSeconds = 30;
root = root_;
if( root.Right(1) != "/" )
root += '/';
if( root == "./" )
root = "";
}
2003-12-07 04:14:52 +00:00
};
2003-12-10 09:43:31 +00:00
2003-12-07 04:14:52 +00:00
void DirectFilenameDB::PopulateFileSet( FileSet &fs, const CString &path )
{
CString sPath = path;
/* Resolve path cases (path/Path -> PATH/path). */
ResolvePath( sPath );
fs.age.GetDeltaTime(); /* reset */
fs.files.clear();
#if defined(WIN32)
WIN32_FIND_DATA fd;
2003-12-10 09:43:31 +00:00
if ( sPath.size() > 0 && sPath.Right(1) == "/" )
2003-12-07 04:14:52 +00:00
sPath.erase( sPath.size() - 1 );
2003-12-10 09:43:31 +00:00
HANDLE hFind = DoFindFirstFile( root+sPath+"/*", &fd );
2003-12-07 04:14:52 +00:00
if( hFind == INVALID_HANDLE_VALUE )
return;
do {
if(!strcmp(fd.cFileName, ".") || !strcmp(fd.cFileName, ".."))
continue;
File f;
f.SetName( fd.cFileName );
f.dir = !!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY);
f.size = fd.nFileSizeLow;
2003-12-10 07:07:42 +00:00
f.hash = fd.ftLastWriteTime.dwLowDateTime;
2003-12-07 04:14:52 +00:00
fs.files.insert(f);
} while( FindNextFile( hFind, &fd ) );
FindClose(hFind);
#else
2004-01-05 20:18:27 +00:00
int OldDir = DoOpen(".", O_RDONLY);
2003-12-07 04:14:52 +00:00
if( OldDir == -1 )
RageException::Throw( "Couldn't open(.): %s", strerror(errno) );
2003-12-07 22:22:27 +00:00
if( chdir(root+sPath) == -1 )
2003-12-07 04:14:52 +00:00
{
/* Only log once per dir. */
if( LOG )
LOG->MapLog("chdir " + sPath, "Couldn't chdir(%s): %s", sPath.c_str(), strerror(errno) );
close( OldDir );
return;
}
DIR *d = opendir(".");
while(struct dirent *ent = readdir(d))
{
if(!strcmp(ent->d_name, ".")) continue;
if(!strcmp(ent->d_name, "..")) continue;
File f;
f.SetName( ent->d_name );
struct stat st;
2004-01-05 20:18:27 +00:00
if( DoStat(ent->d_name, &st) == -1 )
2003-12-07 04:14:52 +00:00
{
/* If it's a broken symlink, ignore it. Otherwise, warn. */
if( lstat(ent->d_name, &st) == 0 )
continue;
/* Huh? */
if(LOG)
LOG->Warn("Got file '%s' in '%s' from list, but can't stat? (%s)",
ent->d_name, sPath.c_str(), strerror(errno));
continue;
} else {
f.dir = (st.st_mode & S_IFDIR);
f.size = st.st_size;
2003-12-10 07:07:42 +00:00
f.hash = st.st_mtime;
2003-12-07 04:14:52 +00:00
}
fs.files.insert(f);
}
closedir(d);
if( fchdir( OldDir ) == -1 )
RageException::Throw( "Couldn't fchdir(): %s", strerror(errno) );
close( OldDir );
#endif
}
2003-12-04 08:25:59 +00:00
class RageFileObjDirect: public RageFileObj
{
private:
int fd;
2003-12-10 05:00:28 +00:00
CString path; /* for Copy */
2003-12-04 08:25:59 +00:00
2003-12-11 05:00:18 +00:00
CString write_buf;
2003-12-04 08:25:59 +00:00
public:
2003-12-10 05:00:28 +00:00
RageFileObjDirect( const CString &path, int fd_, RageFile &p );
2003-12-04 08:25:59 +00:00
virtual ~RageFileObjDirect();
virtual int Read(void *buffer, size_t bytes);
virtual int Write(const void *buffer, size_t bytes);
2003-12-12 07:47:38 +00:00
virtual int Flush();
2003-12-04 08:25:59 +00:00
virtual void Rewind();
virtual int Seek( int offset );
2003-12-10 05:00:28 +00:00
virtual RageFileObj *Copy( RageFile &p ) const;
2003-12-11 07:40:03 +00:00
virtual CString GetDisplayPath() const { return path; }
2003-12-04 08:25:59 +00:00
};
RageFileDriverDirect::RageFileDriverDirect( CString root_ ):
2003-12-07 22:22:27 +00:00
RageFileDriver( new DirectFilenameDB(root_) ),
2003-12-04 08:25:59 +00:00
root(root_)
{
2003-12-06 05:52:44 +00:00
if( root.Right(1) != "/" )
root += '/';
2003-12-04 08:25:59 +00:00
}
2003-12-10 09:43:31 +00:00
static CString MakeTempFilename( const CString &sPath )
{
/* "Foo/bar/baz" -> "Foo/bar/new.baz". Prepend to the basename, so if we're
* writing something that might be wildcard-searched, these temp files won't
* match. */
return Dirname(sPath) + "new." + Basename(sPath);
}
2003-12-10 09:43:31 +00:00
RageFileObj *MakeFileObjDirect( CString sPath, int mode, RageFile &p, int &err )
2003-12-04 08:25:59 +00:00
{
int fd;
if( mode & RageFile::READ )
{
fd = DoOpen( sPath, O_BINARY|O_RDONLY, 0644 );
}
2003-12-04 08:25:59 +00:00
else
2003-12-07 04:14:52 +00:00
{
CString out;
if( mode & RageFile::STREAMED )
out = sPath;
else
out = MakeTempFilename(sPath);
/* Open a temporary file for writing. */
fd = DoOpen( out, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0644 );
2003-12-07 04:14:52 +00:00
}
2003-12-04 08:25:59 +00:00
if( fd == -1 )
{
err = errno;
return NULL;
}
2003-12-10 05:00:28 +00:00
return new RageFileObjDirect( sPath, fd, p );
}
RageFileObj *RageFileDriverDirect::Open( const CString &path, int mode, RageFile &p, int &err )
2003-12-10 05:00:28 +00:00
{
2003-12-15 02:32:55 +00:00
CString sPath = path;
2003-12-10 05:00:28 +00:00
/* This partially resolves. For example, if "abc/def" exists, and we're opening
* "ABC/DEF/GHI/jkl/mno", this will resolve it to "abc/def/GHI/jkl/mno"; we'll
* create the missing parts below. */
2003-12-16 07:23:54 +00:00
/* XXX: does it? */
2003-12-10 05:00:28 +00:00
FDB->ResolvePath( sPath );
if( mode & RageFile::WRITE )
2003-12-10 05:00:28 +00:00
{
const CString dir = Dirname(sPath);
if( this->GetFileType(dir) != RageFileManager::TYPE_DIR )
2003-12-15 02:38:52 +00:00
CreateDirectories( root + dir );
2003-12-10 05:00:28 +00:00
}
2003-12-15 02:32:55 +00:00
return MakeFileObjDirect( root + sPath, mode, p, err );
2003-12-10 05:00:28 +00:00
}
2003-12-16 07:23:54 +00:00
bool RageFileDriverDirect::Remove( const CString &path )
{
CString sPath = path;
FDB->ResolvePath( sPath );
switch( this->GetFileType(sPath) )
{
2004-01-06 05:44:28 +00:00
case RageFileManager::TYPE_FILE:
2003-12-16 07:23:54 +00:00
LOG->Trace("remove '%s'", (root + sPath).c_str());
if( DoRemove( root + sPath ) == -1 )
{
LOG->Warn("remove(%s) failed: %s",
(root + sPath).c_str(), strerror(errno) );
return false;
}
FDB->DelFile( sPath );
return true;
2004-01-06 05:44:28 +00:00
case RageFileManager::TYPE_DIR:
2003-12-16 07:23:54 +00:00
LOG->Trace("rmdir '%s'", (root + sPath).c_str());
if( DoRmdir( root + sPath ) == -1 )
{
LOG->Warn("rmdir(%s) failed: %s",
(root + sPath).c_str(), strerror(errno) );
return false;
}
FDB->DelFile( sPath );
return true;
2004-01-06 05:44:28 +00:00
case RageFileManager::TYPE_NONE: return false;
2003-12-16 07:23:54 +00:00
default: ASSERT(0); return false;
}
}
2003-12-10 05:00:28 +00:00
RageFileObj *RageFileObjDirect::Copy( RageFile &p ) const
{
int err;
RageFileObj *ret = MakeFileObjDirect( path, parent.GetOpenMode(), p, err );
if( ret == NULL )
RageException::Throw("Couldn't reopen \"%s\": %s", path.c_str(), strerror(err) );
2003-12-10 05:48:31 +00:00
ret->Seek( parent.Tell() );
2003-12-10 05:00:28 +00:00
return ret;
2003-12-04 08:25:59 +00:00
}
2003-12-05 00:07:18 +00:00
bool RageFileDriverDirect::Ready()
{
2004-01-06 05:40:24 +00:00
return PathReady( root );
2003-12-05 00:07:18 +00:00
}
2003-12-24 11:54:11 +00:00
static const unsigned int BUFSIZE = 1024*64;
2003-12-10 05:00:28 +00:00
RageFileObjDirect::RageFileObjDirect( const CString &path_, int fd_, RageFile &p ):
2003-12-04 08:25:59 +00:00
RageFileObj( p )
{
2003-12-10 05:00:28 +00:00
path = path_;
2003-12-04 08:25:59 +00:00
fd = fd_;
ASSERT( fd != -1 );
2003-12-11 05:00:18 +00:00
if( parent.GetOpenMode() & RageFile::WRITE )
2003-12-11 21:48:19 +00:00
write_buf.reserve( BUFSIZE );
2003-12-04 08:25:59 +00:00
}
RageFileObjDirect::~RageFileObjDirect()
{
bool failed = false;
if( parent.GetOpenMode() & RageFile::WRITE )
{
/* Flush the output buffer. */
Flush();
if( !(parent.GetOpenMode() & RageFile::STREAMED) )
{
/* Force a kernel buffer flush. */
if( fsync( fd ) == -1 )
{
LOG->Warn("Error synchronizing %s: %s", this->path.c_str(), strerror(errno) );
SetError( strerror(errno) );
failed = true;
}
}
}
2003-12-11 05:00:18 +00:00
2003-12-04 08:25:59 +00:00
if( fd != -1 )
{
if( close( fd ) == -1 )
{
LOG->Warn("Error closing %s: %s", this->path.c_str(), strerror(errno) );
SetError( strerror(errno) );
failed = true;
}
}
/* If we failed to flush the file properly, something's amiss--don't touch the original file! */
if( !failed &&
(parent.GetOpenMode()&RageFile::WRITE) &&
!(parent.GetOpenMode() & RageFile::STREAMED) )
{
/*
* We now have path written to MakeTempFilename(path). Rename the temporary
* file over the real path. This should be an atomic operation with a journalling
* filesystem. That is, there should be no intermediate state a JFS might restore
* the file we're writing (in the case of a crash/powerdown) to an empty or partial
* file.
*
* If we want to keep a backup, we can move the old file to "path.old" and then
* the new file to "path". However, this leaves an intermediate state between
* the renames where no "path" exists, so if we're restored to that, then we
* won't be able to find the file. The data is still there--as path.old, provided
* it's not overwritten again--but the user will have to recover it on his own.
* A safer (but much slower) way to do this is to simply CopyFile a backup first.
*/
CString sOldPath = MakeTempFilename(path);
CString sNewPath = path;
#if defined(WIN32)
2003-12-21 07:44:08 +00:00
if( WinMoveFile(sOldPath, sNewPath) )
return;
2003-12-21 07:44:08 +00:00
/* We failed. */
int err = GetLastError();
const CString error = werr_ssprintf( err, "Error renaming \"%s\" to \"%s\"", sOldPath.c_str(), sNewPath.c_str() );
LOG->Warn( "%s", error.c_str() );
SetError( error );
#else
if( rename( sOldPath, sNewPath ) == -1 )
{
LOG->Warn( "Error renaming \"%s\" to \"%s\": %s",
sOldPath.c_str(), sNewPath.c_str(), strerror(errno) );
SetError( strerror(errno) );
}
#endif
}
2003-12-04 08:25:59 +00:00
}
int RageFileObjDirect::Read( void *buf, size_t bytes )
{
int ret = read( fd, buf, bytes );
if( ret == -1 )
{
SetError( strerror(errno) );
return -1;
}
return ret;
}
2003-12-12 07:47:38 +00:00
int RageFileObjDirect::Flush()
2003-12-11 21:48:19 +00:00
{
if( !write_buf.size() )
return 0;
int ret = write( fd, write_buf.data(), write_buf.size() );
if( ret == -1 )
{
LOG->Warn("Error writing %s: %s", this->path.c_str(), strerror(errno) );
SetError( strerror(errno) );
}
write_buf.erase();
2003-12-13 06:52:25 +00:00
write_buf.reserve( BUFSIZE );
2003-12-11 21:48:19 +00:00
return ret;
}
2003-12-04 08:25:59 +00:00
int RageFileObjDirect::Write( const void *buf, size_t bytes )
{
2003-12-11 21:48:19 +00:00
if( write_buf.size()+bytes > BUFSIZE )
{
2003-12-12 07:47:38 +00:00
if( Flush() == -1 )
2003-12-11 21:48:19 +00:00
return -1;
2003-12-13 06:52:25 +00:00
ASSERT( !write_buf.size() );
/* The buffer is cleared. If we still don't have space, it's bigger than
* the buffer size, so just write it directly. */
if( bytes >= BUFSIZE )
{
int ret = write( fd, buf, bytes );
if( ret == -1 )
{
LOG->Warn("Error writing %s: %s", this->path.c_str(), strerror(errno) );
SetError( strerror(errno) );
return -1;
}
return bytes;
}
2003-12-11 21:48:19 +00:00
}
2003-12-11 05:00:18 +00:00
write_buf.append( (const char *)buf, (const char *)buf+bytes );
return bytes;
2003-12-04 08:25:59 +00:00
}
void RageFileObjDirect::Rewind()
{
lseek( fd, 0, SEEK_SET );
}
int RageFileObjDirect::Seek( int offset )
{
return lseek( fd, offset, SEEK_SET );
}
/*
* Copyright (c) 2003 by the person(s) listed below. All rights reserved.
* Glenn Maynard
2003-12-05 00:07:18 +00:00
* Chris Danford
2003-12-04 08:25:59 +00:00
*/