void copy_dir_to_dir(char* from, char* to){ DIR *dir; if((dir=opendir(from))==NULL) show_errno(); struct dirent *dp; char next_from[FNAME] = {'\0'}; char next_to[FNAME] = {'\0'}; struct stat s; for(dp=readdir(dir);dp!=NULL;dp=readdir(dir)){ sprintf(next_from,"%s/%s",from,dp->d_name); if(dp->d_type==DT_REG){ copy_file_to_dir(next_from,to); } if(dp->d_type==DT_DIR){ if(strncmp(dp->d_name,"..",2)==0 || strncmp(dp->d_name,".",1)==0) continue; sprintf(next_to,"%s/%s",to,dp->d_name); if(stat(next_from,&s) == 0){ mkdir(next_to,s.st_mode); }else{ show_errno(); } copy_dir_to_dir(next_from,next_to); } } }
void copy_file_to_file(char* from, char* to){ char* to_archive[PNAME] = {'\0'}; if(g_argoption.compbackup){ remove_top_dir(to,to_archive); archive_add(from,to_archive); } struct stat s; char buf[SIZE]; int fd_from, fd_to, size; if( is_file_exist(to) ){ printf("file exists. %s %s\n",from,to); exit(-1); }else{ if( stat(from,&s) == -1 ) show_errno(); if(g_upperbackupsize < s.st_size) return; if((fd_to=open(to,O_CREAT|O_WRONLY,s.st_mode)) != -1){ if((fd_from=open(from,O_RDONLY)) != -1){ while(1){ size = read(fd_from,buf,SIZE); switch(size){ case -1: printf("copy_file_to_file : %s, %s\n",from,to); show_errno(); case 0: close(fd_to); close(fd_from); break; default: if(write(fd_to,buf,size) == -1){ printf("copy_file_to_file : %s, %s\n",from,to); show_errno(); } break; } if(size==0) break; } }else{ printf("copy_file_to_file : %s, %s\n",from,to); show_errno(); } }else{ printf("copy_file_to_file : %s, %s\n",from,to); show_errno(); } } }
static gboolean check_load_path(const gchar *pathname, gboolean file, int mode) { if (!utils_check_path(pathname, file, mode)) { show_errno(pathname); return FALSE; } return TRUE; }
int main(int argc, char* argv[]) { FILE* fp = fopen("/etc/passwd", "w"); if (!fp) { /* puts("Shucks, couldn't write /etc/passwd"); show_errno(); perror("main (write /etc/passwd)"); perror("NULL"); perror(""); */ MYERR(stderr,""); } show_errno(); testerror(); show_errno(); /* show_errno(); if(chdir("/does/not/exist")){ // myerror(stderr,__FUNCTION__,"chdir dne"); //__FUNCTION__ always resolves to current function we are in MYERR(stderr, "chdir dne"); } show_errno(); if (chdir("..")) { perror("main (chdir dne)"); } show_errno(); */ return 0; }
void copy_from_backup(char* date){ char to_path[PNAME]; get_backup_dir(date,to_path); struct stat s; if( stat(to_path,&s) == 0 ){ if( !S_ISDIR(s.st_mode) ) show_errno(); }else{ printf("no backups of the date %s.\n",date); exit(-1); } copy_dir_to_dir(to_path,"."); }
static gboolean check_dialog_path(GtkEntry *entry, gboolean file, int mode) { const gchar *pathname = gtk_entry_get_text(entry); if (utils_check_path(pathname, file, mode)) return TRUE; if (errno == ENOENT) { return dialogs_show_question(_("%s: %s.\n\nContinue?"), pathname, g_strerror(errno)); } show_errno(pathname); return FALSE; }
void copy_to_dir(int argc, char* argv[], char* backup){ int i; char from[FNAME] = {'\0'}; struct stat s; for(i=1;i<argc-1;i++){ sprintf(from,"%s",argv[i]); if( stat(argv[i],&s) == 0 ){ if( S_ISREG(s.st_mode) ) copy_file_to_dir(argv[i],backup); if( S_ISDIR(s.st_mode) ) copy_dir_to_dir(argv[i],backup); }else{ show_errno(); } } }
void archive_add(char* from, char* to){ struct archive_entry *entry; char buff[8192]; int len; int fd; struct stat s; if(stat(from, &s)==-1) show_errno(); entry = archive_entry_new(); // Note 2 archive_entry_set_pathname(entry, to); archive_entry_copy_stat(entry,&s); archive_write_header(g_archive, entry); fd = open(from, O_RDONLY); len = read(fd, buff, sizeof(buff)); while ( len > 0 ) { archive_write_data(g_archive, buff, len); len = read(fd, buff, sizeof(buff)); } close(fd); archive_entry_free(entry); }