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(); }
// Scan items to see if listing is necessary void FXDirList::scan(FXbool force){ FXString pathname; FXDirItem *item; FXStat info; // Do root first time if(!firstitem || force){ listRootItems(); } // Check all items item=(FXDirItem*)firstitem; while(item){ // Is directory already populated? if(item->isDirectory() && item->getFirst()){ // Get the full path of the item pathname=getItemPathname(item); // Stat this directory; should not fail as parent has been scanned already FXStat::statFile(pathname,info); // Get the mod date of the item FXTime newdate=info.modified(); // Forced, date was changed, or failed to get proper date and counter expired if(force || (item->date!=newdate) || (counter==0)){ // And do the refresh listChildItems(item,force,true); // Remember when we did this item->date=newdate; } // Go deeper if(item->first){ item=(FXDirItem*)item->first; continue; } } // Go up while(!item->next && item->parent){ item=(FXDirItem*)item->parent; } // Go to next item=(FXDirItem*)item->next; } }
void GMSyncTask::update() { GMTrackFilenameList tracklist; FXStringList pathlist; FXStat data; database->beginTask(); taskmanager->setStatus("Updating Files.."); for (FXint i=0;i<files.no() && processing;i++) { database->getPathList(files[i],pathlist); for (FXint p=0;p<pathlist.no() && processing;p++) { const FXString & path = pathlist[p]; if (!options.exclude_folder.empty() && filter_path(options.exclude_folder,path)) continue; const FXint path_index = dbtracks.hasPath(path); database->getFileList(path,tracklist); for (FXint t=0;t<tracklist.no() && processing;t++) { const FXString & name = tracklist[t].filename; if (options.exclude_file.empty() || !FXPath::match(name,options.exclude_file,matchflags)) { if (FXStat::statFile(path+PATHSEPSTRING+name,data)) { if (data.isReadable()) parse_update(path,name,options_sync.update_always ? forever : data.modified(),path_index); else parse_update(path,name,0,path_index); } else if (options_sync.remove_missing) { dbtracks.remove(tracklist[t].id); changed=true; } } } if (processing) update_tracks(path_index); } } if (changed) { database->sync_album_year(); database->sync_tracks_removed(); } 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; }
// Copy srcfile to dstfile, overwriting dstfile if allowed bool FXFile::copy(const FXString& srcfile,const FXString& dstfile,bool overwrite){ if(srcfile!=dstfile){ FXuchar buffer[4096]; FXival nwritten,nread; FXStat stat; FXFile src(srcfile,FXIO::Reading); if(src.isOpen()){ if(FXStat::statFile(srcfile,stat)){ FXFile dst(dstfile,overwrite?FXIO::Writing:FXIO::Writing|FXIO::Exclusive,stat.mode()); if(dst.isOpen()){ while(1){ nread=src.readBlock(buffer,sizeof(buffer)); if(nread<0) return false; if(nread==0) break; nwritten=dst.writeBlock(buffer,nread); if(nwritten<0) return false; } return true; } } } } return false; }
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(); }
// List child items void FXDirList::listChildItems(FXDirItem *par,FXbool force,FXbool notify){ FXDirItem *oldlist=par->list; FXDirItem *newlist=NULL; FXDirItem **po=&oldlist; FXDirItem **pn=&newlist; FXDirItem *olditem=NULL; FXDirItem *newitem=NULL; FXDirItem *link; FXString pathname; FXString directory; FXString name; FXStat info; FXDir dir; FXuint mode; //FXTRACE((1,"FXDirList::listChildItems(%p = \"%s\",%s)\n",par,par->getText().text(),force?"true":"false")); // Path to parent node directory=getItemPathname(par); // Managed to open directory if(dir.open(directory)){ // Process directory entries while(dir.next(name)){ // Never show "." and ".." in the directory list widget if(name[0]=='.' && (name[1]==0 || (name[1]=='.' && name[2]==0))) continue; // Normally don't show hiden files or directories if(name[0]=='.' && !(options&DIRLIST_SHOWHIDDEN)) continue; // Build full pathname of entry pathname=directory; if(!ISPATHSEP(pathname.tail())) pathname+=PATHSEPSTRING; pathname+=name; #ifdef WIN32 // Get file/link info if(!FXStat::statFile(pathname,info)) continue; mode=info.mode(); // Normally don't show hiden files or directories if((mode&FXIO::Hidden) && !(options&DIRLIST_SHOWHIDDEN)) continue; #else // Get file/link info if(!FXStat::statLink(pathname,info)) continue; mode=info.mode(); // If its a link, get file mode from target if(info.isLink()){ mode=FXStat::mode(pathname) | FXIO::SymLink; } #endif // Skip item if it is a file and we want only directories, or if it fails to match wildcard pattern if(!(mode&FXIO::Directory) && !((options&DIRLIST_SHOWFILES) && FXPath::match(name,pattern,matchmode))) continue; // Find it, and take it out from the old list if found for(FXDirItem** pp=po; (olditem=*pp)!=NULL; pp=&olditem->link){ if(compare(olditem->label,name)==0){ *pp=olditem->link; olditem->link=NULL; break; } } // Use a new item if forced, if there was no old item, or if the item information was changed if(force || !olditem || olditem->getDate()!=info.modified() || olditem->getSize()!=info.size() || olditem->getMode()!=mode){ // Make new item newitem=(FXDirItem*)createItem(name,NULL,NULL,NULL); // Update item information newitem->setDraggable(draggable); newitem->setAssoc(NULL); newitem->setSize(info.size()); newitem->setDate(info.modified()); newitem->setMode(mode); // Determine icons and type if(newitem->isDirectory()){ newitem->setHasItems(true); newitem->setOpenIcon(opendiricon); newitem->setClosedIcon(closeddiricon); if(associations) newitem->setAssoc(associations->findDirBinding(pathname)); } else if(newitem->isExecutable()){ newitem->setHasItems(false); newitem->setOpenIcon(applicationicon); newitem->setClosedIcon(applicationicon); if(associations) newitem->setAssoc(associations->findExecBinding(pathname)); } else{ newitem->setHasItems(false); newitem->setOpenIcon(documenticon); newitem->setClosedIcon(documenticon); if(associations) newitem->setAssoc(associations->findFileBinding(pathname)); } // If association is found, use it if(newitem->getAssoc()){ if(newitem->getAssoc()->miniicon) newitem->setClosedIcon(newitem->getAssoc()->miniicon); if(newitem->getAssoc()->miniiconopen) newitem->setOpenIcon(newitem->getAssoc()->miniiconopen); } // Create item if(id()) newitem->create(); // Replace existing item or add new one if(olditem){ newitem->list=olditem->list; // Old items list of children setItem(olditem,newitem,notify); } else{ appendItem(par,newitem,notify); } *pn=newitem; pn=&newitem->link; // Hang newitem in list } // No change else{ *pn=olditem; pn=&olditem->link; // Hang olditem in list } } dir.close(); } // Wipe items remaining in list:- they have disappeared!! for(olditem=oldlist; olditem; olditem=link){ link=olditem->link; removeItem(olditem,notify); } // Now we know for sure whether we really have subitems or not par->setHasItems(!!par->first); // Remember new list par->list=newlist; // Update sort order sortChildItems(par); // Need to layout recalc(); }
// 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; } } } }