Files
itgmania212121/stepmania/src/RageFileDriverDirectHelpers.cpp
T

347 lines
9.3 KiB
C++
Raw Normal View History

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"
#include "Foreach.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)
2004-01-06 05:35:07 +00:00
/* Wrappers for low-level file functions, to work around Xbox issues: */
int DoMkdir( const CString &sPath, int perm )
{
return mkdir( DoPathReplace(sPath), perm );
2004-01-06 05:35:07 +00:00
}
int DoOpen( const CString &sPath, int flags, int perm )
{
return open( DoPathReplace(sPath), flags, perm );
2004-01-06 05:35:07 +00:00
}
int DoStat( const CString &sPath, struct stat *st )
{
return stat( DoPathReplace(sPath), st );
2004-01-06 05:35:07 +00:00
}
int DoRename( const CString &sOldPath, const CString &sNewPath )
{
CString TempPath = sPath;
TempPath.Replace( "/", "\\" );
return rename( sPath );
}
2004-01-06 05:35:07 +00:00
int DoRemove( const CString &sPath )
{
return remove( DoPathReplace(sPath) );
2004-01-06 05:35:07 +00:00
}
int DoRmdir( const CString &sPath )
{
return rmdir( DoPathReplace(sPath) );
2004-01-06 05:35:07 +00:00
}
HANDLE DoFindFirstFile( const CString &sPath, WIN32_FIND_DATA *fd )
{
return FindFirstFile( DoPathReplace(sPath), fd );
}
#endif
CString DoPathReplace(const CString &sPath)
2004-01-06 05:35:07 +00:00
{
CString TempPath = sPath;
#if defined(XBOX)
TempPath.Replace( "//", "\\" );
2004-01-06 05:35:07 +00:00
TempPath.Replace( "/", "\\" );
#endif
return TempPath;
2004-01-06 05:35:07 +00:00
}
#if defined(WIN32)
2005-05-31 21:48:12 +00:00
static bool WinMoveFileInternal( const CString &sOldPath, const CString &sNewPath )
2004-01-06 05:35:07 +00:00
{
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 ) )
2005-05-31 21:48:12 +00:00
return true;
2004-01-06 05:35:07 +00:00
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
2005-05-31 21:48:12 +00:00
return false;
2004-01-06 05:35:07 +00:00
}
if( MoveFile( sOldPath, sNewPath ) )
2005-05-31 21:48:12 +00:00
return true;
2004-01-06 05:35:07 +00:00
if( GetLastError() != ERROR_ALREADY_EXISTS )
2005-05-31 21:48:12 +00:00
return false;
2004-01-06 05:35:07 +00:00
if( !DeleteFile( sNewPath ) )
2005-05-31 21:48:12 +00:00
return false;
2004-01-06 05:35:07 +00:00
2005-05-31 21:48:12 +00:00
return !!MoveFile( sOldPath, sNewPath );
2004-01-06 05:35:07 +00:00
}
2005-05-31 21:48:12 +00:00
bool WinMoveFile( CString sOldPath, CString sNewPath )
2004-01-06 05:35:07 +00:00
{
if( WinMoveFileInternal( DoPathReplace(sOldPath), DoPathReplace(sNewPath) ) )
2005-05-31 21:48:12 +00:00
return true;
2004-01-06 05:35:07 +00:00
if( GetLastError() != ERROR_ACCESS_DENIED )
2005-05-31 21:48:12 +00:00
return false;
2004-01-06 05:35:07 +00:00
/* Try turning off the read-only bit on the file we're overwriting. */
SetFileAttributes( DoPathReplace(sNewPath), FILE_ATTRIBUTE_NORMAL );
2004-01-06 05:35:07 +00:00
return WinMoveFileInternal( DoPathReplace(sOldPath), DoPathReplace(sNewPath) );
2004-01-06 05:35:07 +00:00
}
2004-01-06 06:00:03 +00:00
#endif
2004-01-06 05:35:07 +00:00
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 )
{
2004-05-08 03:16:22 +00:00
/* XXX: handle "//foo/bar" paths in Windows */
2004-01-06 06:00:03 +00:00
CStringArray parts;
CString curpath;
2004-05-08 03:16:22 +00:00
/* If Path is absolute, add the initial slash ("ignore empty" will remove it). */
if( Path.Left(1) == "/" )
curpath = "/";
/* Ignore empty, so eg. "/foo/bar//baz" doesn't try to create "/foo/bar" twice. */
split( Path, "/", parts, true );
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)
2005-05-31 21:43:39 +00:00
if( curpath.size() == 2 && curpath[1] == ':' ) /* C: */
2004-01-06 06:00:03 +00:00
{
2004-05-08 03:16:22 +00:00
/* Don't try to create the drive letter alone. */
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
2005-05-31 23:26:07 +00:00
if( DoMkdir(curpath, 0777) == 0 )
2004-01-06 06:00:03 +00:00
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;
}
DirectFilenameDB::DirectFilenameDB( CString root_ )
{
ExpireSeconds = 30;
2005-01-27 03:54:49 +00:00
SetRoot( root_ );
}
void DirectFilenameDB::SetRoot( CString root_ )
{
root = root_;
2005-05-31 21:27:55 +00:00
/* "\abcd\" -> "/abcd/": */
root.Replace( "\\", "/" );
/* "/abcd/" -> "/abcd": */
if( root.Right(1) == "/" )
root.erase( root.size()-1, 1 );
}
void DirectFilenameDB::PopulateFileSet( FileSet &fs, const CString &path )
{
CString sPath = path;
2004-10-21 05:43:43 +00:00
#if defined(XBOX)
/* Xbox doesn't handle path names which end with ".", which are used when using an
* alternative song directory */
2005-05-31 21:35:06 +00:00
if( sPath.size() > 0 && sPath.Right(1) == "." )
sPath.erase( sPath.size() - 1 );
2004-10-21 05:43:43 +00:00
#endif
/* 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+"/*" );
if( hFind == INVALID_HANDLE_VALUE )
return;
do {
2005-05-31 22:03:48 +00:00
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;
2005-05-31 22:03:48 +00:00
fs.files.insert( f );
} while( FindNextFile( hFind, &fd ) );
2005-05-31 22:03:48 +00:00
FindClose( hFind );
#else
2004-12-21 10:35:11 +00:00
/* Ugly: POSIX threads are not guaranteed to have per-thread cwds, and only
* a few systems have openat() or equivalent; one or the other is needed
* to do efficient, thread-safe directory traversal. Instead, we have to
* use absolute paths, which forces the system to re-parse the directory
* for each file. This isn't a major issue, since most large directory
2005-05-31 21:43:39 +00:00
* scans are I/O-bound. */
2004-12-21 10:35:11 +00:00
2005-05-31 22:03:48 +00:00
DIR *pDir = opendir(root+sPath);
if( pDir == NULL )
return;
2005-05-31 22:03:48 +00:00
while( struct dirent *pEnt = readdir(pDir) )
{
2005-05-31 22:03:48 +00:00
if( !strcmp(pEnt->d_name, ".") )
continue;
if( !strcmp(pEnt->d_name, "..") )
continue;
File f;
2005-05-31 22:03:48 +00:00
f.SetName( pEnt->d_name );
struct stat st;
2005-05-31 22:03:48 +00:00
if( DoStat(root+sPath + "/" + pEnt->d_name, &st) == -1 )
{
/* If it's a broken symlink, ignore it. Otherwise, warn. */
2005-05-31 22:03:48 +00:00
if( lstat(pEnt->d_name, &st) == 0 )
continue;
/* Huh? */
2005-05-31 22:03:48 +00:00
if( LOG )
LOG->Warn( "Got file '%s' in '%s' from list, but can't stat? (%s)",
pEnt->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);
}
2005-05-31 22:03:48 +00:00
closedir( pDir );
#endif
/*
* Check for any ".ignore" markers. If a marker exists, hide the marker and its
* corresponding file.
* For example, if "file.xml.ignore" exists, hide both it and "file.xml" by
* removing them from the file set.
* Ignore markers are used for convenience during build staging and are not used in
* performance-critical situations. To avoid incurring some of the overheard
* due to ignore markers, delete the file instead instead of using an ignore marker.
*/
static const CString IGNORE_MARKER_BEGINNING = "ignore-";
vector<CString> vsFilesToRemove;
for( set<File>::iterator iter = fs.files.lower_bound(IGNORE_MARKER_BEGINNING);
iter != fs.files.end();
iter++ )
{
if( !BeginsWith( iter->lname, IGNORE_MARKER_BEGINNING ) )
break;
CString sFileLNameToIgnore = iter->lname.Right( iter->lname.length() - IGNORE_MARKER_BEGINNING.length() );
vsFilesToRemove.push_back( iter->name );
vsFilesToRemove.push_back( sFileLNameToIgnore );
}
FOREACH_CONST( CString, vsFilesToRemove, iter )
{
// Erase the file corresponding to the ignore marker
File fileToDelete;
fileToDelete.SetName( *iter );
set<File>::iterator iter2 = fs.files.find( fileToDelete );
if( iter2 != fs.files.end() )
fs.files.erase( iter2 );
}
}
2004-05-06 00:42:06 +00:00
/*
* Copyright (c) 2003-2005 Glenn Maynard, Chris Danford, Renaud Lepage
2004-05-06 00:42:06 +00:00
* 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.
*/