Files
itgmania212121/stepmania/src/RageFileDriverDirect.cpp
T

242 lines
4.7 KiB
C++
Raw Normal View History

2003-12-04 08:25:59 +00:00
#include "global.h"
#include "RageFileDriverDirect.h"
#include "RageUtil.h"
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#if defined(WIN32)
#include <io.h>
#endif
#if !defined(O_BINARY)
#define O_BINARY 0
#endif
/* XXX: Drop FileDB and cache/resolve in here. */
#include "RageUtil_FileDB.h"
/* This driver handles direct file access. */
class RageFileObjDirect: public RageFileObj
{
private:
int fd;
public:
RageFileObjDirect( int fd, RageFile &p );
virtual ~RageFileObjDirect();
virtual int Read(void *buffer, size_t bytes);
virtual int Write(const void *buffer, size_t bytes);
virtual void Rewind();
virtual int Seek( int offset );
virtual int SeekCur( int offset );
virtual int GetFileSize();
};
RageFileDriverDirect::RageFileDriverDirect( CString root_ ):
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-05 02:25:32 +00:00
void FDB_GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo );
void RageFileDriverDirect::GetDirListing( CString sPath, CStringArray &AddTo, bool bOnlyDirs, bool bReturnPathToo )
{
const unsigned OldStart = AddTo.size();
FDB_GetDirListing( root+sPath, AddTo, bOnlyDirs, bReturnPathToo );
if( bReturnPathToo )
{
/* Remove the root path. */
for( unsigned j = OldStart; j < AddTo.size(); ++j )
AddTo[j].erase( 0, root.size() );
}
}
2003-12-04 08:25:59 +00:00
RageFileObj *RageFileDriverDirect::Open( CString sPath, RageFile::OpenMode mode, RageFile &p, int &err )
{
2003-12-05 02:25:32 +00:00
sPath = root + sPath;
2003-12-04 08:25:59 +00:00
ResolvePath( sPath );
int flags = O_BINARY;
if( mode == RageFile::READ )
flags |= O_RDONLY;
else
flags |= O_WRONLY|O_CREAT|O_TRUNC;
2003-12-04 08:44:54 +00:00
2003-12-05 02:25:32 +00:00
int fd = open( sPath, flags, 0644 );
2003-12-04 08:25:59 +00:00
if( fd == -1 )
{
err = errno;
return NULL;
}
return new RageFileObjDirect( fd, p );
}
static bool DoStat(CString sPath, struct stat *st)
{
TrimRight(sPath, "/\\");
return stat(sPath.c_str(), st) != -1;
}
RageFileManager::FileType RageFileDriverDirect::GetFileType( CString sPath )
{
2003-12-05 02:25:32 +00:00
sPath = root + sPath;
2003-12-04 08:25:59 +00:00
ResolvePath( sPath );
struct stat st;
if( !DoStat(sPath, &st) )
return RageFileManager::TYPE_NONE;
if( st.st_mode & S_IFDIR )
return RageFileManager::TYPE_DIR;
return RageFileManager::TYPE_FILE;
}
int RageFileDriverDirect::GetFileSizeInBytes( CString sPath )
{
2003-12-05 02:25:32 +00:00
sPath = root + sPath;
2003-12-04 08:25:59 +00:00
struct stat st;
if( !DoStat(sPath, &st) )
2003-12-05 02:25:32 +00:00
return -1;
2003-12-04 08:25:59 +00:00
return st.st_size;
}
int RageFileDriverDirect::GetFileModTime( CString sPath )
{
2003-12-05 02:25:32 +00:00
sPath = root + sPath;
2003-12-04 08:25:59 +00:00
struct stat st;
if( !DoStat(sPath, &st) )
return -1;
return st.st_mtime;
}
2003-12-05 00:07:18 +00:00
#ifdef _WINDOWS
#include "windows.h"
#endif
bool RageFileDriverDirect::Ready()
{
#ifdef _WINDOWS
// Windows will throw up a message box if we try to write to a
// removable drive with no disk inserted. Find out whether there's a
// disk in the drive w/o writing a file.
// find drive letter
vector<CString> matches;
static Regex parse("^([A-Za-z]+):");
parse.Compare( root, matches );
if( matches.size() != 1 )
return false;
CString sDrive = matches[0];
TCHAR szVolumeNameBuffer[MAX_PATH];
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD lpFileSystemFlags;
TCHAR szFileSystemNameBuffer[MAX_PATH];
BOOL bResult = GetVolumeInformation(
sDrive + ":\\",
szVolumeNameBuffer,
sizeof(szVolumeNameBuffer),
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&lpFileSystemFlags,
szFileSystemNameBuffer,
sizeof(szFileSystemNameBuffer) );
return !!bResult;
#else
// Try to create directory before writing a temp file.
2003-12-05 00:21:29 +00:00
CreateDirectories( root ); // XXX
2003-12-05 00:07:18 +00:00
// Try to write a file.
2003-12-05 00:21:29 +00:00
CString sFile = root + "temp";
2003-12-05 00:07:18 +00:00
RageFile f;
if( !f.Open( sFile, RageFile::WRITE ) )
return false;
f.Close();
remove( sFile );
return true;
#endif
}
2003-12-04 08:25:59 +00:00
RageFileObjDirect::RageFileObjDirect( int fd_, RageFile &p ):
RageFileObj( p )
{
fd = fd_;
ASSERT( fd != -1 );
}
RageFileObjDirect::~RageFileObjDirect()
{
if( fd != -1 )
close( fd );
}
int RageFileObjDirect::Read( void *buf, size_t bytes )
{
int ret = read( fd, buf, bytes );
if( ret == -1 )
{
SetError( strerror(errno) );
return -1;
}
return ret;
}
int RageFileObjDirect::Write( const void *buf, size_t bytes )
{
int ret = write( fd, buf, bytes );
if( ret == -1 )
{
SetError( strerror(errno) );
return -1;
}
return ret;
}
void RageFileObjDirect::Rewind()
{
lseek( fd, 0, SEEK_SET );
}
int RageFileObjDirect::Seek( int offset )
{
return lseek( fd, offset, SEEK_SET );
}
int RageFileObjDirect::SeekCur( int offset )
{
return lseek( fd, offset, SEEK_CUR );
}
int RageFileObjDirect::GetFileSize()
{
const int OldPos = lseek( fd, 0, SEEK_CUR );
const int ret = lseek( fd, 0, SEEK_END );
lseek( fd, OldPos, SEEK_SET );
return ret;
}
/*
* 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
*/