예제 #1
0
// 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;
  }
예제 #2
0
// 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();
  }
예제 #3
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;
  }
예제 #4
0
파일: migration.cpp 프로젝트: gahr/fxite
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;
      }
    }
  }
}