Files
itgmania212121/src/RageFileDriverDirect.cpp
T

496 lines
13 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
2006-09-18 22:45:42 +00:00
#include "archutils/Win32/ErrorStrings.h"
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
2009-11-05 01:09:24 +00:00
/* Direct filesystem access: */
2004-02-10 23:39:45 +00:00
static struct FileDriverEntry_DIR: public FileDriverEntry
{
FileDriverEntry_DIR(): FileDriverEntry( "DIR" ) { }
2005-12-28 03:59:52 +00:00
RageFileDriver *Create( const RString &sRoot ) const { return new RageFileDriverDirect( sRoot ); }
2004-02-10 23:39:45 +00:00
} const g_RegisterDriver;
2009-11-05 01:09:24 +00:00
/* Direct read-only filesystem access: */
static struct FileDriverEntry_DIRRO: public FileDriverEntry
{
FileDriverEntry_DIRRO(): FileDriverEntry( "DIRRO" ) { }
RageFileDriver *Create( const RString &sRoot ) const { return new RageFileDriverDirectReadOnly( sRoot ); }
} const g_RegisterDriver2;
2003-12-04 08:25:59 +00:00
/* This driver handles direct file access. */
class RageFileObjDirect: public RageFileObj
{
public:
2005-12-28 03:59:52 +00:00
RageFileObjDirect( const RString &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 );
2006-10-20 00:01:18 +00:00
virtual RageFileObjDirect *Copy() const;
2005-12-28 03:59:52 +00:00
virtual RString GetDisplayPath() const { return m_sPath; }
virtual int GetFileSize() const;
2005-06-01 23:47:56 +00:00
private:
bool FinalFlush();
2005-12-06 17:12:18 +00:00
int m_iFD;
2005-06-01 23:47:56 +00:00
int m_iMode;
2005-12-28 03:59:52 +00:00
RString m_sPath; /* for Copy */
/*
* When not streaming to disk, we write to a temporary file, and rename to the
* real file on completion. If any write, this is aborted. When streaming to
* disk, allow recovering from errors.
*/
bool m_bWriteFailed;
bool WriteFailed() const { return !(m_iMode & RageFile::STREAMED) && m_bWriteFailed; }
2003-12-04 08:25:59 +00:00
};
2005-12-28 03:59:52 +00:00
RageFileDriverDirect::RageFileDriverDirect( const RString &sRoot ):
2005-12-13 03:33:32 +00:00
RageFileDriver( new DirectFilenameDB(sRoot) )
2003-12-04 08:25:59 +00:00
{
2005-12-13 03:33:32 +00:00
Remount( sRoot );
2003-12-04 08:25:59 +00:00
}
2003-12-10 09:43:31 +00:00
2005-12-28 03:59:52 +00:00
static RString MakeTempFilename( const RString &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
static RageFileObjDirect *MakeFileObjDirect( RString sPath, int iMode, int &iError )
2003-12-04 08:25:59 +00:00
{
2005-12-13 03:33:32 +00:00
int iFD;
if( iMode & RageFile::READ )
{
2005-12-13 03:33:32 +00:00
iFD = 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
{
2005-12-28 03:59:52 +00:00
RString sOut;
2005-12-13 03:33:32 +00:00
if( iMode & RageFile::STREAMED )
sOut = sPath;
else
2005-12-13 03:33:32 +00:00
sOut = MakeTempFilename(sPath);
/* Open a temporary file for writing. */
2005-12-13 03:33:32 +00:00
iFD = DoOpen( sOut, O_BINARY|O_WRONLY|O_CREAT|O_TRUNC, 0666 );
2003-12-07 04:14:52 +00:00
}
2005-12-13 03:33:32 +00:00
if( iFD == -1 )
2003-12-04 08:25:59 +00:00
{
2005-12-13 03:33:32 +00:00
iError = errno;
2003-12-04 08:25:59 +00:00
return NULL;
}
2003-12-10 05:00:28 +00:00
#if defined(UNIX)
struct stat st;
if( fstat(iFD, &st) != -1 && (st.st_mode & S_IFDIR) )
{
iError = EISDIR;
close( iFD );
return NULL;
}
#endif
2005-12-13 03:33:32 +00:00
return new RageFileObjDirect( sPath, iFD, iMode );
2003-12-10 05:00:28 +00:00
}
2005-12-28 03:59:52 +00:00
RageFileBasic *RageFileDriverDirect::Open( const RString &sPath_, int iMode, int &iError )
2003-12-10 05:00:28 +00:00
{
2005-12-28 03:59:52 +00:00
RString sPath = sPath_;
2005-12-13 03:33:32 +00:00
ASSERT( sPath.size() && sPath[0] == '/' );
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 );
2005-12-13 03:33:32 +00:00
if( iMode & RageFile::WRITE )
2003-12-10 05:00:28 +00:00
{
2005-12-28 03:59:52 +00:00
const RString dir = Dirname(sPath);
2003-12-10 05:00:28 +00:00
if( this->GetFileType(dir) != RageFileManager::TYPE_DIR )
2005-12-13 03:33:32 +00:00
CreateDirectories( m_sRoot + dir );
2003-12-10 05:00:28 +00:00
}
2005-12-13 03:33:32 +00:00
return MakeFileObjDirect( m_sRoot + sPath, iMode, iError );
2003-12-10 05:00:28 +00:00
}
2005-12-28 03:59:52 +00:00
bool RageFileDriverDirect::Move( const RString &sOldPath_, const RString &sNewPath_ )
{
2005-12-28 03:59:52 +00:00
RString sOldPath = sOldPath_;
RString sNewPath = sNewPath_;
FDB->ResolvePath( sOldPath );
FDB->ResolvePath( sNewPath );
if( this->GetFileType(sOldPath) == RageFileManager::TYPE_NONE )
return false;
2005-12-13 03:33:32 +00:00
{
2005-12-28 03:59:52 +00:00
const RString sDir = Dirname(sNewPath);
2005-12-13 03:33:32 +00:00
CreateDirectories( m_sRoot + sDir );
}
int size = FDB->GetFileSize( sOldPath );
int hash = FDB->GetFileHash( sOldPath );
TRACE( ssprintf("rename \"%s\" -> \"%s\"", (m_sRoot + sOldPath).c_str(), (m_sRoot + sNewPath).c_str()) );
2005-12-13 03:33:32 +00:00
if( DoRename(m_sRoot + sOldPath, m_sRoot + sNewPath) == -1 )
{
WARN( ssprintf("rename(%s,%s) failed: %s", (m_sRoot + sOldPath).c_str(), (m_sRoot + sNewPath).c_str(), strerror(errno)) );
return false;
}
FDB->DelFile( sOldPath );
FDB->AddFile( sNewPath, size, hash, NULL );
return true;
}
2005-12-28 03:59:52 +00:00
bool RageFileDriverDirect::Remove( const RString &sPath_ )
2003-12-16 07:23:54 +00:00
{
2005-12-28 03:59:52 +00:00
RString sPath = sPath_;
2003-12-16 07:23:54 +00:00
FDB->ResolvePath( sPath );
switch( this->GetFileType(sPath) )
{
2004-01-06 05:44:28 +00:00
case RageFileManager::TYPE_FILE:
TRACE( ssprintf("remove '%s'", (m_sRoot + sPath).c_str()) );
2005-12-13 03:33:32 +00:00
if( DoRemove(m_sRoot + sPath) == -1 )
2003-12-16 07:23:54 +00:00
{
WARN( ssprintf("remove(%s) failed: %s", (m_sRoot + 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:
TRACE( ssprintf("rmdir '%s'", (m_sRoot + sPath).c_str()) );
2005-12-13 03:33:32 +00:00
if( DoRmdir(m_sRoot + sPath) == -1 )
2003-12-16 07:23:54 +00:00
{
WARN( ssprintf("rmdir(%s) failed: %s", (m_sRoot + 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;
}
}
2006-10-20 00:01:18 +00:00
RageFileObjDirect *RageFileObjDirect::Copy() const
2003-12-10 05:00:28 +00:00
{
2005-06-05 15:15:55 +00:00
int iErr;
2006-10-20 00:01:18 +00:00
RageFileObjDirect *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
2008-11-24 07:55:11 +00:00
ret->Seek( (int)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-12-28 03:59:52 +00:00
bool RageFileDriverDirect::Remount( const RString &sPath )
2005-01-27 03:43:55 +00:00
{
2005-12-13 03:33:32 +00:00
m_sRoot = 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. */
2005-12-13 03:33:32 +00:00
CreateDirectories( m_sRoot );
2005-01-27 03:43:55 +00:00
return true;
}
2009-11-05 01:09:24 +00:00
/* The DIRRO driver is just like DIR, except writes are disallowed. */
RageFileDriverDirectReadOnly::RageFileDriverDirectReadOnly( const RString &sRoot ):
RageFileDriverDirect( sRoot ) { }
RageFileBasic *RageFileDriverDirectReadOnly::Open( const RString &sPath, int iMode, int &iError )
{
if( iMode & RageFile::WRITE )
{
iError = EROFS;
return NULL;
}
return RageFileDriverDirect::Open( sPath, iMode, iError );
}
bool RageFileDriverDirectReadOnly::Move( const RString &sOldPath, const RString &sNewPath ) { return false; }
bool RageFileDriverDirectReadOnly::Remove( const RString &sPath ) { return false; }
2003-12-24 11:54:11 +00:00
static const unsigned int BUFSIZE = 1024*64;
2005-12-28 03:59:52 +00:00
RageFileObjDirect::RageFileObjDirect( const RString &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_bWriteFailed = false;
2005-06-05 15:15:55 +00:00
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 )
this->EnableWriteBuffering( BUFSIZE );
2003-12-04 08:25:59 +00:00
}
namespace
{
#if !defined(WIN32)
bool FlushDir( RString sPath, RString &sError )
{
/* Wait for the directory to be flushed. */
int dirfd = open( sPath, O_RDONLY );
if( dirfd == -1 )
{
sError = strerror(errno);
return false;
}
if( fsync( dirfd ) == -1 )
{
sError = strerror(errno);
close( dirfd );
return false;
}
close( dirfd );
return true;
}
#else
bool FlushDir( RString sPath, RString &sError )
{
return true;
}
#endif
}
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 )
{
WARN( ssprintf("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
RString sError;
if( !FlushDir(Dirname(m_sPath), sError) )
2004-03-25 01:22:08 +00:00
{
WARN( ssprintf("Error synchronizing fsync(%s dir): %s", this->m_sPath.c_str(), sError.c_str()) );
SetError( sError );
2004-03-25 01:22:08 +00:00
return false;
}
2003-12-11 05:00:18 +00:00
2004-03-25 01:22:08 +00:00
return true;
}
RageFileObjDirect::~RageFileObjDirect()
{
2005-12-13 03:33:32 +00:00
bool bFailed = !FinalFlush();
2004-03-25 01:22:08 +00:00
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 )
{
WARN( ssprintf("Error closing %s: %s", this->m_sPath.c_str(), strerror(errno)) );
SetError( strerror(errno) );
2005-12-13 03:33:32 +00:00
bFailed = true;
}
}
2005-12-06 17:07:21 +00:00
if( !(m_iMode & RageFile::WRITE) || (m_iMode & RageFile::STREAMED) )
return;
/* We now have path written to MakeTempFilename(m_sPath). Rename the temporary
* file over the real path. */
do
{
2005-12-13 03:33:32 +00:00
if( bFailed || WriteFailed() )
break;
/*
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.
*/
2005-12-28 03:59:52 +00:00
RString sOldPath = MakeTempFilename(m_sPath);
RString 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();
2005-12-28 03:59:52 +00:00
const RString error = werr_ssprintf( err, "Error renaming \"%s\" to \"%s\"", sOldPath.c_str(), sNewPath.c_str() );
WARN( ssprintf("%s", error.c_str()) );
2003-12-21 07:44:08 +00:00
SetError( error );
break;
#else
if( rename( sOldPath, sNewPath ) == -1 )
{
2007-04-24 08:56:05 +00:00
WARN( ssprintf("Error renaming \"%s\" to \"%s\": %s",
sOldPath.c_str(), sNewPath.c_str(), strerror(errno)) );
SetError( strerror(errno) );
break;
}
if( m_iMode & RageFile::SLOW_FLUSH )
{
RString sError;
if( !FlushDir(Dirname(m_sPath), sError) )
{
WARN( ssprintf("Error synchronizing fsync(%s dir): %s", this->m_sPath.c_str(), sError.c_str()) );
SetError( sError );
}
}
/* Success. */
return;
#endif
} while(0);
/* The write or the rename failed. Delete the incomplete temporary file. */
DoRemove( MakeTempFilename(m_sPath) );
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-12-13 03:33:32 +00:00
static int RetriedWrite( int iFD, const void *pBuf, size_t iCount )
2004-01-13 00:06:46 +00:00
{
2005-12-13 03:33:32 +00:00
int iTries = 3, iRet;
2005-12-06 16:22:11 +00:00
do
{
2005-12-13 03:33:32 +00:00
iRet = write( iFD, pBuf, iCount );
2005-12-06 16:22:11 +00:00
}
2005-12-13 03:33:32 +00:00
while( iRet == -1 && errno == EINTR && iTries-- );
2004-01-13 00:06:46 +00:00
2005-12-13 03:33:32 +00:00
return iRet;
2004-01-13 00:06:46 +00:00
}
int RageFileObjDirect::FlushInternal()
2003-12-11 21:48:19 +00:00
{
if( WriteFailed() )
{
SetError( "previous write failed" );
return -1;
}
return 0;
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
{
if( WriteFailed() )
{
SetError( "previous write failed" );
return -1;
}
/* The buffer is cleared. If we still don't have space, it's bigger than
* the buffer size, so just write it directly. */
int iRet = RetriedWrite( m_iFD, pBuf, iBytes );
if( iRet == -1 )
2003-12-11 21:48:19 +00:00
{
SetError( strerror(errno) );
m_bWriteFailed = true;
return -1;
2003-12-11 21:48:19 +00:00
}
2005-06-05 15:15:55 +00:00
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
{
return (int)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
{
const int iOldPos = (int)lseek( m_iFD, 0, SEEK_CUR );
2006-12-29 09:55:28 +00:00
ASSERT_M( iOldPos != -1, ssprintf("\"%s\": %s", m_sPath.c_str(), strerror(errno)) );
const int iRet = (int)lseek( m_iFD, 0, SEEK_END );
2006-12-29 09:55:28 +00:00
ASSERT_M( iRet != -1, ssprintf("\"%s\": %s", m_sPath.c_str(), strerror(errno)) );
2005-06-05 15:15:55 +00:00
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