示例#1
0
void cp_r(char *path1,char *path2) {
	int fp1,fp2,buff[BUFFSIZE];
	struct stat info;
	struct dirent *dp;
	char ptr[1000];
	char ptr2[1000];
	DIR *dirp1=opendir(path1); //apro la directory sorgente
	stat(path1,&info);
	if(S_ISREG(info.st_mode)) { //usare read e write per copiare file
		fp1=open(path1,O_RDONLY);
		fp2=open(path2,O_WRONLY | O_CREAT, info.st_mode); // info.st_mode mi permette di copiare i file
		while(read(fp1,buff,BUFFSIZE)>0) {		  // nel nuovo direttorio mantenendo i permessi
			write(fp2,buff,BUFFSIZE);
		}
		close(fp1); close(fp2);
		return;
	}
	else {
		mkdir(path2,/*  S_IRWXU | S_IRWXG | S_IRWXO*/ info.st_mode);
		while((dp=readdir(dirp1))!=NULL) {
			if(dp->d_name[0]!='.') {
				sprintf(ptr,"%s/%s",path1,dp->d_name);
				sprintf(ptr2,"%s/%s",path2,dp->d_name);
				cp_r(ptr,ptr2);
			}
		}
	}		
	closedir(dirp1);
}
示例#2
0
int main (int argc, char *argv[]) { // Copiare il I path nel II path, assumo che il II non esista
	if(argc!=3) {
		fprintf(stderr,"Arguments error\n");
		exit(-1);
	}

	DIR *dirp1 = opendir(argv[1]);
	DIR *dirp2 = opendir(argv[2]);
	if (dirp1==NULL) {
		fprintf(stderr, "Path 1 error\n");
		exit(1);
	}
	if (dirp2!=NULL) {
		fprintf(stderr, "Path 2 exist\n");
		exit(1);
	}
	
	cp_r(argv[1],argv[2]);

	return(0);
}
示例#3
0
/** Build the chroot at the path **/
bool WorkerBee::build_chroot(const std::string &path, uid_t user, gid_t group, string_set &executables, string_set &extra_dirs) {
  // Make the root path
  make_path(strdup(path.c_str()));
  string_set already_copied;
  std::string full_path;
  
  // The base directories  
  string_set base_dirs;
  base_dirs.insert("bin");
  base_dirs.insert("usr");
  base_dirs.insert("var");
  base_dirs.insert("lib");
  base_dirs.insert("home");
  base_dirs.insert("etc");
  
  // Add the extra directories requested
  for (string_set::iterator dir = extra_dirs.begin(); dir != extra_dirs.end(); ++dir) base_dirs.insert(dir->c_str());
  
  for (string_set::iterator dir = base_dirs.begin(); dir != base_dirs.end(); ++dir) {
    if ((*dir->c_str()) == '/') full_path = path + *dir; else full_path = path + '/' + *dir;
    
    // Make the paths
    make_path(full_path.c_str());
  }
  
  // Build the root libraries
  for (string_set::iterator executable = executables.begin(); executable != executables.end(); ++executable) {
    // If we are pointed at an absolute path to a binary
    // then find the linked libraries of the executable
    // If it's not found, then find it, then look up the libraries
    std::string res_bin;
    if (abs_path(*executable))
      res_bin = *executable;
    else {
      res_bin = find_binary(*executable);
    }
    
    // The libraries for the resolved binary 
    bee_files_set *s_libs = libs_for(res_bin);
    
    // collect the libraries and copy them to the full path of the chroot
    for (bee_files_set::iterator bf = s_libs->begin(); bf != s_libs->end(); ++bf) {
      BeeFile bee = *bf;
      std::string s (bee.file_path());
      
      // Don't copy if the file is already copied
      if (already_copied.count(s)) {
      } else {
        // If it is a symlink, then make a symlink, otherwise copy the file
        // If the library starts with a '/' then don't add one, otherwise, do
        // i.e. if libs/hi.so.1 turns into full_path/libs/hi.so.1 otherwise libs.so.1 turns into full_path/libs.so.1
        if (s.c_str()[0] == '/') full_path = path + s.c_str(); else full_path = path + '/' + s.c_str();
        
        if (bee.is_link()) {
          // make symlink
          make_path(dirname(strdup(full_path.c_str()))); 
          if (symlink(bee.sym_origin().c_str(), full_path.c_str())) {
            fprintf(stderr, "Could not make a symlink: %s to %s because %s\n", bee.sym_origin().c_str(), full_path.c_str(), strerror(errno));
          }
        } else {
          // Copy the file (recursively)
          cp_r(s, full_path);
        }
        // Change the permissions to match the original file
        struct stat file_stats = bee.file_stats();
        mode_t mode = file_stats.st_mode;
  			
  			if (chown(full_path.c_str(), user, group) != 0) {
  			  fprintf(stderr, "Could not change owner of '%s' to %i\n", full_path.c_str(), user);
  			}
  			
        if (chmod(full_path.c_str(), mode) != 0) {
          fprintf(stderr, "Could not change permissions to '%s' %o\n", full_path.c_str(), mode);
        }
        // Add it to the already_copied set and move on
        already_copied.insert(s);
      }
    }
    
    // Copy the executables and make them executable
    std::string bin_path = path + '/' + res_bin;
    cp_r(res_bin.c_str(), bin_path.c_str());
    
    if (chown(bin_path.c_str(), user, group) != 0) {
		  fprintf(stderr, "Could not change owner of '%s' to %i\n", bin_path.c_str(), user);
		}
		
    if (chmod(bin_path.c_str(), S_IREAD|S_IEXEC|S_IXGRP|S_IRGRP|S_IWRITE)) {
      fprintf(stderr, "Could not change permissions to '%s' make it executable\n", bin_path.c_str());
    }
		
  }
  return true;
}