2004-01-06 05:35:07 +00:00
|
|
|
#include "global.h"
|
|
|
|
|
#include "RageFileDriverDirectHelpers.h"
|
|
|
|
|
#include "RageUtil.h"
|
2004-01-06 06:00:03 +00:00
|
|
|
#include "RageLog.h"
|
2004-01-06 05:35:07 +00:00
|
|
|
|
2004-04-05 05:22:32 +00:00
|
|
|
#include <cerrno>
|
2004-01-06 05:35:07 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
|
|
#if !defined(WIN32)
|
|
|
|
|
#include <dirent.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#else
|
2004-01-17 05:21:24 +00:00
|
|
|
#if !defined(_XBOX)
|
2004-01-06 05:35:07 +00:00
|
|
|
#include <windows.h>
|
2004-01-17 05:21:24 +00:00
|
|
|
#endif
|
2004-01-06 05:35:07 +00:00
|
|
|
#include <io.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(XBOX)
|
|
|
|
|
/* Wrappers for low-level file functions, to work around Xbox issues: */
|
|
|
|
|
int DoMkdir( const CString &sPath, int perm )
|
|
|
|
|
{
|
|
|
|
|
CString TempPath = sPath;
|
|
|
|
|
TempPath.Replace( "/", "\\" );
|
|
|
|
|
return mkdir( TempPath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DoOpen( const CString &sPath, int flags, int perm )
|
|
|
|
|
{
|
|
|
|
|
CString TempPath = sPath;
|
|
|
|
|
TempPath.Replace( "/", "\\" );
|
|
|
|
|
return open( TempPath, flags, perm );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DoStat( const CString &sPath, struct stat *st )
|
|
|
|
|
{
|
|
|
|
|
CString TempPath = sPath;
|
|
|
|
|
TempPath.Replace( "/", "\\" );
|
|
|
|
|
return stat( sPath, st );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DoRemove( const CString &sPath )
|
|
|
|
|
{
|
|
|
|
|
CString TempPath = sPath;
|
|
|
|
|
TempPath.Replace( "/", "\\" );
|
|
|
|
|
return remove( sPath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int DoRmdir( const CString &sPath )
|
|
|
|
|
{
|
|
|
|
|
CString TempPath = sPath;
|
|
|
|
|
TempPath.Replace( "/", "\\" );
|
|
|
|
|
return rmdir( sPath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HANDLE DoFindFirstFile( const CString &sPath, WIN32_FIND_DATA *fd )
|
|
|
|
|
{
|
|
|
|
|
CString TempPath = sPath;
|
|
|
|
|
TempPath.Replace( "/", "\\" );
|
|
|
|
|
return FindFirstFile( TempPath, fd );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
|
|
|
|
int WinMoveFileInternal( const CString &sOldPath, const CString &sNewPath )
|
|
|
|
|
{
|
|
|
|
|
static bool Win9x = false;
|
|
|
|
|
|
|
|
|
|
/* Windows botches rename: it returns error if the file exists. In NT,
|
|
|
|
|
* we can use MoveFileEx( new, old, MOVEFILE_REPLACE_EXISTING ) (though I
|
|
|
|
|
* don't know if it has similar atomicity guarantees to rename). In
|
|
|
|
|
* 9x, we're screwed, so just delete any existing file (we aren't going
|
|
|
|
|
* to be robust on 9x anyway). */
|
|
|
|
|
if( !Win9x )
|
|
|
|
|
{
|
|
|
|
|
if( MoveFileEx( sOldPath, sNewPath, MOVEFILE_REPLACE_EXISTING ) )
|
|
|
|
|
return 1;
|
|
|
|
|
|
2004-01-12 09:51:31 +00:00
|
|
|
// On Win9x, MoveFileEx is expected to fail (returns ERROR_CALL_NOT_IMPLEMENTED).
|
|
|
|
|
DWORD err = GetLastError();
|
|
|
|
|
if( err == ERROR_CALL_NOT_IMPLEMENTED )
|
|
|
|
|
Win9x = true;
|
|
|
|
|
else
|
2004-01-06 05:35:07 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if( MoveFile( sOldPath, sNewPath ) )
|
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
|
|
if( GetLastError() != ERROR_ALREADY_EXISTS )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
if( !DeleteFile( sNewPath ) )
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return MoveFile( sOldPath, sNewPath );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int WinMoveFile( CString sOldPath, CString sNewPath )
|
|
|
|
|
{
|
|
|
|
|
#if defined(XBOX)
|
|
|
|
|
sOldPath.Replace( "/", "\\" );
|
|
|
|
|
sNewPath.Replace( "/", "\\" );
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
if( WinMoveFileInternal(sOldPath, sNewPath) )
|
|
|
|
|
return 1;
|
|
|
|
|
if( GetLastError() != ERROR_ACCESS_DENIED )
|
|
|
|
|
return 0;
|
|
|
|
|
/* Try turning off the read-only bit on the file we're overwriting. */
|
|
|
|
|
SetFileAttributes( sNewPath, FILE_ATTRIBUTE_NORMAL );
|
|
|
|
|
|
|
|
|
|
return WinMoveFileInternal( sOldPath, sNewPath );
|
|
|
|
|
}
|
2004-01-06 06:00:03 +00:00
|
|
|
#endif
|
2004-01-06 05:35:07 +00:00
|
|
|
|
|
|
|
|
bool PathReady( CString path )
|
|
|
|
|
{
|
|
|
|
|
#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( path, 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.
|
|
|
|
|
CreateDirectories( path );
|
|
|
|
|
|
|
|
|
|
// Try to write a file.
|
|
|
|
|
const CString sFile = path + "temp";
|
|
|
|
|
int fd = DoOpen( sFile, O_WRONLY|O_CREAT|O_TRUNC );
|
|
|
|
|
if( fd == -1 )
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
close( fd );
|
|
|
|
|
remove( sFile );
|
|
|
|
|
return true;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2004-01-06 06:00:03 +00:00
|
|
|
/* mkdir -p. Doesn't fail if Path already exists and is a directory. */
|
|
|
|
|
bool CreateDirectories( CString Path )
|
|
|
|
|
{
|
|
|
|
|
CStringArray parts;
|
|
|
|
|
CString curpath;
|
2004-03-16 05:01:35 +00:00
|
|
|
split( Path, "/", parts, false );
|
2004-01-06 06:00:03 +00:00
|
|
|
|
|
|
|
|
for(unsigned i = 0; i < parts.size(); ++i)
|
|
|
|
|
{
|
2004-05-01 01:40:58 +00:00
|
|
|
if( i )
|
|
|
|
|
curpath += "/";
|
|
|
|
|
curpath += parts[i];
|
2004-01-06 06:00:03 +00:00
|
|
|
|
|
|
|
|
#if defined(WIN32)
|
2004-05-01 01:40:58 +00:00
|
|
|
if( (curpath.size() == 2 && curpath[1] == ':') || /* C: */
|
|
|
|
|
(curpath.size() == 3 && curpath[1] == ':' && curpath[2] == '/') ) /* C:/ */
|
2004-01-06 06:00:03 +00:00
|
|
|
{
|
2004-05-01 01:40:58 +00:00
|
|
|
/* Don't try to create the drive letter alone, or the root directory. */
|
2004-01-06 06:00:03 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2004-01-06 05:35:07 +00:00
|
|
|
#endif
|
2004-01-06 06:00:03 +00:00
|
|
|
|
|
|
|
|
if( DoMkdir(curpath, 0755) == 0 )
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
/* When creating a directory that already exists over Samba, Windows is
|
|
|
|
|
* returning ENOENT instead of EEXIST. */
|
2004-02-13 22:23:07 +00:00
|
|
|
/* I can't reproduce this anymore. If we get ENOENT, log it but keep
|
|
|
|
|
* going. */
|
|
|
|
|
if( errno == ENOENT && LOG )
|
|
|
|
|
LOG->Warn("Couldn't create %s: %s", curpath.c_str(), strerror(errno) );
|
|
|
|
|
if( errno == EEXIST || errno == ENOENT )
|
|
|
|
|
continue; // we expect to see these errors
|
|
|
|
|
|
2004-01-06 06:00:03 +00:00
|
|
|
if( LOG )
|
2004-02-13 22:23:07 +00:00
|
|
|
{
|
2004-01-06 06:00:03 +00:00
|
|
|
LOG->Warn("Couldn't create %s: %s", curpath.c_str(), strerror(errno) );
|
2004-02-13 22:23:07 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
2004-01-06 06:00:03 +00:00
|
|
|
|
|
|
|
|
/* Make sure it's a directory. */
|
|
|
|
|
struct stat st;
|
|
|
|
|
DoStat( curpath, &st );
|
|
|
|
|
if( !(st.st_mode & S_IFDIR) )
|
|
|
|
|
{
|
|
|
|
|
if( LOG )
|
|
|
|
|
LOG->Warn("Couldn't create %s: path exists and is not a directory", curpath.c_str() );
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2004-02-11 02:58:40 +00:00
|
|
|
DirectFilenameDB::DirectFilenameDB( CString root_ )
|
|
|
|
|
{
|
|
|
|
|
ExpireSeconds = 30;
|
|
|
|
|
root = root_;
|
|
|
|
|
if( root.Right(1) != "/" )
|
|
|
|
|
root += '/';
|
|
|
|
|
if( root == "./" )
|
|
|
|
|
root = "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
if ( sPath.size() > 0 && sPath.Right(1) == "/" )
|
|
|
|
|
sPath.erase( sPath.size() - 1 );
|
|
|
|
|
|
|
|
|
|
HANDLE hFind = DoFindFirstFile( root+sPath+"/*", &fd );
|
2004-05-06 00:01:06 +00:00
|
|
|
CHECKPOINT_M( root+sPath+"/*" );
|
2004-02-11 02:58:40 +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;
|
|
|
|
|
f.hash = fd.ftLastWriteTime.dwLowDateTime;
|
|
|
|
|
|
|
|
|
|
fs.files.insert(f);
|
|
|
|
|
} while( FindNextFile( hFind, &fd ) );
|
|
|
|
|
FindClose(hFind);
|
|
|
|
|
#else
|
|
|
|
|
int OldDir = DoOpen(".", O_RDONLY);
|
|
|
|
|
if( OldDir == -1 )
|
|
|
|
|
RageException::Throw( "Couldn't open(.): %s", strerror(errno) );
|
|
|
|
|
|
|
|
|
|
if( chdir(root+sPath) == -1 )
|
|
|
|
|
{
|
|
|
|
|
/* Only log once per dir. */
|
|
|
|
|
if( LOG && errno != ENOENT )
|
|
|
|
|
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;
|
|
|
|
|
if( DoStat(ent->d_name, &st) == -1 )
|
|
|
|
|
{
|
|
|
|
|
/* 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;
|
|
|
|
|
f.hash = st.st_mtime;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fs.files.insert(f);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
closedir(d);
|
|
|
|
|
if( fchdir( OldDir ) == -1 )
|
|
|
|
|
RageException::Throw( "Couldn't fchdir(): %s", strerror(errno) );
|
|
|
|
|
close( OldDir );
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|