void GMSyncTask::import_and_update(){ FXStat data; FXString name; database->beginTask(); taskmanager->setStatus("Syncing Files.."); for (FXint i=0;i<files.no() && processing;i++) { if (FXStat::statLink(files[i],data)) { if (data.isDirectory()) { if(!(name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0)))) { if(files[i].tail()==PATHSEP) traverse(files[i].rafter(PATHSEP),nullptr,data.index()); else traverse(files[i],nullptr,data.index()); } } } } if (changed) { database->sync_album_year(); database->sync_tracks_removed(); } database->commitTask(); }
void GMSyncTask::traverse(const FXString & path,Seen * seen,FXlong index) { FXDir directory; FXStat data; FXString name; // Location Seen here={seen,index}; // First scan subfolders if (directory.open(path)) { while(directory.next(name) && processing) { if (FXStat::statLink(path+PATHSEPSTRING+name,data)) { if (data.isDirectory()) { if(!(name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0)))){ for(Seen *s=seen; s; s=s->next){ if(data.index()==s->node) goto next_dir; } if (options.exclude_folder.empty() || !FXPath::match(name,options.exclude_folder,matchflags)) traverse(path+PATHSEPSTRING+name,&here,data.index()); } } } next_dir:; } directory.close(); } // Scan files in current folder if(directory.open(path) && processing) { FXint path_index = dbtracks.hasPath(path); while(directory.next(name) && processing) { if (FXStat::statFile(path+PATHSEPSTRING+name,data)) { if (!data.isDirectory() && data.isReadable()) { if (options.exclude_file.empty() || !FXPath::match(name,options.exclude_file,matchflags)) { if (FXPath::match(name,FILE_PATTERNS,matchflags)) { parse_update(path,name,options_sync.update_always ? forever : data.modified(),path_index); } } } } } if (processing) update_tracks(path_index); } }
void GMImportTask::import() { FXStat data; FXString name; FXint dircount=0; database->beginTask(); if (dbtracks.playlist) dbtracks.playlist_queue = database->getNextQueue(dbtracks.playlist); // Run through all directories for (FXint i=0;i<files.no() && processing;i++) { if (FXStat::statLink(files[i],data)) { if (data.isDirectory()) { if(!(name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0)))) { if(files[i].tail()==PATHSEP) scan(files[i].rafter(PATHSEP),nullptr,data.index()); else scan(files[i],nullptr,data.index()); } dircount++; } } } // Scan remaining files if (dircount<files.no()) { for (FXint i=0;i<files.no() && processing;i++) { if (FXStat::statFile(files[i],data)) { if (!data.isDirectory() && data.isReadable()) { const FXString path = FXPath::directory(files[i]); const FXString name = FXPath::name(files[i]); if (FXPath::match(name,FILE_PATTERNS,matchflags)){ FXint path_index = dbtracks.hasPath(path); parse(path,name,path_index); } } } } import_tracks(); } database->sync_album_year(); database->commitTask(); }
// Remove file or directory, recursively if allowed bool FXFile::removeFiles(const FXString& path,bool recursive){ FXStat stat; FXTRACE((100,"removeFiles(%s)\n",path.text())); if(FXStat::statLink(path,stat)){ if(stat.isDirectory()){ if(recursive){ FXDir dir(path); FXString name; while(dir.next()){ name=dir.name(); if(name[0]=='.' && (name[1]=='\0' || (name[1]=='.' && name[2]=='\0'))) continue; if(!FXFile::removeFiles(path+PATHSEP+name,true)) return false; } } return FXDir::remove(path); } return FXFile::remove(path); } return false; }
FXbool FXRemoveFiles::remove(const FXString& path){ FXbool result; FXStat stat; send_filename(path); result = FXStat::statLink(path,stat); if (!result) result = FXStat::statFile(path,stat); if (result) { if(stat.isDirectory()){ FXDir dir(path); FXString name; while(dir.next() && running){ name=dir.name(); if(name[0]=='.' && (name[1]=='\0' || (name[1]=='.' && name[2]=='\0'))) continue; if(!remove(path+PATHSEP+name) && !ignore) return FALSE; } if (ignore) { FXDir::remove(path); return TRUE; } else if (FXDir::remove(path)==FALSE){ queryDelete(path); if (ignore==FALSE) return FALSE; } return TRUE; } if (ignore) { FXFile::remove(path); return TRUE; } if (!FXFile::remove(path)) { queryDelete(path); if (ignore==FALSE) return FALSE; } return TRUE; } return FALSE; }
// Recursively visit files and directories FXbool FXDirVisitor::traverse(const FXString& path){ FXStat data; if(FXStat::statLink(path,data)){ if(data.isDirectory()){ if(enter(path)){ FXDir directory(path); FXString name; while(directory.next(name)){ if(name[0]!='.' || (name[1]!=0 && (name[1]!='.' || name[2]!=0))){ if(!traverse(path+(ISPATHSEP(path.tail())?"":PATHSEPSTRING)+name)) break; } } return leave(path); } } else{ return visit(path); // Its a file } } return false; }
// List all the files in directory FXint FXDir::listFiles(FXString*& filelist,const FXString& path,const FXString& pattern,FXuint flags){ FXDir dir(path); // Initialize to empty filelist=NULL; // Get directory stream pointer if(dir.isOpen()){ FXuint mode=(flags&CaseFold)?(FILEMATCH_FILE_NAME|FILEMATCH_NOESCAPE|FILEMATCH_CASEFOLD):(FILEMATCH_FILE_NAME|FILEMATCH_NOESCAPE); FXString *newlist; FXint size=0; FXint count=0; FXString pathname; FXString name; FXStat data; // Loop over directory entries while(dir.next(name)){ // Build full pathname pathname=path; if(!ISPATHSEP(pathname.tail())) pathname+=PATHSEPSTRING; pathname+=name; // Get info on file if(!FXStat::statFile(pathname,data)) continue; #ifdef WIN32 // Filter out files; a bit tricky... if(!data.isDirectory() && ((flags&NoFiles) || (data.isHidden() && !(flags&HiddenFiles)) || (!(flags&AllFiles) && !FXPath::match(pattern,name,mode)))) continue; // Filter out directories; even more tricky! if(data.isDirectory() && ((flags&NoDirs) || (data.isHidden() && !(flags&HiddenDirs)) || ((name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) && (flags&NoParent)) || (!(flags&AllDirs) && !FXPath::match(pattern,name,mode)))) continue; #else // Filter out files; a bit tricky... if(!data.isDirectory() && ((flags&NoFiles) || (name[0]=='.' && !(flags&HiddenFiles)) || (!(flags&AllFiles) && !FXPath::match(pattern,name,mode)))) continue; // Filter out directories; even more tricky! if(data.isDirectory() && ((flags&NoDirs) || (name[0]=='.' && !(flags&HiddenDirs)) || ((name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) && (flags&NoParent)) || (!(flags&AllDirs) && !FXPath::match(pattern,name,mode)))) continue; #endif // Grow list if(count+1>=size){ size=size?(size<<1):256; newlist=new FXString [size]; for(int i=0; i<count; i++){ newlist[i].adopt(filelist[i]); } delete [] filelist; filelist=newlist; } // Add to list filelist[count++].adopt(name); } return count; } return 0; }
// Recursively copy files or directories from srcfile to dstfile, overwriting dstfile if allowed bool FXFile::copyFiles(const FXString& srcfile,const FXString& dstfile,bool overwrite){ if(srcfile!=dstfile){ FXString name,linkname; FXStat srcstat; FXStat dststat; FXTRACE((100,"FXFile::copyFiles(%s,%s)\n",srcfile.text(),dstfile.text())); if(FXStat::statLink(srcfile,srcstat)){ // Destination is a directory? if(FXStat::statLink(dstfile,dststat)){ if(!dststat.isDirectory()){ if(!overwrite) return false; //FXTRACE((100,"FXFile::remove(%s)\n",dstfile.text())); if(!FXFile::remove(dstfile)) return false; } } // Source is a directory if(srcstat.isDirectory()){ // Make destination directory if needed if(!dststat.isDirectory()){ //FXTRACE((100,"FXDir::create(%s)\n",dstfile.text())); // Make directory if(!FXDir::create(dstfile,srcstat.mode()|FXIO::OwnerWrite)) return false; } // Open source directory FXDir dir(srcfile); // Copy source directory while(dir.next()){ // Next name name=dir.name(); // Skip '.' and '..' if(name[0]=='.' && (name[1]=='\0' || (name[1]=='.' && name[2]=='\0'))) continue; // Recurse if(!FXFile::copyFiles(srcfile+PATHSEP+name,dstfile+PATHSEP+name,overwrite)) return false; } // OK return true; } // Source is a file if(srcstat.isFile()){ //FXTRACE((100,"FXFile::copyFile(%s,%s)\n",srcfile.text(),dstfile.text())); // Simply copy if(!FXFile::copy(srcfile,dstfile,overwrite)) return false; // OK return true; } // Source is symbolic link: make a new one if(srcstat.isLink()){ linkname=FXFile::symlink(srcfile); FXTRACE((100,"symlink(%s,%s)\n",srcfile.text(),dstfile.text())); // New symlink to whatever old one referred to if(!FXFile::symlink(srcfile,dstfile)) return false; // OK return true; } // Source is fifo: make a new one if(srcstat.isFifo()){ //FXTRACE((100,"FXPipe::create(%s)\n",dstfile.text())); #if defined(FX_FOXCOMPAT) && !defined(FX_DISABLEGUI) // Make named pipe if(!FXPipe::create(dstfile,srcstat.mode())) return false; #endif // OK return true; } /* // Source is device: make a new one if(S_ISBLK(status1.st_mode) || S_ISCHR(status1.st_mode) || S_ISSOCK(status1.st_mode)){ FXTRACE((100,"mknod(%s)\n",newfile.text())); return ::mknod(newfile.text(),status1.st_mode,status1.st_rdev)==0; } */ } } return false; }
static void copyConfigFiles(const FXString& srcfile,const FXString& dstfile, FXString &errors,FXint level) { if (srcfile!=dstfile) { FXString name,linkname; FXStat srcstat; FXStat dststat; if (DoStat(srcfile,srcstat)) { if (DoStat(dstfile,dststat)) { errno=0; Error(_("File exists"), dstfile); return; } if (srcstat.isDirectory()) { # ifdef WIN32 if ((level==2) && (FXPath::name(srcfile)=="servers")) { FXDir::remove(srcfile); return; } # endif if (!dststat.isDirectory()) { if (!FXDir::create(dstfile,srcstat.mode()|FXIO::OwnerWrite)) { Error(_("Create failed"), dstfile); return; } } FXDir dir(srcfile); if (dir.isOpen()) { while(dir.next(name)) { if (name[0]=='.' && (name[1]=='\0' || (name[1]=='.' && name[2]=='\0'))) continue; copyConfigFiles(srcfile+PATHSEP+name,dstfile+PATHSEP+name,errors,level+1); } } else { Error(_("Access failed"), srcfile); } return; } FXString newname=dstfile.text(); # ifndef WIN32 if ((level==2) && ((FXPath::name(srcfile)=="settings")||(FXPath::name(srcfile)=="styles"))) { newname+=".rc"; } # endif if (srcstat.isFile()) { if (!FXFile::copy(srcfile,newname,false)) { Error(_("Copy failed"), srcfile); } return; } if (srcstat.isLink()) { linkname=FXFile::symlink(srcfile); if (!FXFile::symlink(linkname,newname)) { Error(_("Link failed"), newname); } return; } if (srcstat.isFifo()) { if (!FXPipe::create(dstfile,srcstat.mode())) { Error(_("Create fifo failed"), dstfile); } return; } if (srcstat.isSocket()) { errno=0; Error(_("Ignored socket"), srcfile); return; } if (srcstat.isCharacter()||srcstat.isBlock()) { errno=0; Error(_("Ignored device node"), srcfile); return; } } } }