// The failure conditions here are dealt with by the rubble cleaning code. static int delete_backup(struct sdirs *sdirs, const char *cname, struct bu *bu, const char *manual_delete) { logp("deleting %s backup %" PRId64 "\n", cname, bu->bno); if(sdirs->global_sparse) { const char *candidate_str=bu->path+strlen(sdirs->base)+1; if(remove_from_global_sparse( sdirs->global_sparse, candidate_str)) return -1; } if(!bu->next && !bu->prev) { // The current, and only, backup. if(do_rename_w(bu->path, sdirs->deleteme, cname, bu)) return -1; // If interrupted here, there will be a dangling 'current' // symlink. if(unlink(sdirs->current)) { logp("unlink %s: %s\n", sdirs->current, strerror(errno)); return -1; } return recursive_delete_w(sdirs, bu, manual_delete); } if(!bu->next && bu->prev) { // The current backup. There are other backups left. // Need to point the symlink at the previous backup. const char *target=NULL; target=bu->prev->basename; unlink(sdirs->currenttmp); if(do_symlink(target, sdirs->currenttmp)) return -1; // If interrupted here, there is a currenttmp and a current // symlink, and they both point to valid directories. if(do_rename_w(bu->path, sdirs->deleteme, cname, bu)) return -1; // If interrupted here, there is a currenttmp and a current // symlink, and the current link is dangling. if(do_rename(sdirs->currenttmp, sdirs->current)) return -1; // If interrupted here, moving the symlink could have failed // after current was deleted but before currenttmp was renamed. if(recursive_delete_w(sdirs, bu, manual_delete)) return -1; return 0; } // It is not the current backup. if(do_rename_w(bu->path, sdirs->deleteme, cname, bu) || recursive_delete_w(sdirs, bu, manual_delete)) return -1; return 0; }
static void expand_fs(char *path, struct cramfs_inode *inode) { if (S_ISDIR(inode->mode)) do_directory(path, inode); else if (S_ISREG(inode->mode)) do_file(path, inode); else if (S_ISLNK(inode->mode)) do_symlink(path, inode); else do_special_inode(path, inode); }
int main( int argc, char** argv ) { int error; argv0 = argv[ 0 ]; if ( ( argc == 2 ) && ( ( strcmp( argv[ 1 ], "-h" ) == 0 ) || ( strcmp( argv[ 1 ], "--help" ) == 0 ) ) ) { print_usage( EXIT_SUCCESS ); } if ( argc != 3 ) { print_usage( EXIT_FAILURE ); } error = do_symlink( argv[ 2 ], argv[ 1 ] ); if ( error < 0 ) { return EXIT_FAILURE; } return EXIT_SUCCESS; }
static void expand_fs(int pathlen, char *path, struct cramfs_inode *inode) { if (S_ISDIR(inode->mode)) { int count = inode->size; unsigned long offset = inode->offset << 2; char *newpath = malloc(pathlen + 256); if (count > 0 && offset < start_inode) { start_inode = offset; } /* XXX - need to check end_inode for empty case? */ memcpy(newpath, path, pathlen); newpath[pathlen] = '/'; pathlen++; if (opt_verbose) { print_node('d', inode, path); } if (opt_extract) { mkdir(path, inode->mode); change_file_status(path, inode); } while (count > 0) { struct cramfs_inode *child = iget(offset); int size; int newlen = child->namelen << 2; size = sizeof(struct cramfs_inode) + newlen; count -= size; offset += sizeof(struct cramfs_inode); memcpy(newpath + pathlen, romfs_read(offset), newlen); newpath[pathlen + newlen] = 0; if ((pathlen + newlen) - strlen(newpath) > 3) { fprintf(stderr, "%s: invalid cramfs--bad path length\n", filename); exit(4); } expand_fs(strlen(newpath), newpath, child); offset += newlen; if (offset > end_inode) { end_inode = offset; } } return; } if (S_ISREG(inode->mode)) { int fd = 0; unsigned long offset = inode->offset << 2; if (offset > 0 && offset < start_data) { start_data = offset; } if (opt_verbose) { print_node('f', inode, path); } if (opt_extract) { fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, inode->mode); } if (inode->size) { do_uncompress(fd, offset, inode->size); } if (opt_extract) { close(fd); change_file_status(path, inode); } return; } if (S_ISLNK(inode->mode)) { unsigned long offset = inode->offset << 2; if (offset < start_data) { start_data = offset; } do_symlink(path, inode); return; } else { do_special_inode(path, inode); return; } }
int replay(const char *log_data) { uint64_t fv,lv; //fv = filesystem's version lv = log's version uint32_t ts; uint8_t status; char buff[10000]; char *ptr; char* errormsgs[]={ ERROR_STRINGS }; char *test_ptr; sprintf(buff,"%s",log_data); ptr = buff; //for test test_ptr = buff; fv = shadow_fs_getversion(); GETU64(lv,ptr); if(lv < fv) { MFSLOG(LOG_ERR,"the changelog's verison %lu is smaller than filesystem's version %lu",lv,fv); //more complicated method to ensure consistency } else { status = ERROR_MISMATCH; EAT(ptr,lv,':'); EAT(ptr,lv,' '); GETU32(ts,ptr); EAT(ptr,lv,'|'); switch (*ptr) { case 'A': if (strncmp(ptr,"ACCESS",6)==0) { status = do_access(lv,ts,ptr+6); } else if (strncmp(ptr,"ATTR",4)==0) { status = do_attr(lv,ts,ptr+4); } else if (strncmp(ptr,"APPEND",6)==0) { status = do_append(lv,ts,ptr+6); } else if (strncmp(ptr,"AQUIRE",6)==0) { status = do_aquire(lv,ts,ptr+6); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'C': if (strncmp(ptr,"CREATE",6)==0) { status = do_create(lv,ts,ptr+6); } else if (strncmp(ptr,"CUSTOMER",8)==0) { // deprecated status = do_session(lv,ts,ptr+8); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'E': if (strncmp(ptr,"EMPTYTRASH",10)==0) { status = do_emptytrash(lv,ts,ptr+10); } else if (strncmp(ptr,"EMPTYRESERVED",13)==0) { status = do_emptyreserved(lv,ts,ptr+13); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'F': if (strncmp(ptr,"FREEINODES",10)==0) { status = do_freeinodes(lv,ts,ptr+10); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'I': if (strncmp(ptr,"INCVERSION",10)==0) { status = do_incversion(lv,ts,ptr+10); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'L': if (strncmp(ptr,"LENGTH",6)==0) { status = do_length(lv,ts,ptr+6); } else if (strncmp(ptr,"LINK",4)==0) { status = do_link(lv,ts,ptr+4); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'M': if (strncmp(ptr,"MOVE",4)==0) { status = do_move(lv,ts,ptr+4); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'P': if (strncmp(ptr,"PURGE",5)==0) { status = do_purge(lv,ts,ptr+5); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'R': if (strncmp(ptr,"RELEASE",7)==0) { status = do_release(lv,ts,ptr+7); } else if (strncmp(ptr,"REPAIR",6)==0) { status = do_repair(lv,ts,ptr+6); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'S': if (strncmp(ptr,"SETEATTR",8)==0) { status = do_seteattr(lv,ts,ptr+8); } else if (strncmp(ptr,"SETGOAL",7)==0) { status = do_setgoal(lv,ts,ptr+7); } else if (strncmp(ptr,"SETPATH",7)==0) { status = do_setpath(lv,ts,ptr+7); } else if (strncmp(ptr,"SETTRASHTIME",12)==0) { status = do_settrashtime(lv,ts,ptr+12); } else if (strncmp(ptr,"SNAPSHOT",8)==0) { status = do_snapshot(lv,ts,ptr+8); } else if (strncmp(ptr,"SYMLINK",7)==0) { status = do_symlink(lv,ts,ptr+7); } else if (strncmp(ptr,"SESSION",7)==0) { status = do_session(lv,ts,ptr+7); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'T': if (strncmp(ptr,"TRUNC",5)==0) { status = do_trunc(lv,ts,ptr+5); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'U': if (strncmp(ptr,"UNLINK",6)==0) { status = do_unlink(lv,ts,ptr+6); } else if (strncmp(ptr,"UNDEL",5)==0) { status = do_undel(lv,ts,ptr+5); } else if (strncmp(ptr,"UNLOCK",6)==0) { status = do_unlock(lv,ts,ptr+6); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'W': if (strncmp(ptr,"WRITE",5)==0) { status = do_write(lv,ts,ptr+5); } else { MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; default: MFSLOG(LOG_ERR,"%"PRIu64": unknown entry '%s'",lv,ptr); } /** * if master is down, slave switch, we may missed some metadata * return 0 to let the process continue, otherwise, we will reply the * log forever. * * Dongyang, Zhang */ if (status!=STATUS_OK) { MFSLOG(LOG_ERR,"%"PRIu64": error: %"PRIu8" (%s),the log is (%s)",lv,status,errormsgs[status],test_ptr); // syslog(LOG_ERR,"%"PRIu64": error: %"PRIu8" (%s)",lv,status,errormsgs[status]); return 1; } fv = shadow_fs_getversion(); if (lv+1!=fv) { MFSLOG(LOG_ERR,"%"PRIu64": version mismatch fsversion:%"PRIu64"",lv,fv); return 1; } } return 0; }
int restore(void) { FILE *fd; char buff[10000]; char *ptr; uint64_t v,lv; uint32_t ts; uint8_t status; uint32_t dplen; char *datapath = NULL; char *logpath = NULL; char* errormsgs[]={ ERROR_STRINGS }; v = shadow_fs_getversion(); lv = 0; MFSLOG(LOG_NOTICE,"meta data version: %"PRIu64"",v); datapath = strdup(DATA_PATH); dplen = strlen(datapath); logpath = malloc(dplen+sizeof("/changelog.0.mfs")); memcpy(logpath,datapath,dplen); memcpy(logpath+dplen,"/changelog.0.mfs",sizeof("/changelog.0.mfs")); fd = fopen(logpath,"r"); if (fd==NULL) { MFSLOG(LOG_NOTICE,"can't open changemeta file: %s",logpath); return 1; } while (fgets(buff,10000,fd)) { ptr = buff; GETU64(lv,ptr); if (lv<v) { // skip } else { status = ERROR_MISMATCH; EAT(ptr,lv,':'); EAT(ptr,lv,' '); GETU32(ts,ptr); EAT(ptr,lv,'|'); switch (*ptr) { case 'A': if (strncmp(ptr,"ACCESS",6)==0) { status = do_access(lv,ts,ptr+6); } else if (strncmp(ptr,"ATTR",4)==0) { status = do_attr(lv,ts,ptr+4); } else if (strncmp(ptr,"APPEND",6)==0) { status = do_append(lv,ts,ptr+6); } else if (strncmp(ptr,"AQUIRE",6)==0) { status = do_aquire(lv,ts,ptr+6); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'C': if (strncmp(ptr,"CREATE",6)==0) { status = do_create(lv,ts,ptr+6); } else if (strncmp(ptr,"CUSTOMER",8)==0) { // deprecated status = do_session(lv,ts,ptr+8); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'E': if (strncmp(ptr,"EMPTYTRASH",10)==0) { status = do_emptytrash(lv,ts,ptr+10); } else if (strncmp(ptr,"EMPTYRESERVED",13)==0) { status = do_emptyreserved(lv,ts,ptr+13); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'F': if (strncmp(ptr,"FREEINODES",10)==0) { status = do_freeinodes(lv,ts,ptr+10); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'I': if (strncmp(ptr,"INCVERSION",10)==0) { status = do_incversion(lv,ts,ptr+10); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'L': if (strncmp(ptr,"LENGTH",6)==0) { status = do_length(lv,ts,ptr+6); } else if (strncmp(ptr,"LINK",4)==0) { status = do_link(lv,ts,ptr+4); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'M': if (strncmp(ptr,"MOVE",4)==0) { status = do_move(lv,ts,ptr+4); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'P': if (strncmp(ptr,"PURGE",5)==0) { status = do_purge(lv,ts,ptr+5); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'R': if (strncmp(ptr,"RELEASE",7)==0) { status = do_release(lv,ts,ptr+7); } else if (strncmp(ptr,"REPAIR",6)==0) { status = do_repair(lv,ts,ptr+6); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'S': if (strncmp(ptr,"SETEATTR",8)==0) { status = do_seteattr(lv,ts,ptr+8); } else if (strncmp(ptr,"SETGOAL",7)==0) { status = do_setgoal(lv,ts,ptr+7); } else if (strncmp(ptr,"SETPATH",7)==0) { status = do_setpath(lv,ts,ptr+7); } else if (strncmp(ptr,"SETTRASHTIME",12)==0) { status = do_settrashtime(lv,ts,ptr+12); } else if (strncmp(ptr,"SNAPSHOT",8)==0) { status = do_snapshot(lv,ts,ptr+8); } else if (strncmp(ptr,"SYMLINK",7)==0) { status = do_symlink(lv,ts,ptr+7); } else if (strncmp(ptr,"SESSION",7)==0) { status = do_session(lv,ts,ptr+7); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'T': if (strncmp(ptr,"TRUNC",5)==0) { status = do_trunc(lv,ts,ptr+5); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'U': if (strncmp(ptr,"UNLINK",6)==0) { status = do_unlink(lv,ts,ptr+6); } else if (strncmp(ptr,"UNDEL",5)==0) { status = do_undel(lv,ts,ptr+5); } else if (strncmp(ptr,"UNLOCK",6)==0) { status = do_unlock(lv,ts,ptr+6); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; case 'W': if (strncmp(ptr,"WRITE",5)==0) { status = do_write(lv,ts,ptr+5); } else { MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } break; default: MFSLOG(LOG_NOTICE,"%"PRIu64": unknown entry '%s'",lv,ptr); } /** * let the restore continue if we missed some meta data, otherwise the * * Dongyang Zhang */ if (status!=STATUS_OK) { MFSLOG(LOG_ERR,"%"PRIu64": error: %"PRIu8" (%s)",lv,status,errormsgs[status]); return 1; } v = shadow_fs_getversion(); if (lv+1!=v) { MFSLOG(LOG_ERR,"%"PRIu64": version mismatch",lv); return 1; } } } fclose(fd); MFSLOG(LOG_NOTICE,"version after applying changelog: %"PRIu64"",v); return 0; }
void do_file_entry(const u8* base, const char* dir, const char* path, const char* name, int namelen, const struct cramfs_inode* inode) { int dirlen=strlen(dir); int pathlen=strlen(path); char pname[dirlen+pathlen+namelen+3]; const char* basename; if (dirlen) { strncpy(pname, dir, dirlen); } if (pathlen) { if (dirlen) { pname[dirlen]='/'; ++dirlen; } strncpy(pname+dirlen, path, pathlen); } if (namelen) { if (pathlen+dirlen) { pname[dirlen+pathlen]='/'; ++pathlen; } strncpy(pname+dirlen+pathlen, name, namelen); } pname[pathlen+dirlen+namelen]=0; basename=namelen ? pname+dirlen+pathlen : "/"; // Create things here printmode(inode); printuidgid(inode); if (S_ISREG(inode->mode)) { do_file(base, inode->offset<<2, inode->size, pname, basename, inode->mode); } else if (S_ISDIR(inode->mode)) { do_directory(base, inode->offset<<2, inode->size, pname, basename, inode->mode); } else if (S_ISLNK(inode->mode)) { do_symlink(base, inode->offset<<2, inode->size, pname, basename, inode->mode); } else if (S_ISFIFO(inode->mode)) { do_fifo(base, inode->offset<<2, inode->size, pname, basename, inode->mode, inode->uid, inode->gid); } else if (S_ISSOCK(inode->mode)) { do_socket(base, inode->offset<<2, inode->size, pname, basename, inode->mode); } else if (S_ISCHR(inode->mode)) { do_chrdev(base, inode->offset<<2, inode->size, pname, basename, inode->mode, inode->uid, inode->gid); } else if (S_ISBLK(inode->mode)) { do_blkdev(base, inode->offset<<2, inode->size, pname, basename, inode->mode, inode->uid, inode->gid); } else { do_unknown(base, inode->offset<<2, inode->size, pname, basename, inode->mode); } if (geteuid() == 0) { if (lchown(pname, inode->uid, inode->gid) == -1) perror("cannot change owner or group"); } else if(opt_idsfile && path && path[0]) { char dfp[1024]; char *p; FILE *f; strcpy(dfp,pname); p = strrchr(dfp,'/'); if (!p) { fprintf(stderr,"could not find path in '%s'\n",pname); return; } strcpy(p+1,opt_idsfile); f = fopen(dfp,"at"); if (!f) { perror(dfp); return; } fprintf(f,"%s,%u,%u,%08x\n",basename,inode->uid,inode->gid,inode->mode); fclose(f); } if (geteuid() == 0 || !opt_idsfile) { if (inode->mode & (S_ISGID|S_ISUID|S_ISVTX)) { if (0 != chmod(pname, inode->mode)){ perror("chmod"); return; } } } printf("\n"); }
void addPath(const char *path, int squash_uids, int squash_perms) { size_t len; char *full_name = NULL, *lnk = NULL; struct dirent *file = NULL; DIR *direc = NULL; struct stat st; int overWrite; __u16 inodeType; direc = opendir(path); if ( !direc ) log_error("[Local fs opendir] Cannot open directory %s", path); while (( file = readdir(direc)) != NULL ) { if (( !strcmp(file->d_name, "." )) || ( !strcmp(file->d_name, ".." ))) continue; len = strlen(path) + strlen( file->d_name ) + 3; if ( full_name ) { free( full_name ); full_name = NULL; } full_name = (char*)malloc(len + 2); if ( !full_name ) log_error("[Local fs stat] Memory allocation error ( full_name --> %s/%s )", path, file->d_name); memset(full_name, 0, len + 2); snprintf(full_name, len, "%s/%s", path, file->d_name); lstat(full_name, &st); mode_t fmt = st.st_mode & S_IFMT; if ( squash_uids ) st.st_uid = st.st_gid = 0; if ( squash_perms ) st.st_mode &= ~( LINUX_S_IRWXG | LINUX_S_IRWXO ); overWrite = name_to_inode( file->d_name ); if ( st.st_nlink > 1 ) { if (( lnk = linklist_add(st.st_dev, st.st_ino, full_name + modPath_path_len))) { if ( overWrite ) { log_action(ACT_RM, file->d_name, NULL, 0, 0, 0, 0, 0, 0, overWrite); if ( !do_rm(file->d_name)) log_error("[Filesystem error] cannot rm %s/%s", log_cwd(), file->d_name); } log_action(ACT_HARDLINK, file->d_name, lnk, 0, 0, 0, 0, 0, 0, overWrite); if (!do_hardlink(&st, lnk, file->d_name)) log_error("[Filesystem error] cannot hardlink %s --> %s", file->d_name, lnk); continue; } } if ( overWrite ) inodeType = inode_mode( file->d_name ); if (( fmt == S_IFDIR ) && ( overWrite ) && ( !LINUX_S_ISDIR(inodeType))) log_error("[Remote fs mismatch] %s/%s exists but isn't a directory when it should be.", log_cwd(), file->d_name); else if (( fmt != S_IFDIR) && ( overWrite )) { if ( LINUX_S_ISDIR(inodeType)) log_error("[Remote fs mismatch] %s/%s exists but is a directory when it shouldn't be.", log_cwd(), file->d_name); if ((!LINUX_S_ISREG(inodeType)) && (!LINUX_S_ISLNK(inodeType)) && (!LINUX_S_ISBLK(inodeType)) && (!LINUX_S_ISCHR(inodeType)) && (!LINUX_S_ISFIFO(inodeType)) && (!LINUX_S_ISSOCK(inodeType))) log_error("[Remote fs mismatch] Existing file %s/%s has unknown/unsupported type [0x%x].", log_cwd(), file->d_name); } switch ( fmt ) { case S_IFDIR: // Directory log_action(ACT_MKDIR, file->d_name, NULL, 0, 0, 0, 0, 0, 0, overWrite); if ( !overWrite ) if ( !do_mkdir( &st, file->d_name )) log_error("[Filesystem error] cannot mkdir %s/%s", log_cwd(), file->d_name); log_action(ACT_CHMODE, file->d_name, NULL, st.st_mode, 0, 0, 0, 0, 0, overWrite); if ( !do_chmode(file->d_name, st.st_mode)) log_error("[Filesystem error] Failed to chmode 0x%x for directory %s/%s", st.st_mode, log_cwd(), file->d_name); log_action(ACT_CHOWN, file->d_name, NULL, 0, st.st_uid, st.st_gid, 0, 0, 0, 0); if ( !do_chown(file->d_name, st.st_uid, st.st_gid)) log_error("[Filesystem error] Failed to chown %ld, %ld for directory %s/%s", st.st_uid, st.st_gid, log_cwd(), file->d_name); log_action(ACT_CHDIR, file->d_name, NULL, 0, 0, 0, 0, 0, 0, 0); if ( !do_chdir( file->d_name )) log_error("[Filesystem error] cannot chdir to newly created %s/%s", log_cwd(), file->d_name); addPath(full_name, squash_uids, squash_perms); log_action(ACT_CHDIR, "..", NULL, 0, 0, 0, 0, 0, 0, 0); if ( !do_chdir("..")) log_error("[Filesystem error] cannot chdir to parent directory"); break; case S_IFREG: // Regular file if ( overWrite ) { log_action(ACT_RM, file->d_name, NULL, 0, 0, 0, 0, 0, 0, overWrite); if ( !do_rm(file->d_name)) log_error("[Filesystem error] cannot rm %s/%s", log_cwd(), file->d_name); } log_action(ACT_WRITE, file->d_name, NULL, 0, 0, 0, 0, 0, 0, overWrite); if ( !do_write(full_name, file->d_name)) log_error("[Filesystem error] cannot write %s/%s", log_cwd(), file->d_name); break; case S_IFLNK: // Symbolic link lnk = (char*)malloc(MAX_PATHSIZE + 2); if ( !lnk ) log_error("[symlink] Memory allocation error (lnk)"); int len = readlink(full_name, lnk, MAX_PATHSIZE); if ( len == -1 ) { free(lnk); log_error("[Local filesystem error] Cannot read destination for link %s/%s", log_cwd(), file->d_name); } else lnk[len] = '\0'; if ( overWrite ) { log_action(ACT_RM, file->d_name, NULL, 0, 0, 0, 0, 0, 0, overWrite); if ( !do_rm(file->d_name)) log_error("[Filesystem error] cannot rm %s/%s", log_cwd(), file->d_name); } log_action(ACT_SYMLINK, file->d_name, lnk, 0, 0, 0, 0, 0, 0, overWrite); if ( !do_symlink(&st, lnk, file->d_name)) log_error("[Filesystem error] cannot symlink %s/%s --> %s", log_cwd(), file->d_name, lnk); free(lnk); break; case S_IFBLK: // Block device node case S_IFCHR: // Character device node case S_IFSOCK: // socket case S_IFIFO: // fifo if ( overWrite ) { log_action(ACT_RM, file->d_name, NULL, 0, 0, 0, 0, 0, 0, overWrite); if ( !do_rm(file->d_name)) log_error("[Filesystem error] cannot rm %s/%s", log_cwd(), file->d_name); } char nodetype = ( fmt == S_IFBLK ? 'b' : ( fmt == S_IFCHR ? 'c' : ( fmt == S_IFSOCK ? 's' : 'p' ))); unsigned long major = 0, minor = 0; if (( nodetype == 'b' ) || ( nodetype == 'c' )) { major = (long)major(st.st_rdev); minor = (long)minor(st.st_rdev); } log_action(ACT_MKNOD, file->d_name, NULL, 0, 0, 0, nodetype, major, minor, overWrite); if ( !do_mknod(file->d_name, nodetype, major, minor)) log_error("[Filesystem error] cannot mknod %c %ld,%ld %s/%s", log_cwd(), nodetype, major, minor, log_cwd(), file->d_name); break; } if ( fmt != S_IFDIR ) { // Not dir ? log_action(ACT_CHMODE, file->d_name, NULL, st.st_mode, 0, 0, 0, 0, 0, overWrite); if ( !do_chmode(file->d_name, st.st_mode)) log_error("[Filesystem error] Failed to chmode 0x%x for file %s/%s", st.st_mode, log_cwd(), file->d_name); log_action(ACT_CHOWN, file->d_name, NULL, 0, st.st_uid, st.st_gid, 0, 0, 0, 0); if ( !do_chown(file->d_name, st.st_uid, st.st_gid)) log_error("[Filesystem error] Failed to chown %ld, %ld for file %s/%s", st.st_uid, st.st_gid, log_cwd(), file->d_name); } if ( full_name ) { free( full_name ); full_name = NULL; } } closedir(direc); }
/* If we have a --backup-dir, then we get here from make_backup(). * We will move the file to be deleted into a parallel directory tree. */ static int keep_backup(const char *fname) { STRUCT_STAT st; struct file_struct *file; char *buf; int kept = 0; int ret_code; /* return if no file to keep */ if (do_lstat(fname, &st) < 0) return 1; if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) return 1; /* the file could have disappeared */ if (!(buf = get_backup_name(fname))) { unmake_file(file); return 0; } /* Check to see if this is a device file, or link */ if ((am_root && preserve_devices && IS_DEVICE(file->mode)) || (preserve_specials && IS_SPECIAL(file->mode))) { uint32 *devp = F_RDEV_P(file); dev_t rdev = MAKEDEV(DEV_MAJOR(devp), DEV_MINOR(devp)); do_unlink(buf); if (do_mknod(buf, file->mode, rdev) < 0 && (errno != ENOENT || make_bak_dir(buf) < 0 || do_mknod(buf, file->mode, rdev) < 0)) { rsyserr(FERROR, errno, "mknod %s failed", full_fname(buf)); } else if (verbose > 2) { rprintf(FINFO, "make_backup: DEVICE %s successful.\n", fname); } kept = 1; do_unlink(fname); } if (!kept && S_ISDIR(file->mode)) { /* make an empty directory */ if (do_mkdir(buf, file->mode) < 0 && (errno != ENOENT || make_bak_dir(buf) < 0 || do_mkdir(buf, file->mode) < 0)) { rsyserr(FINFO, errno, "mkdir %s failed", full_fname(buf)); } ret_code = do_rmdir(fname); if (verbose > 2) { rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", full_fname(fname), ret_code); } kept = 1; } #ifdef SUPPORT_LINKS if (!kept && preserve_links && S_ISLNK(file->mode)) { const char *sl = F_SYMLINK(file); if (safe_symlinks && unsafe_symlink(sl, buf)) { if (verbose) { rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", full_fname(buf), sl); } kept = 1; } else { do_unlink(buf); if (do_symlink(sl, buf) < 0 && (errno != ENOENT || make_bak_dir(buf) < 0 || do_symlink(sl, buf) < 0)) { rsyserr(FERROR, errno, "link %s -> \"%s\"", full_fname(buf), sl); } do_unlink(fname); kept = 1; } } #endif if (!kept && !S_ISREG(file->mode)) { rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname); unmake_file(file); return 1; } /* move to keep tree if a file */ if (!kept) { if (robust_move(fname, buf) != 0) { rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", full_fname(fname), buf); } else if (st.st_nlink > 1) { /* If someone has hard-linked the file into the backup * dir, rename() might return success but do nothing! */ robust_unlink(fname); /* Just in case... */ } } set_file_attrs(buf, file, NULL, 0); unmake_file(file); if (verbose > 1) { rprintf(FINFO, "backed up %s to %s\n", fname, buf); } return 1; }
/** * Acts on file number @p i from @p flist, whose name is @p fname. * * First fixes up permissions, then generates checksums for the file. * * @note This comment was added later by mbp who was trying to work it * out. It might be wrong. **/ int recv_generator(char *fname, struct file_list *flist, int i, int f_out) { int fd; STRUCT_STAT st; struct map_struct *buf; struct sum_struct *s; int statret; struct file_struct *file = flist->files[i]; struct timeval tv_start; char *fnamecmp; char fnamecmpbuf[MAXPATHLEN]; extern char *compare_dest; extern int list_only; extern int preserve_perms; extern int only_existing; extern int orig_umask; if (list_only) return 0; if (verbose > 2) rprintf(FINFO,"recv_generator(%s,%d)\n",fname,i); statret = link_stat(fname,&st); if (only_existing && statret == -1 && errno == ENOENT) { /* we only want to update existing files */ if (verbose > 1) rprintf(FINFO, "not creating new file \"%s\"\n",fname); return 0; } if (statret == 0 && !preserve_perms && (S_ISDIR(st.st_mode) == S_ISDIR(file->mode))) { /* if the file exists already and we aren't perserving presmissions then act as though the remote end sent us the file permissions we already have */ file->mode = (file->mode & _S_IFMT) | (st.st_mode & ~_S_IFMT); } if (S_ISDIR(file->mode)) { /* The file to be received is a directory, so we need * to prepare appropriately. If there is already a * file of that name and it is *not* a directory, then * we need to delete it. If it doesn't exist, then * recursively create it. */ if (dry_run) return 0; /* XXXX -- might cause inaccuracies?? -- mbp */ if (statret == 0 && !S_ISDIR(st.st_mode)) { if (robust_unlink(fname) != 0) { rprintf(FERROR, RSYNC_NAME ": recv_generator: unlink \"%s\" to make room for directory: %s\n", fname,strerror(errno)); return 0; } statret = -1; } if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) { if (!(relative_paths && errno==ENOENT && create_directory_path(fname, orig_umask)==0 && do_mkdir(fname,file->mode)==0)) { rprintf(FERROR, RSYNC_NAME ": recv_generator: mkdir \"%s\": %s (2)\n", fname,strerror(errno)); } } /* f_out is set to -1 when doing final directory permission and modification time repair */ if (set_perms(fname,file,NULL,0) && verbose && (f_out != -1)) rprintf(FINFO,"%s/\n",fname); return 0; } if (preserve_links && S_ISLNK(file->mode)) { #if SUPPORT_LINKS char lnk[MAXPATHLEN]; int l; extern int safe_symlinks; if (safe_symlinks && unsafe_symlink(file->link, fname)) { if (verbose) { rprintf(FINFO,"ignoring unsafe symlink \"%s\" -> \"%s\"\n", fname,file->link); } return 0; } if (statret == 0) { l = readlink(fname,lnk,MAXPATHLEN-1); if (l > 0) { lnk[l] = 0; /* A link already pointing to the * right place -- no further action * required. */ if (strcmp(lnk,file->link) == 0) { set_perms(fname,file,&st,1); return 0; } } /* Not a symlink, so delete whatever's * already there and put a new symlink * in place. */ delete_file(fname); } if (do_symlink(file->link,fname) != 0) { rprintf(FERROR,RSYNC_NAME": symlink \"%s\" -> \"%s\": %s\n", fname,file->link,strerror(errno)); } else { set_perms(fname,file,NULL,0); if (verbose) { rprintf(FINFO,"%s -> %s\n", fname,file->link); } } #endif return 0; } #ifdef HAVE_MKNOD if (am_root && preserve_devices && IS_DEVICE(file->mode)) { if (statret != 0 || st.st_mode != file->mode || st.st_rdev != file->rdev) { delete_file(fname); if (verbose > 2) rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", fname,(int)file->mode,(int)file->rdev); if (do_mknod(fname,file->mode,file->rdev) != 0) { rprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno)); } else { set_perms(fname,file,NULL,0); if (verbose) rprintf(FINFO,"%s\n",fname); } } else { set_perms(fname,file,&st,1); } return 0; } #endif if (preserve_hard_links && check_hard_link(file)) { if (verbose > 1) rprintf(FINFO, "recv_generator: \"%s\" is a hard link\n",f_name(file)); return 0; } if (!S_ISREG(file->mode)) { rprintf(FINFO, "skipping non-regular file \"%s\"\n",fname); return 0; } fnamecmp = fname; if ((statret == -1) && (compare_dest != NULL)) { /* try the file at compare_dest instead */ int saveerrno = errno; snprintf(fnamecmpbuf,MAXPATHLEN,"%s/%s",compare_dest,fname); statret = link_stat(fnamecmpbuf,&st); if (!S_ISREG(st.st_mode)) statret = -1; if (statret == -1) errno = saveerrno; else fnamecmp = fnamecmpbuf; } if (statret == -1) { if (errno == ENOENT) { write_int(f_out,i); if (!dry_run) { if(do_stats){ gettimeofday(&tv_start,0); rprintf(FINFO, "File start: %s time: %d %d\n",fnamecmp, tv_start.tv_sec, tv_start.tv_usec); } send_sums(NULL,f_out); return 1; } } else { if (verbose > 1) rprintf(FERROR, RSYNC_NAME ": recv_generator failed to open \"%s\": %s\n", fname, strerror(errno)); return 0; } } if (!S_ISREG(st.st_mode)) { if (delete_file(fname) != 0) { return 0; } if(do_stats){ gettimeofday(&tv_start,0); rprintf(FINFO, "File start: %s time: %d %d\n",fnamecmp, tv_start.tv_sec, tv_start.tv_usec); } /* now pretend the file didn't exist */ write_int(f_out,i); if (!dry_run) send_sums(NULL,f_out); return 1; } if (opt_ignore_existing && fnamecmp == fname) { if (verbose > 1) rprintf(FINFO,"%s exists\n",fname); return 0; } if (update_only && cmp_modtime(st.st_mtime,file->modtime)>0 && fnamecmp == fname) { if (verbose > 1) rprintf(FINFO,"%s is newer\n",fname); return 0; } if (skip_file(fname, file, &st)) { if (fnamecmp == fname) set_perms(fname,file,&st,1); return 0; } if (dry_run) { if(do_stats){ gettimeofday(&tv_start,0); rprintf(FINFO, "File start: %s time: %d %d\n",fnamecmp, tv_start.tv_sec, tv_start.tv_usec); } write_int(f_out,i); return 1; } if (disable_deltas_p()) { if(do_stats){ gettimeofday(&tv_start,0); rprintf(FINFO, "File start: %s time: %d %d\n",fnamecmp, tv_start.tv_sec, tv_start.tv_usec); } write_int(f_out,i); send_sums(NULL,f_out); return 1; } if(do_stats){ gettimeofday(&tv_start,0); rprintf(FINFO, "File start: %s time: %d %d\n",fnamecmp, tv_start.tv_sec, tv_start.tv_usec); } /* open the file */ fd = do_open(fnamecmp, O_RDONLY, 0); if (fd == -1) { rprintf(FERROR,RSYNC_NAME": failed to open \"%s\", continuing : %s\n",fnamecmp,strerror(errno)); /* pretend the file didn't exist */ write_int(f_out,i); send_sums(NULL,f_out); return 1; } if (st.st_size > 0) { buf = map_file(fd,st.st_size); } else { buf = NULL; } if (verbose > 3) rprintf(FINFO,"gen mapped %s of size %.0f\n",fnamecmp,(double)st.st_size); if(do_stats){ timing(TIMING_START); } s = generate_sums(buf,st.st_size,adapt_block_size(file, block_size)); if(do_stats){ rprintf(FINFO, "Generator: %s %s\n", fnamecmp,timing(TIMING_END)); } if (verbose > 2) rprintf(FINFO,"sending sums for %d\n",i); write_int(f_out,i); if(do_stats){ timing(TIMING_START); } send_sums(s,f_out); if(do_stats){ rprintf(FINFO, "Send sums: %s %s\n", fnamecmp,timing(TIMING_END)); } close(fd); if (buf) unmap_file(buf); free_sums(s); return 1; }
/* if we have a backup_dir, then we get here from make_backup(). We will move the file to be deleted into a parallel directory tree */ static int keep_backup(char *fname) { static int initialised; char keep_name [MAXPATHLEN]; STRUCT_STAT st; struct file_struct *file; int kept=0; int ret_code; if (!initialised) { if (backup_dir[strlen(backup_dir) - 1] == '/') backup_dir[strlen(backup_dir) - 1] = 0; if (verbose > 0) rprintf (FINFO, "backup_dir is %s\n", backup_dir); initialised = 1; } /* return if no file to keep */ #if SUPPORT_LINKS if (do_lstat (fname, &st)) return 1; #else if (do_stat (fname, &st)) return 1; #endif file = make_file(-1, fname, NULL, 1); /* the file could have disappeared */ if (!file) return 1; /* make a complete pathname for backup file */ if (strlen(backup_dir) + strlen(fname) + (suffix_specified ? strlen(backup_suffix) : 0) > (MAXPATHLEN - 1)) { rprintf (FERROR, "keep_backup filename too long\n"); return 0; } if (suffix_specified) { snprintf(keep_name, sizeof (keep_name), "%s/%s%s", backup_dir, fname, backup_suffix); } else { snprintf(keep_name, sizeof (keep_name), "%s/%s", backup_dir, fname); } #ifdef HAVE_MKNOD /* Check to see if this is a device file, or link */ if(IS_DEVICE(file->mode)) { if(am_root && preserve_devices) { make_bak_dir(fname,backup_dir); if(do_mknod(keep_name,file->mode,file->rdev)!=0) { rprintf(FERROR,"mknod %s : %s\n",keep_name,strerror(errno)); } else { if(verbose>2) rprintf(FINFO,"make_backup : DEVICE %s successful.\n",fname); }; }; kept=1; do_unlink(fname); }; #endif if(!kept && S_ISDIR(file->mode)) { /* make an empty directory */ make_bak_dir(fname,backup_dir); do_mkdir(keep_name,file->mode); ret_code=do_rmdir(fname); if(verbose>2) rprintf(FINFO,"make_backup : RMDIR %s returns %i\n",fname,ret_code); kept=1; }; #if SUPPORT_LINKS if(!kept && preserve_links && S_ISLNK(file->mode)) { extern int safe_symlinks; if (safe_symlinks && unsafe_symlink(file->link, keep_name)) { if (verbose) { rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n", keep_name,file->link); } kept=1; } make_bak_dir(fname,backup_dir); if(do_symlink(file->link,keep_name) != 0) { rprintf(FERROR,"link %s -> %s : %s\n",keep_name,file->link,strerror(errno)); }; do_unlink(fname); kept=1; }; #endif if(!kept && preserve_hard_links && check_hard_link(file)) { if(verbose > 1) rprintf(FINFO,"%s is a hard link\n",f_name(file)); }; if(!kept && !S_ISREG(file->mode)) { rprintf(FINFO,"make_bak: skipping non-regular file %s\n",fname); } /* move to keep tree if a file */ if(!kept) { if (!robust_move (fname, keep_name)) rprintf(FERROR, "keep_backup failed %s -> %s : %s\n", fname, keep_name, strerror(errno)); }; set_perms (keep_name, file, NULL, 0); free_file (file); free (file); if (verbose > 1) rprintf (FINFO, "keep_backup %s -> %s\n", fname, keep_name); return 1; } /* keep_backup */
/* Hard-link, rename, or copy an item to the backup name. Returns 0 for * failure, 1 if item was moved, 2 if item was duplicated or hard linked * into backup area, or 3 if item doesn't exist or isn't a regular file. */ int make_backup(const char *fname, BOOL prefer_rename) { stat_x sx; struct file_struct *file; int save_preserve_xattrs; char *buf; int ret = 0; init_stat_x(&sx); /* Return success if no file to keep. */ if (x_lstat(fname, &sx.st, NULL) < 0) return 3; if (!(buf = get_backup_name(fname))) return 0; /* Try a hard-link or a rename first. Using rename is not atomic, but * is more efficient than forcing a copy for larger files when no hard- * linking is possible. */ if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0) goto success; if (errno == EEXIST || errno == EISDIR) { STRUCT_STAT bakst; if (do_lstat(buf, &bakst) == 0) { int flags = get_del_for_flag(bakst.st_mode) | DEL_FOR_BACKUP | DEL_RECURSE; if (delete_item(buf, bakst.st_mode, flags) != 0) return 0; } if ((ret = link_or_rename(fname, buf, prefer_rename, &sx.st)) != 0) goto success; } /* Fall back to making a copy. */ if (!(file = make_file(fname, NULL, &sx.st, 0, NO_FILTERS))) return 3; /* the file could have disappeared */ #ifdef SUPPORT_ACLS if (preserve_acls && !S_ISLNK(file->mode)) { get_acl(fname, &sx); cache_tmp_acl(file, &sx); free_acl(&sx); } #endif #ifdef SUPPORT_XATTRS if (preserve_xattrs) { get_xattr(fname, &sx); cache_tmp_xattr(file, &sx); free_xattr(&sx); } #endif /* Check to see if this is a device file, or link */ if ((am_root && preserve_devices && IS_DEVICE(file->mode)) || (preserve_specials && IS_SPECIAL(file->mode))) { if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) rsyserr(FERROR, errno, "mknod %s failed", full_fname(buf)); else if (DEBUG_GTE(BACKUP, 1)) rprintf(FINFO, "make_backup: DEVICE %s successful.\n", fname); ret = 2; } #ifdef SUPPORT_LINKS if (!ret && preserve_links && S_ISLNK(file->mode)) { const char *sl = F_SYMLINK(file); if (safe_symlinks && unsafe_symlink(sl, fname)) { if (INFO_GTE(SYMSAFE, 1)) { rprintf(FINFO, "not backing up unsafe symlink \"%s\" -> \"%s\"\n", fname, sl); } ret = 2; } else { if (do_symlink(sl, buf) < 0) rsyserr(FERROR, errno, "link %s -> \"%s\"", full_fname(buf), sl); else if (DEBUG_GTE(BACKUP, 1)) rprintf(FINFO, "make_backup: SYMLINK %s successful.\n", fname); ret = 2; } } #endif if (!ret && !S_ISREG(file->mode)) { rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname); unmake_file(file); #ifdef SUPPORT_ACLS uncache_tmp_acls(); #endif #ifdef SUPPORT_XATTRS uncache_tmp_xattrs(); #endif return 3; } /* Copy to backup tree if a file. */ if (!ret) { if (copy_file(fname, buf, -1, file->mode) < 0) { rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", full_fname(fname), buf); unmake_file(file); #ifdef SUPPORT_ACLS uncache_tmp_acls(); #endif #ifdef SUPPORT_XATTRS uncache_tmp_xattrs(); #endif return 0; } if (DEBUG_GTE(BACKUP, 1)) rprintf(FINFO, "make_backup: COPY %s successful.\n", fname); ret = 2; } save_preserve_xattrs = preserve_xattrs; preserve_xattrs = 0; set_file_attrs(buf, file, NULL, fname, ATTRS_SET_NANO); preserve_xattrs = save_preserve_xattrs; unmake_file(file); #ifdef SUPPORT_ACLS uncache_tmp_acls(); #endif #ifdef SUPPORT_XATTRS uncache_tmp_xattrs(); #endif success: if (INFO_GTE(BACKUP, 1)) rprintf(FINFO, "backed up %s to %s\n", fname, buf); return ret; }
void recv_generator(char *fname,struct file_list *flist,int i,int f_out) { int fd; STRUCT_STAT st; struct map_struct *buf; struct sum_struct *s; int statret; struct file_struct *file = flist->files[i]; char *fnamecmp; char fnamecmpbuf[MAXPATHLEN]; extern char *compare_dest; extern int list_only; if (list_only) return; if (verbose > 2) rprintf(FINFO,"recv_generator(%s,%d)\n",fname,i); statret = link_stat(fname,&st); if (S_ISDIR(file->mode)) { if (dry_run) return; if (statret == 0 && !S_ISDIR(st.st_mode)) { if (robust_unlink(fname) != 0) { rprintf(FERROR,"unlink %s : %s\n",fname,strerror(errno)); return; } statret = -1; } if (statret != 0 && do_mkdir(fname,file->mode) != 0 && errno != EEXIST) { if (!(relative_paths && errno==ENOENT && create_directory_path(fname)==0 && do_mkdir(fname,file->mode)==0)) { rprintf(FERROR,"mkdir %s : %s (2)\n", fname,strerror(errno)); } } if (set_perms(fname,file,NULL,0) && verbose) rprintf(FINFO,"%s/\n",fname); return; } if (preserve_links && S_ISLNK(file->mode)) { #if SUPPORT_LINKS char lnk[MAXPATHLEN]; int l; extern int safe_symlinks; if (safe_symlinks && unsafe_symlink(file->link, fname)) { if (verbose) { rprintf(FINFO,"ignoring unsafe symlink %s -> %s\n", fname,file->link); } return; } if (statret == 0) { l = readlink(fname,lnk,MAXPATHLEN-1); if (l > 0) { lnk[l] = 0; if (strcmp(lnk,file->link) == 0) { set_perms(fname,file,&st,1); return; } } } delete_file(fname); if (do_symlink(file->link,fname) != 0) { rprintf(FERROR,"link %s -> %s : %s\n", fname,file->link,strerror(errno)); } else { set_perms(fname,file,NULL,0); if (verbose) { rprintf(FINFO,"%s -> %s\n", fname,file->link); } } #endif return; } #ifdef HAVE_MKNOD if (am_root && preserve_devices && IS_DEVICE(file->mode)) { if (statret != 0 || st.st_mode != file->mode || st.st_rdev != file->rdev) { delete_file(fname); if (verbose > 2) rprintf(FINFO,"mknod(%s,0%o,0x%x)\n", fname,(int)file->mode,(int)file->rdev); if (do_mknod(fname,file->mode,file->rdev) != 0) { rprintf(FERROR,"mknod %s : %s\n",fname,strerror(errno)); } else { set_perms(fname,file,NULL,0); if (verbose) rprintf(FINFO,"%s\n",fname); } } else { set_perms(fname,file,&st,1); } return; } #endif if (preserve_hard_links && check_hard_link(file)) { if (verbose > 1) rprintf(FINFO,"%s is a hard link\n",f_name(file)); return; } if (!S_ISREG(file->mode)) { rprintf(FINFO,"skipping non-regular file %s\n",fname); return; } fnamecmp = fname; if ((statret == -1) && (compare_dest != NULL)) { /* try the file at compare_dest instead */ int saveerrno = errno; slprintf(fnamecmpbuf,MAXPATHLEN,"%s/%s",compare_dest,fname); statret = link_stat(fnamecmpbuf,&st); if (!S_ISREG(st.st_mode)) statret = -1; if (statret == -1) errno = saveerrno; else fnamecmp = fnamecmpbuf; } if (statret == -1) { if (errno == ENOENT) { write_int(f_out,i); if (!dry_run) send_sums(NULL,f_out); } else { if (verbose > 1) rprintf(FERROR,"recv_generator failed to open %s\n",fname); } return; } if (!S_ISREG(st.st_mode)) { if (delete_file(fname) != 0) { return; } /* now pretend the file didn't exist */ write_int(f_out,i); if (!dry_run) send_sums(NULL,f_out); return; } if (update_only && st.st_mtime > file->modtime && fnamecmp == fname) { if (verbose > 1) rprintf(FINFO,"%s is newer\n",fname); return; } if (skip_file(fname, file, &st)) { if (fnamecmp == fname) set_perms(fname,file,&st,1); return; } if (dry_run) { write_int(f_out,i); return; } if (whole_file) { write_int(f_out,i); send_sums(NULL,f_out); return; } /* open the file */ fd = open(fnamecmp,O_RDONLY); if (fd == -1) { rprintf(FERROR,"failed to open %s : %s\n",fnamecmp,strerror(errno)); rprintf(FERROR,"skipping %s\n",fname); return; } if (st.st_size > 0) { buf = map_file(fd,st.st_size); } else { buf = NULL; } if (verbose > 3) rprintf(FINFO,"gen mapped %s of size %d\n",fnamecmp,(int)st.st_size); s = generate_sums(buf,st.st_size,adapt_block_size(file, block_size)); if (verbose > 2) rprintf(FINFO,"sending sums for %d\n",i); write_int(f_out,i); send_sums(s,f_out); close(fd); if (buf) unmap_file(buf); free_sums(s); }
void do_file_entry(const u8 * base, const char *dir, const char *path, const char *name, int namelen, const struct cramfs_inode *inode) { int dirlen = strlen(dir); int pathlen = strlen(path); char pname[dirlen + pathlen + namelen + 3]; const char *basename; u32 gid = inode->gid; if (dirlen) { strncpy(pname, dir, dirlen); } if (pathlen) { if (dirlen) { pname[dirlen] = '/'; ++dirlen; } strncpy(pname + dirlen, path, pathlen); } if (namelen) { if (pathlen + dirlen) { pname[dirlen + pathlen] = '/'; ++pathlen; } strncpy(pname + dirlen + pathlen, name, namelen); } pname[pathlen + dirlen + namelen] = 0; basename = namelen ? pname + dirlen + pathlen : "/"; // Create things here //printmode(inode); //printuidgid(inode); if (S_ISREG(inode->mode)) { u32 size = inode->size; if (gid > DIR_GID) { // sirius: this is a special LG encoding of the size. // misusing gid field to encode the most significant byte of the size int lg = gid - DIR_GID; gid -= lg; lg = lg * 0x1000000; size += (lg); } do_file(base, inode->offset << 2, size, pname, basename, inode->mode); } else if (S_ISDIR(inode->mode)) { if (DIR_GID == 0) { DIR_GID = gid; } do_directory(base, inode->offset << 2, inode->size, pname, basename, inode->mode); } else if (S_ISLNK(inode->mode)) { do_symlink(base, inode->offset << 2, inode->size, pname, basename, inode->mode); } else if (S_ISFIFO(inode->mode)) { do_fifo(base, inode->offset << 2, inode->size, pname, basename, inode->mode, inode->uid, inode->gid); } else if (S_ISSOCK(inode->mode)) { do_socket(base, inode->offset << 2, inode->size, pname, basename, inode->mode); } else if (S_ISCHR(inode->mode)) { do_chrdev(base, inode->offset << 2, inode->size, pname, basename, inode->mode, inode->uid, inode->gid); } else if (S_ISBLK(inode->mode)) { do_blkdev(base, inode->offset << 2, inode->size, pname, basename, inode->mode, inode->uid, inode->gid); } else { do_unknown(base, inode->offset << 2, inode->size, pname, basename, inode->mode); } if (geteuid() == 0) { if (lchown(pname, inode->uid, gid) == -1) perror("cannot change owner or group"); } else if (opt_idsfile && path && path[0]) { char dfp[1024]; char *p; FILE *f; strcpy(dfp, pname); p = strrchr(dfp, '/'); if (!p) { fprintf(stderr, "could not find path in '%s'\n", pname); return; } strcpy(p + 1, opt_idsfile); f = fopen(dfp, "at"); if (!f) { perror(dfp); return; } fprintf(f, "%s,%u,%u,%08x\n", basename, inode->uid, inode->gid, inode->mode); fclose(f); } if (geteuid() == 0 || !opt_idsfile) { if (inode->mode & (S_ISGID | S_ISUID | S_ISVTX)) { if (0 != chmod(pname, inode->mode)) { perror("chmod"); return; } } } //printf("\n"); }
static int parse_dispatch_command(struct sftp_conn *conn, const char *cmd, char **pwd, int err_abort) { char *path1, *path2, *tmp; int pflag, lflag, iflag, cmdnum, i; unsigned long n_arg; Attrib a, *aa; char path_buf[MAXPATHLEN]; int err = 0; glob_t g; path1 = path2 = NULL; cmdnum = parse_args(&cmd, &pflag, &lflag, &iflag, &n_arg, &path1, &path2); if (iflag != 0) err_abort = 0; memset(&g, 0, sizeof(g)); /* Perform command */ switch (cmdnum) { case 0: /* Blank line */ break; case -1: /* Unrecognized command */ err = -1; break; case I_GET: err = process_get(conn, path1, path2, *pwd, pflag); break; case I_PUT: err = process_put(conn, path1, path2, *pwd, pflag); break; case I_RENAME: path1 = make_absolute(path1, *pwd); path2 = make_absolute(path2, *pwd); err = do_rename(conn, path1, path2); break; case I_SYMLINK: path2 = make_absolute(path2, *pwd); err = do_symlink(conn, path1, path2); break; case I_RM: path1 = make_absolute(path1, *pwd); remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); for (i = 0; g.gl_pathv[i] && !interrupted; i++) { printf("Removing %s\n", g.gl_pathv[i]); err = do_rm(conn, g.gl_pathv[i]); if (err != 0 && err_abort) break; } break; case I_MKDIR: path1 = make_absolute(path1, *pwd); attrib_clear(&a); a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; a.perm = 0777; err = do_mkdir(conn, path1, &a); break; case I_RMDIR: path1 = make_absolute(path1, *pwd); err = do_rmdir(conn, path1); break; case I_CHDIR: path1 = make_absolute(path1, *pwd); if ((tmp = do_realpath(conn, path1)) == NULL) { err = 1; break; } if ((aa = do_stat(conn, tmp, 0)) == NULL) { xfree(tmp); err = 1; break; } if (!(aa->flags & SSH2_FILEXFER_ATTR_PERMISSIONS)) { error("Can't change directory: Can't check target"); xfree(tmp); err = 1; break; } if (!S_ISDIR(aa->perm)) { error("Can't change directory: \"%s\" is not " "a directory", tmp); xfree(tmp); err = 1; break; } xfree(*pwd); *pwd = tmp; break; case I_LS: if (!path1) { do_globbed_ls(conn, *pwd, *pwd, lflag); break; } /* Strip pwd off beginning of non-absolute paths */ tmp = NULL; if (*path1 != '/') tmp = *pwd; path1 = make_absolute(path1, *pwd); err = do_globbed_ls(conn, path1, tmp, lflag); break; case I_LCHDIR: if (chdir(path1) == -1) { error("Couldn't change local directory to " "\"%s\": %s", path1, strerror(errno)); err = 1; } break; case I_LMKDIR: if (mkdir(path1, 0777) == -1) { error("Couldn't create local directory " "\"%s\": %s", path1, strerror(errno)); err = 1; } break; case I_LLS: local_do_ls(cmd); break; case I_SHELL: local_do_shell(cmd); break; case I_LUMASK: umask(n_arg); printf("Local umask: %03lo\n", n_arg); break; case I_CHMOD: path1 = make_absolute(path1, *pwd); attrib_clear(&a); a.flags |= SSH2_FILEXFER_ATTR_PERMISSIONS; a.perm = n_arg; remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); for (i = 0; g.gl_pathv[i] && !interrupted; i++) { printf("Changing mode on %s\n", g.gl_pathv[i]); err = do_setstat(conn, g.gl_pathv[i], &a); if (err != 0 && err_abort) break; } break; case I_CHOWN: case I_CHGRP: path1 = make_absolute(path1, *pwd); remote_glob(conn, path1, GLOB_NOCHECK, NULL, &g); for (i = 0; g.gl_pathv[i] && !interrupted; i++) { if (!(aa = do_stat(conn, g.gl_pathv[i], 0))) { if (err != 0 && err_abort) break; else continue; } if (!(aa->flags & SSH2_FILEXFER_ATTR_UIDGID)) { error("Can't get current ownership of " "remote file \"%s\"", g.gl_pathv[i]); if (err != 0 && err_abort) break; else continue; } aa->flags &= SSH2_FILEXFER_ATTR_UIDGID; if (cmdnum == I_CHOWN) { printf("Changing owner on %s\n", g.gl_pathv[i]); aa->uid = n_arg; } else { printf("Changing group on %s\n", g.gl_pathv[i]); aa->gid = n_arg; } err = do_setstat(conn, g.gl_pathv[i], aa); if (err != 0 && err_abort) break; } break; case I_PWD: printf("Remote working directory: %s\n", *pwd); break; case I_LPWD: if (!getcwd(path_buf, sizeof(path_buf))) { error("Couldn't get local cwd: %s", strerror(errno)); err = -1; break; } printf("Local working directory: %s\n", path_buf); break; case I_QUIT: /* Processed below */ break; case I_HELP: help(); break; case I_VERSION: printf("SFTP protocol version %u\n", sftp_proto_version(conn)); break; case I_PROGRESS: showprogress = !showprogress; if (showprogress) printf("Progress meter enabled\n"); else printf("Progress meter disabled\n"); break; default: fatal("%d is not implemented", cmdnum); } if (g.gl_pathc) globfree(&g); if (path1) xfree(path1); if (path2) xfree(path2); /* If an unignored error occurs in batch mode we should abort. */ if (err_abort && err != 0) return (-1); else if (cmdnum == I_QUIT) return (1); return (0); }
int restore_line(const char *filename,uint64_t lv,char *line) { char *ptr; uint32_t ts; int status; char* errormsgs[]={ ERROR_STRINGS }; status = ERROR_MISMATCH; ptr = line; EAT(ptr,filename,lv,':'); EAT(ptr,filename,lv,' '); GETU32(ts,ptr); EAT(ptr,filename,lv,'|'); switch (*ptr) { case 'A': if (strncmp(ptr,"ACCESS",6)==0) { status = do_access(filename,lv,ts,ptr+6); } else if (strncmp(ptr,"ATTR",4)==0) { status = do_attr(filename,lv,ts,ptr+4); } else if (strncmp(ptr,"APPEND",6)==0) { status = do_append(filename,lv,ts,ptr+6); } else if (strncmp(ptr,"ACQUIRE",7)==0) { status = do_acquire(filename,lv,ts,ptr+7); } else if (strncmp(ptr,"AQUIRE",6)==0) { status = do_acquire(filename,lv,ts,ptr+6); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'C': if (strncmp(ptr,"CREATE",6)==0) { status = do_create(filename,lv,ts,ptr+6); } else if (strncmp(ptr,"CUSTOMER",8)==0) { // deprecated status = do_session(filename,lv,ts,ptr+8); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'E': if (strncmp(ptr,"EMPTYTRASH",10)==0) { status = do_emptytrash(filename,lv,ts,ptr+10); } else if (strncmp(ptr,"EMPTYRESERVED",13)==0) { status = do_emptyreserved(filename,lv,ts,ptr+13); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'F': if (strncmp(ptr,"FREEINODES",10)==0) { status = do_freeinodes(filename,lv,ts,ptr+10); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'I': if (strncmp(ptr,"INCVERSION",10)==0) { status = do_incversion(filename,lv,ts,ptr+10); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'L': if (strncmp(ptr,"LENGTH",6)==0) { status = do_length(filename,lv,ts,ptr+6); } else if (strncmp(ptr,"LINK",4)==0) { status = do_link(filename,lv,ts,ptr+4); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'M': if (strncmp(ptr,"MOVE",4)==0) { status = do_move(filename,lv,ts,ptr+4); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'P': if (strncmp(ptr,"PURGE",5)==0) { status = do_purge(filename,lv,ts,ptr+5); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'Q': if (strncmp(ptr,"QUOTA",5)==0) { status = do_quota(filename,lv,ts,ptr+5); } break; case 'R': if (strncmp(ptr,"RELEASE",7)==0) { status = do_release(filename,lv,ts,ptr+7); } else if (strncmp(ptr,"REPAIR",6)==0) { status = do_repair(filename,lv,ts,ptr+6); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'S': if (strncmp(ptr,"SETEATTR",8)==0) { status = do_seteattr(filename,lv,ts,ptr+8); } else if (strncmp(ptr,"SETGOAL",7)==0) { status = do_setgoal(filename,lv,ts,ptr+7); } else if (strncmp(ptr,"SETPATH",7)==0) { status = do_setpath(filename,lv,ts,ptr+7); } else if (strncmp(ptr,"SETTRASHTIME",12)==0) { status = do_settrashtime(filename,lv,ts,ptr+12); } else if (strncmp(ptr,"SETXATTR",8)==0) { status = do_setxattr(filename,lv,ts,ptr+8); } else if (strncmp(ptr,"SNAPSHOT",8)==0) { status = do_snapshot(filename,lv,ts,ptr+8); } else if (strncmp(ptr,"SYMLINK",7)==0) { status = do_symlink(filename,lv,ts,ptr+7); } else if (strncmp(ptr,"SESSION",7)==0) { status = do_session(filename,lv,ts,ptr+7); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'T': if (strncmp(ptr,"TRUNC",5)==0) { status = do_trunc(filename,lv,ts,ptr+5); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'U': if (strncmp(ptr,"UNLINK",6)==0) { status = do_unlink(filename,lv,ts,ptr+6); } else if (strncmp(ptr,"UNDEL",5)==0) { status = do_undel(filename,lv,ts,ptr+5); } else if (strncmp(ptr,"UNLOCK",6)==0) { status = do_unlock(filename,lv,ts,ptr+6); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; case 'W': if (strncmp(ptr,"WRITE",5)==0) { status = do_write(filename,lv,ts,ptr+5); } else { printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } break; default: printf("%s:%"PRIu64": unknown entry '%s'\n",filename,lv,ptr); } if (status>STATUS_OK) { printf("%s:%"PRIu64": error: %d (%s)\n",filename,lv,status,errormsgs[status]); } return status; }
/* If we have a --backup-dir, then we get here from make_backup(). * We will move the file to be deleted into a parallel directory tree. */ static int keep_backup(const char *fname) { stat_x sx; struct file_struct *file; char *buf; int save_preserve_xattrs = preserve_xattrs; int kept = 0; int ret_code; /* return if no file to keep */ if (x_lstat(fname, &sx.st, NULL) < 0) return 1; #ifdef SUPPORT_ACLS sx.acc_acl = sx.def_acl = NULL; #endif #ifdef SUPPORT_XATTRS sx.xattr = NULL; #endif if (!(file = make_file(fname, NULL, NULL, 0, NO_FILTERS))) return 1; /* the file could have disappeared */ if (!(buf = get_backup_name(fname))) { unmake_file(file); #ifdef SUPPORT_ACLS uncache_tmp_acls(); #endif #ifdef SUPPORT_XATTRS uncache_tmp_xattrs(); #endif return 0; } #ifdef SUPPORT_ACLS if (preserve_acls && !S_ISLNK(file->mode)) { get_acl(fname, &sx); cache_tmp_acl(file, &sx); free_acl(&sx); } #endif #ifdef SUPPORT_XATTRS if (preserve_xattrs) { get_xattr(fname, &sx); cache_tmp_xattr(file, &sx); free_xattr(&sx); } #endif /* Check to see if this is a device file, or link */ if ((am_root && preserve_devices && IS_DEVICE(file->mode)) || (preserve_specials && IS_SPECIAL(file->mode))) { int save_errno; do_unlink(buf); if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) { save_errno = errno ? errno : EINVAL; /* 0 paranoia */ if (errno == ENOENT && make_bak_dir(buf) == 0) { if (do_mknod(buf, file->mode, sx.st.st_rdev) < 0) save_errno = errno ? errno : save_errno; else save_errno = 0; } if (save_errno) { rsyserr(FERROR, save_errno, "mknod %s failed", full_fname(buf)); } } else save_errno = 0; if (verbose > 2 && save_errno == 0) { rprintf(FINFO, "make_backup: DEVICE %s successful.\n", fname); } kept = 1; do_unlink(fname); } if (!kept && S_ISDIR(file->mode)) { /* make an empty directory */ if (do_mkdir(buf, file->mode) < 0) { int save_errno = errno ? errno : EINVAL; /* 0 paranoia */ if (errno == ENOENT && make_bak_dir(buf) == 0) { if (do_mkdir(buf, file->mode) < 0) save_errno = errno ? errno : save_errno; else save_errno = 0; } if (save_errno) { rsyserr(FINFO, save_errno, "mkdir %s failed", full_fname(buf)); } } ret_code = do_rmdir(fname); if (verbose > 2) { rprintf(FINFO, "make_backup: RMDIR %s returns %i\n", full_fname(fname), ret_code); } kept = 1; } #ifdef SUPPORT_LINKS if (!kept && preserve_links && S_ISLNK(file->mode)) { const char *sl = F_SYMLINK(file); if (safe_symlinks && unsafe_symlink(sl, fname)) { if (verbose) { rprintf(FINFO, "ignoring unsafe symlink %s -> %s\n", full_fname(buf), sl); } kept = 1; } else { do_unlink(buf); if (do_symlink(sl, buf) < 0) { int save_errno = errno ? errno : EINVAL; /* 0 paranoia */ if (errno == ENOENT && make_bak_dir(buf) == 0) { if (do_symlink(sl, buf) < 0) save_errno = errno ? errno : save_errno; else save_errno = 0; } if (save_errno) { rsyserr(FERROR, save_errno, "link %s -> \"%s\"", full_fname(buf), sl); } } do_unlink(fname); kept = 1; } } #endif if (!kept && !S_ISREG(file->mode)) { rprintf(FINFO, "make_bak: skipping non-regular file %s\n", fname); unmake_file(file); #ifdef SUPPORT_ACLS uncache_tmp_acls(); #endif #ifdef SUPPORT_XATTRS uncache_tmp_xattrs(); #endif return 1; } /* move to keep tree if a file */ if (!kept) { if (robust_move(fname, buf) != 0) { rsyserr(FERROR, errno, "keep_backup failed: %s -> \"%s\"", full_fname(fname), buf); } else if (sx.st.st_nlink > 1) { /* If someone has hard-linked the file into the backup * dir, rename() might return success but do nothing! */ robust_unlink(fname); /* Just in case... */ } } preserve_xattrs = 0; set_file_attrs(buf, file, NULL, fname, 0); preserve_xattrs = save_preserve_xattrs; unmake_file(file); #ifdef SUPPORT_ACLS uncache_tmp_acls(); #endif #ifdef SUPPORT_XATTRS uncache_tmp_xattrs(); #endif if (verbose > 1) { rprintf(FINFO, "backed up %s to %s\n", fname, buf); } return 1; }