// FileCopy //------------------------------------------------------------------------------ void TestFileIO::FileCopy() const { // generate a process unique file path AStackString<> path; GenerateTempFileName( path ); // generate copy file name AStackString<> pathCopy( path ); pathCopy += ".copy"; // make sure nothing is left from previous runs FileIO::FileDelete( path.Get() ); FileIO::FileDelete( pathCopy.Get() ); TEST_ASSERT( FileIO::FileExists( path.Get() ) == false ); TEST_ASSERT( FileIO::FileExists( pathCopy.Get() ) == false ); // create it FileStream f; TEST_ASSERT( f.Open( path.Get(), FileStream::WRITE_ONLY ) == true ); f.Close(); // copy it TEST_ASSERT( FileIO::FileCopy( path.Get(), pathCopy.Get() ) ); TEST_ASSERT( FileIO::FileExists( pathCopy.Get() ) == true ); // copy without overwrite allowed should fail const bool allowOverwrite = false; TEST_ASSERT( FileIO::FileCopy( path.Get(), pathCopy.Get(), allowOverwrite ) == false ); // cleanup VERIFY( FileIO::FileDelete( path.Get() ) ); VERIFY( FileIO::FileDelete( pathCopy.Get() ) ); }
//------------------------------------------------------------------------------ /*static*/ bool FileIO::EnsurePathExists( const AString & path ) { // if the entire path already exists, nothing is to be done if( DirectoryExists( path ) ) { return true; } // take a copy to locally manipulate AStackString<> pathCopy( path ); PathUtils::FixupFolderPath( pathCopy ); // ensure correct slash type and termination // handle UNC paths by skipping leading slashes and machine name char * slash = pathCopy.Get(); #if defined( __WINDOWS__ ) if ( *slash == NATIVE_SLASH ) { while ( *slash == NATIVE_SLASH ) { ++slash; } // skip leading double slash while ( *slash != NATIVE_SLASH ) { ++slash; } // skip machine name ++slash; // move into first dir name, so next search will find dir name slash } #endif #if defined( __LINUX__ ) || defined( __APPLE__ ) // for full paths, ignore the first slash if ( *slash == NATIVE_SLASH ) { ++slash; } #endif slash = pathCopy.Find( NATIVE_SLASH, slash ); if ( !slash ) { return false; } do { // truncate the string to the sub path *slash = '\000'; if ( DirectoryExists( pathCopy ) == false ) { // create this level if ( DirectoryCreate( pathCopy ) == false ) { return false; // something went wrong } } *slash = NATIVE_SLASH; // put back the slash slash = pathCopy.Find( NATIVE_SLASH, slash + 1 ); } while ( slash ); return true; }
void buildJavaArgs(CONFIG *config) { // main class LPCSTR mainClass = "com.questdb.BootstrapMain"; // put together static java opts LPCSTR javaOpts = "-da" \ " -XX:+PrintGCApplicationStoppedTime" \ " -XX:+PrintSafepointStatistics" \ " -XX:PrintSafepointStatisticsCount=1" \ " -XX:+UseParNewGC" \ " -XX:+UseConcMarkSweepGC" \ " -XX:+PrintGCDetails" \ " -XX:+PrintGCTimeStamps" \ " -XX:+PrintGCDateStamps" \ " -XX:+UnlockDiagnosticVMOptions" \ " -XX:GuaranteedSafepointInterval=90000000" \ " -XX:-UseBiasedLocking" \ " -XX:BiasedLockingStartupDelay=0"; // put together classpath char classpath[strlen(config->exeName) + 64]; memset(classpath, 0, sizeof(classpath)); pathCopy(classpath, config->exeName); strcat(classpath, "\\questdb.jar"); // put together command line char *args = malloc((strlen(javaOpts) + strlen(classpath) + strlen(mainClass) + strlen(config->dir) + 256) * sizeof(char)); strcpy(args, javaOpts); strcat(args, " -cp \""); strcat(args, classpath); strcat(args, "\" "); strcat(args, mainClass); strcat(args, " -d \""); strcat(args, config->dir); strcat(args, "\""); if (config->forceCopy) { strcat(args, " -f"); } config->javaArgs = args; }
// GetFilesEx //------------------------------------------------------------------------------ /*static*/ bool FileIO::GetFilesEx( const AString & path, const Array< AString > * patterns, bool recurse, Array< FileInfo > * results ) { ASSERT( results ); size_t oldSize = results->GetSize(); if ( recurse ) { // make a copy of the path as it will be modified during recursion AStackString< 256 > pathCopy( path ); PathUtils::EnsureTrailingSlash( pathCopy ); GetFilesRecurseEx( pathCopy, patterns, results ); } else { GetFilesNoRecurseEx( path.Get(), patterns, results ); } return ( results->GetSize() != oldSize ); }
/* * recursiveCp - copy a file to a directory (recursively entered) * * source_head points to a buffer of size _MAX_PATH that ends with a ':' * or a FILESEP. * source_tail points to the null terminator of source_head. * source_wild is the filename/pattern to append to source_head to get the * names of the file(s) to copy. * dest_head points to a buffer of size _MAX_PATH that ends with a ':' * or a FILESEP * dest_tail points to the null terminator of dest_head. * * Note that the buffers source_head/dest_head are passed down the * recursion to save stack space. */ static void recursiveCp( char *source_head, char *source_tail, char *source_wild, char *dest_head, char *dest_tail ) { DIR *directory; struct dirent *nextdirentry; void *crx = NULL; char *new_source_tail; char *new_dest_tail; pathCopy( source_head, source_tail, "*.*" ); directory = opendir( source_head ); if( directory == NULL ) { DropPrintALine( "file \"%s\" not found", source_head ); return; } if( rxflag ) { char *err = FileMatchInit( &crx, source_wild ); if( err != NULL ) { Die( "\"%s\": %s\n", err, source_wild ); } } /* * loop through all files */ while( ( nextdirentry = readdir( directory ) ) != NULL ) { /* * set up file name, then try to copy it */ FNameLower( nextdirentry->d_name ); if( rxflag ) { if( !FileMatch( crx, nextdirentry->d_name ) ) { continue; } } else { if( !FileMatchNoRx( nextdirentry->d_name, source_wild ) ) { continue; } } new_source_tail = pathCopy( source_head, source_tail, nextdirentry->d_name ); new_dest_tail = pathCopy( dest_head, dest_tail, nextdirentry->d_name ); if( nextdirentry->d_attr & _A_SUBDIR ) { if( !IsDotOrDotDot( nextdirentry->d_name ) && rflag ) { int rc; rc = mkdir( dest_head ); if( !rc ) { DirCnt++; } if( !sflag ) { if( rc ) { PrintALineThenDrop( "directory %s already exists", dest_head ); } else { PrintALineThenDrop( "created new directory %s", dest_head ); } } new_dest_tail = pathCopy( dest_head, new_dest_tail, FILESEPSTR ); new_source_tail = pathCopy( source_head, new_source_tail, FILESEPSTR ); recursiveCp( source_head, new_source_tail, rxflag ? "*" : "*.*", dest_head, new_dest_tail ); } } else { CopyOneFile( dest_head, source_head ); } } closedir( directory ); if( rxflag ) { FileMatchFini( crx ); } } /* DoCP */
// GetFilesNoRecurse //------------------------------------------------------------------------------ /*static*/ void FileIO::GetFilesNoRecurse( const char * path, const char * wildCard, Array< AString > * results ) { AStackString< 256 > pathCopy( path ); PathUtils::EnsureTrailingSlash( pathCopy ); const uint32_t baseLength = pathCopy.GetLength(); #if defined( __WINDOWS__ ) pathCopy += '*'; WIN32_FIND_DATA findData; //HANDLE hFind = FindFirstFile( pathCopy.Get(), &findData ); HANDLE hFind = FindFirstFileEx( pathCopy.Get(), FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, 0 ); if ( hFind == INVALID_HANDLE_VALUE) { return; } do { if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { continue; } if ( PathUtils::IsWildcardMatch( wildCard, findData.cFileName ) ) { pathCopy.SetLength( baseLength ); pathCopy += findData.cFileName; results->Append( pathCopy ); } } while ( FindNextFile( hFind, &findData ) != 0 ); FindClose( hFind ); #elif defined( __LINUX__ ) || defined( __APPLE__ ) DIR * dir = opendir( pathCopy.Get() ); if ( dir == nullptr ) { return; } for ( ;; ) { dirent * entry = readdir( dir ); if ( entry == nullptr ) { break; // no more entries } // dir? if ( ( entry->d_type & DT_DIR ) == DT_DIR ) { // ignore dirs continue; } // file - does it match wildcard? if ( PathUtils::IsWildcardMatch( wildCard, entry->d_name ) ) { pathCopy.SetLength( baseLength ); pathCopy += entry->d_name; results->Append( pathCopy ); } } closedir( dir ); #else #error Unknown platform #endif }
// GetFilesNoRecurseEx //------------------------------------------------------------------------------ /*static*/ void FileIO::GetFilesNoRecurseEx( const char * path, const Array< AString > * patterns, Array< FileInfo > * results ) { AStackString< 256 > pathCopy( path ); PathUtils::EnsureTrailingSlash( pathCopy ); const uint32_t baseLength = pathCopy.GetLength(); #if defined( __WINDOWS__ ) pathCopy += '*'; WIN32_FIND_DATA findData; HANDLE hFind = FindFirstFileEx( pathCopy.Get(), FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, 0 ); if ( hFind == INVALID_HANDLE_VALUE) { return; } do { if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { continue; } if ( IsMatch( patterns, findData.cFileName ) ) { pathCopy.SetLength( baseLength ); pathCopy += findData.cFileName; if ( results->GetSize() == results->GetCapacity() ) { results->SetCapacity( results->GetSize() * 2 ); } results->SetSize( results->GetSize() + 1 ); FileInfo & newInfo = results->Top(); newInfo.m_Name = pathCopy; newInfo.m_Attributes = findData.dwFileAttributes; newInfo.m_LastWriteTime = (uint64_t)findData.ftLastWriteTime.dwLowDateTime | ( (uint64_t)findData.ftLastWriteTime.dwHighDateTime << 32 ); newInfo.m_Size = (uint64_t)findData.nFileSizeLow | ( (uint64_t)findData.nFileSizeHigh << 32 ); } } while ( FindNextFile( hFind, &findData ) != 0 ); FindClose( hFind ); #elif defined( __LINUX__ ) || defined( __APPLE__ ) DIR * dir = opendir( pathCopy.Get() ); if ( dir == nullptr ) { return; } for ( ;; ) { dirent * entry = readdir( dir ); if ( entry == nullptr ) { break; // no more entries } // dir? if ( ( entry->d_type & DT_DIR ) == DT_DIR ) { // ingnore dirs continue; } // file - does it match wildcard? if ( IsMatch( patterns, entry->d_name ) ) { pathCopy.SetLength( baseLength ); pathCopy += entry->d_name; if ( results->GetSize() == results->GetCapacity() ) { results->SetCapacity( results->GetSize() * 2 ); } results->SetSize( results->GetSize() + 1 ); FileInfo & newInfo = results->Top(); newInfo.m_Name = pathCopy; // get additional info struct stat info; VERIFY( stat( pathCopy.Get(), &info ) == 0 ); newInfo.m_Attributes = info.st_mode; #if defined( __APPLE__ ) newInfo.m_LastWriteTime = ( ( (uint64_t)info.st_mtimespec.tv_sec * 1000000000ULL ) + (uint64_t)info.st_mtimespec.tv_nsec ); #else newInfo.m_LastWriteTime = ( ( (uint64_t)info.st_mtim.tv_sec * 1000000000ULL ) + (uint64_t)info.st_mtim.tv_nsec ); #endif newInfo.m_Size = info.st_size; } } closedir( dir ); #else #error Unknown platform #endif }
std::string FileSystemAbstraction::EnsureNoPathSepAtEnd(const std::string& path) { std::string pathCopy(path); EnsureNoPathSepAtEnd(pathCopy); return pathCopy; }
// GetFilesNoRecurse //------------------------------------------------------------------------------ /*static*/ void FileIO::GetFilesNoRecurse( const char * path, const char * wildCard, Array< AString > * results ) { AStackString< 256 > pathCopy( path ); PathUtils::EnsureTrailingSlash( pathCopy ); const uint32_t baseLength = pathCopy.GetLength(); #if defined( __WINDOWS__ ) pathCopy += '*'; WIN32_FIND_DATA findData; HANDLE hFind = FindFirstFileEx( pathCopy.Get(), FindExInfoBasic, &findData, FindExSearchNameMatch, nullptr, 0 ); if ( hFind == INVALID_HANDLE_VALUE) { return; } do { if ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) { continue; } if ( PathUtils::IsWildcardMatch( wildCard, findData.cFileName ) ) { pathCopy.SetLength( baseLength ); pathCopy += findData.cFileName; results->Append( pathCopy ); } } while ( FindNextFile( hFind, &findData ) != 0 ); FindClose( hFind ); #elif defined( __LINUX__ ) || defined( __APPLE__ ) DIR * dir = opendir( pathCopy.Get() ); if ( dir == nullptr ) { return; } for ( ;; ) { dirent * entry = readdir( dir ); if ( entry == nullptr ) { break; // no more entries } bool isDir = ( entry->d_type == DT_DIR ); // Not all filesystems have support for returning the file type in // d_type and applications must properly handle a return of DT_UNKNOWN. if ( entry->d_type == DT_UNKNOWN ) { pathCopy.SetLength( baseLength ); pathCopy += entry->d_name; struct stat info; VERIFY( stat( pathCopy.Get(), &info ) == 0 ); isDir = S_ISDIR( info.st_mode ); } // dir? if ( isDir ) { // ignore dirs continue; } // file - does it match wildcard? if ( PathUtils::IsWildcardMatch( wildCard, entry->d_name ) ) { pathCopy.SetLength( baseLength ); pathCopy += entry->d_name; results->Append( pathCopy ); } } closedir( dir ); #else #error Unknown platform #endif }
std::string FileSystemHandler::EnsureNoPathSepAtEnd(const std::string& path) { std::string pathCopy(path); EnsureNoPathSepAtEnd(pathCopy); return pathCopy; }