Files
itgmania212121/stepmania/src/RageFileDriverDirect.cpp
T

429 lines
11 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"
2005-09-03 21:26:04 +00:00
#include "RageFile.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>
2004-04-05 05:22:32 +00:00
#include <cerrno>
2003-12-04 08:25:59 +00:00
#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
2004-01-17 05:21:24 +00:00
#if !defined(_XBOX)
2003-12-07 04:14:52 +00:00
#include <windows.h>
2004-01-17 05:21:24 +00:00
#endif
2003-12-04 08:25:59 +00:00
#include <io.h>
#endif
2004-02-10 23:39:45 +00:00
static struct FileDriverEntry_DIR: public FileDriverEntry
{
FileDriverEntry_DIR(): FileDriverEntry( "DIR" ) { }
RageFileDriver *Create( CString Root ) const { return new RageFileDriverDirect( Root ); }
} const g_RegisterDriver;
2003-12-04 08:25:59 +00:00
/* This driver handles direct file access. */
class RageFileObjDirect: public RageFileObj
{
public:
2005-06-05 15:15:55 +00:00
RageFileObjDirect( const CString &sPath, int iFD, int iMode );
2003-12-04 08:25:59 +00:00
virtual ~RageFileObjDirect();
virtual int ReadInternal( void *pBuffer, size_t iBytes );
virtual int WriteInternal( const void *pBuffer, size_t iBytes );
virtual int FlushInternal();
virtual int SeekInternal( int offset );
2004-12-11 02:25:38 +00:00
virtual RageFileBasic *Copy() const;
2005-06-05 15:15:55 +00:00
virtual CString GetDisplayPath() const { return m_sPath; }
virtual int GetFileSize() const;
2005-06-01 23:47:56 +00:00
private:
2005-06-05 15:15:55 +00:00
int m_iFD;
CString m_sPath; /* for Copy */
2005-06-01 23:47:56 +00:00
2005-06-05 15:15:55 +00:00
CString m_sWriteBuf;
2005-06-01 23:47:56 +00:00
bool FinalFlush();
int m_iMode;
2003-12-04 08:25:59 +00:00
};
RageFileDriverDirect::RageFileDriverDirect( CString root_ ):
2005-01-27 03:43:55 +00:00
RageFileDriver( new DirectFilenameDB(root_) )
2003-12-04 08:25:59 +00:00
{
2005-01-27 03:43:55 +00:00
Remount( 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.new". Both prepend and append: we don't
* want a wildcard search for the filename to match (foo.txt.new matches foo.txt*),
* and we don't want to have the same extension (so "new.foo.sm" doesn't show up
* in *.sm). */
return Dirname(sPath) + "new." + Basename(sPath) + ".new";
}
2003-12-10 09:43:31 +00:00
RageFileObj *MakeFileObjDirect( CString sPath, int mode, int &err )
2003-12-04 08:25:59 +00:00
{
int fd;
if( mode & RageFile::READ )
{
2005-05-31 23:26:07 +00:00
fd = DoOpen( sPath, O_BINARY|O_RDONLY, 0666 );
2004-09-01 23:12:20 +00:00
/* XXX: Windows returns EACCES if we try to open a file on a CDROM that isn't
* ready, instead of something like ENODEV. We want to return that case as
* ENOENT, but we can't distinguish it from file permission errors. */
}
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. */
2005-05-31 23:26:07 +00:00
fd = DoOpen( out, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0666 );
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, mode );
2003-12-10 05:00:28 +00:00
}
2004-12-11 02:25:38 +00:00
RageFileBasic *RageFileDriverDirect::Open( const CString &path, int mode, int &err )
2003-12-10 05:00:28 +00:00
{
ASSERT( path.size() && path[0] == '/' );
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-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
}
return MakeFileObjDirect( root + sPath, mode, err );
2003-12-10 05:00:28 +00:00
}
2005-09-29 06:35:30 +00:00
bool RageFileDriverDirect::Move( const CString &sOldPath_, const CString &sNewPath_ )
{
CString sOldPath = sOldPath_;
CString sNewPath = sNewPath_;
FDB->ResolvePath( sOldPath );
FDB->ResolvePath( sNewPath );
if( this->GetFileType(sOldPath) == RageFileManager::TYPE_NONE )
return false;
{
const CString dir = Dirname(sNewPath);
CreateDirectories( root + dir );
}
LOG->Trace("rename \"%s\" -> \"%s\"", (root + sOldPath).c_str(), (root + sNewPath).c_str() );
if( DoRename(root + sOldPath, root + sNewPath) == -1 )
{
LOG->Warn( "rename(%s,%s) failed: %s", (root + sOldPath).c_str(), (root + sNewPath).c_str(), strerror(errno) );
return false;
}
return true;
}
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 )
{
2005-06-05 15:15:55 +00:00
LOG->Warn( "remove(%s) failed: %s", (root + sPath).c_str(), strerror(errno) );
2003-12-16 07:23:54 +00:00
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 )
{
2005-06-05 15:15:55 +00:00
LOG->Warn( "rmdir(%s) failed: %s", (root + sPath).c_str(), strerror(errno) );
2003-12-16 07:23:54 +00:00
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;
}
}
2004-12-11 02:25:38 +00:00
RageFileBasic *RageFileObjDirect::Copy() const
2003-12-10 05:00:28 +00:00
{
2005-06-05 15:15:55 +00:00
int iErr;
RageFileObj *ret = MakeFileObjDirect( m_sPath, m_iMode, iErr );
2003-12-10 05:00:28 +00:00
if( ret == NULL )
2005-06-05 15:15:55 +00:00
RageException::Throw( "Couldn't reopen \"%s\": %s", m_sPath.c_str(), strerror(iErr) );
2003-12-10 05:00:28 +00:00
2005-06-05 15:15:55 +00:00
ret->Seek( lseek( m_iFD, 0, SEEK_CUR ) );
2003-12-10 05:48:31 +00:00
2003-12-10 05:00:28 +00:00
return ret;
2003-12-04 08:25:59 +00:00
}
2005-01-27 03:43:55 +00:00
bool RageFileDriverDirect::Remount( const CString &sPath )
{
root = sPath;
2005-01-27 03:54:49 +00:00
((DirectFilenameDB *) FDB)->SetRoot( sPath );
2005-01-27 03:43:55 +00:00
/* If the root path doesn't exist, create it. */
CreateDirectories( root );
return true;
}
2003-12-24 11:54:11 +00:00
static const unsigned int BUFSIZE = 1024*64;
2005-06-05 15:15:55 +00:00
RageFileObjDirect::RageFileObjDirect( const CString &sPath, int iFD, int iMode )
2003-12-04 08:25:59 +00:00
{
2005-06-05 15:15:55 +00:00
m_sPath = sPath;
m_iFD = iFD;
m_iMode = iMode;
ASSERT( m_iFD != -1 );
2003-12-11 05:00:18 +00:00
2004-12-09 11:28:24 +00:00
if( m_iMode & RageFile::WRITE )
2005-06-05 15:15:55 +00:00
m_sWriteBuf.reserve( BUFSIZE );
2003-12-04 08:25:59 +00:00
}
2004-03-25 01:22:08 +00:00
bool RageFileObjDirect::FinalFlush()
2003-12-04 08:25:59 +00:00
{
2004-12-09 11:28:24 +00:00
if( !(m_iMode & RageFile::WRITE) )
2004-03-25 01:22:08 +00:00
return true;
/* Flush the output buffer. */
if( Flush() == -1 )
return false;
/* Only do the rest of the flushes if SLOW_FLUSH is enabled. */
2004-12-09 11:28:24 +00:00
if( !(m_iMode & RageFile::SLOW_FLUSH) )
2004-03-25 01:22:08 +00:00
return true;
/* Force a kernel buffer flush. */
2005-06-05 15:15:55 +00:00
if( fsync( m_iFD ) == -1 )
{
2005-06-05 15:15:55 +00:00
LOG->Warn( "Error synchronizing %s: %s", this->m_sPath.c_str(), strerror(errno) );
2004-03-25 01:22:08 +00:00
SetError( strerror(errno) );
return false;
}
2004-04-16 22:31:40 +00:00
2004-04-19 21:00:18 +00:00
#if !defined(WIN32)
2004-03-25 01:22:08 +00:00
/* Wait for the directory to be flushed. */
2005-06-05 15:15:55 +00:00
int dirfd = open( Dirname(m_sPath), O_RDONLY );
2004-03-25 01:22:08 +00:00
if( dirfd == -1 )
{
2005-06-05 15:15:55 +00:00
LOG->Warn( "Error synchronizing open(%s dir): %s", this->m_sPath.c_str(), strerror(errno) );
2004-03-25 01:22:08 +00:00
SetError( strerror(errno) );
return false;
}
2003-12-11 05:00:18 +00:00
2004-03-25 01:22:08 +00:00
if( fsync( dirfd ) == -1 )
{
2005-06-05 15:15:55 +00:00
LOG->Warn( "Error synchronizing fsync(%s dir): %s", this->m_sPath.c_str(), strerror(errno) );
2004-03-25 01:22:08 +00:00
SetError( strerror(errno) );
close( dirfd );
return false;
}
close( dirfd );
2004-04-19 21:00:18 +00:00
#endif
2004-03-25 01:22:08 +00:00
return true;
}
RageFileObjDirect::~RageFileObjDirect()
{
bool failed = !FinalFlush();
2005-06-05 15:15:55 +00:00
if( m_iFD != -1 )
{
2005-06-05 15:15:55 +00:00
if( close( m_iFD ) == -1 )
{
2005-06-05 15:15:55 +00:00
LOG->Warn( "Error closing %s: %s", this->m_sPath.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 &&
2004-12-09 11:28:24 +00:00
(m_iMode & RageFile::WRITE) &&
!(m_iMode & RageFile::STREAMED) )
{
/*
2005-06-05 15:15:55 +00:00
* We now have path written to MakeTempFilename(m_sPath). 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.
*/
2005-06-05 15:15:55 +00:00
CString sOldPath = MakeTempFilename(m_sPath);
CString sNewPath = m_sPath;
#if defined(WIN32)
if( WinMoveFile(DoPathReplace(sOldPath), DoPathReplace(sNewPath)) )
2003-12-21 07:44:08 +00:00
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
}
2005-06-05 15:15:55 +00:00
int RageFileObjDirect::ReadInternal( void *pBuf, size_t iBytes )
2003-12-04 08:25:59 +00:00
{
2005-06-05 15:15:55 +00:00
int iRet = read( m_iFD, pBuf, iBytes );
if( iRet == -1 )
2003-12-04 08:25:59 +00:00
{
SetError( strerror(errno) );
return -1;
}
2005-06-05 15:15:55 +00:00
return iRet;
2003-12-04 08:25:59 +00:00
}
2004-01-13 00:06:46 +00:00
/* write(), but retry a couple times on EINTR. */
2005-06-05 15:15:55 +00:00
static int retried_write( int iFD, const void *pBuf, size_t iCount )
2004-01-13 00:06:46 +00:00
{
2005-12-06 16:22:11 +00:00
int tries = 3, ret;
do
{
ret = write( iFD, pBuf, iCount );
}
while( ret == -1 && errno == EINTR && tries-- );
2004-01-13 00:06:46 +00:00
2005-12-06 16:22:11 +00:00
return ret;
2004-01-13 00:06:46 +00:00
}
int RageFileObjDirect::FlushInternal()
2003-12-11 21:48:19 +00:00
{
2005-06-05 15:15:55 +00:00
if( !m_sWriteBuf.size() )
2003-12-11 21:48:19 +00:00
return 0;
2005-06-05 15:15:55 +00:00
int iRet = retried_write( m_iFD, m_sWriteBuf.data(), m_sWriteBuf.size() );
if( iRet == -1 )
2003-12-11 21:48:19 +00:00
{
2005-06-05 15:15:55 +00:00
LOG->Warn("Error writing %s: %s", this->m_sPath.c_str(), strerror(errno) );
2003-12-11 21:48:19 +00:00
SetError( strerror(errno) );
}
2005-06-05 15:15:55 +00:00
m_sWriteBuf.erase();
m_sWriteBuf.reserve( BUFSIZE );
return iRet;
2003-12-11 21:48:19 +00:00
}
2005-06-05 15:15:55 +00:00
int RageFileObjDirect::WriteInternal( const void *pBuf, size_t iBytes )
2003-12-04 08:25:59 +00:00
{
2005-06-05 15:15:55 +00:00
if( m_sWriteBuf.size()+iBytes > BUFSIZE )
2003-12-11 21:48:19 +00:00
{
2003-12-12 07:47:38 +00:00
if( Flush() == -1 )
2003-12-11 21:48:19 +00:00
return -1;
2005-06-05 15:15:55 +00:00
ASSERT( !m_sWriteBuf.size() );
2003-12-13 06:52:25 +00:00
/* The buffer is cleared. If we still don't have space, it's bigger than
* the buffer size, so just write it directly. */
2005-06-05 15:15:55 +00:00
if( iBytes >= BUFSIZE )
2003-12-13 06:52:25 +00:00
{
2005-06-05 15:15:55 +00:00
int iRet = retried_write( m_iFD, pBuf, iBytes );
if( iRet == -1 )
2003-12-13 06:52:25 +00:00
{
2005-06-05 15:15:55 +00:00
LOG->Warn("Error writing %s: %s", this->m_sPath.c_str(), strerror(errno) );
2003-12-13 06:52:25 +00:00
SetError( strerror(errno) );
return -1;
}
2005-06-05 15:15:55 +00:00
return iBytes;
2003-12-13 06:52:25 +00:00
}
2003-12-11 21:48:19 +00:00
}
2005-06-05 15:15:55 +00:00
m_sWriteBuf.append( (const char *) pBuf, (const char *) pBuf+iBytes );
return iBytes;
2003-12-04 08:25:59 +00:00
}
2005-06-05 15:15:55 +00:00
int RageFileObjDirect::SeekInternal( int iOffset )
2003-12-04 08:25:59 +00:00
{
2005-06-05 15:15:55 +00:00
return lseek( m_iFD, iOffset, SEEK_SET );
2003-12-04 08:25:59 +00:00
}
int RageFileObjDirect::GetFileSize() const
2004-01-11 22:38:27 +00:00
{
2005-06-05 15:15:55 +00:00
const int iOldPos = lseek( m_iFD, 0, SEEK_CUR );
ASSERT_M( iOldPos != -1, strerror(errno) );
const int iRet = lseek( m_iFD, 0, SEEK_END );
ASSERT_M( iRet != -1, strerror(errno) );
lseek( m_iFD, iOldPos, SEEK_SET );
return iRet;
2004-01-11 22:38:27 +00:00
}
2003-12-04 08:25:59 +00:00
/*
2004-05-06 00:42:06 +00:00
* Copyright (c) 2003-2004 Glenn Maynard, Chris Danford
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
2003-12-04 08:25:59 +00:00
*/
2004-05-06 00:42:06 +00:00