int main ( int argc, char * argv [] ) { cmd_defaults(&cmd,argv[0]); init_error(); if(!is_path_absolute(argv[0])) { wrn_print("Require execution with absolute path(%s)",argv[0]); terminate(); } cmd_process(argc,argv,&cmd); if(!cmd.no_dump_config) dump_cmd(&cmd); // if( check_creds(&cmd.creds) == -1 ) // wrn_print("check_creds failed, program execution compromised"); if( cmd.kill_running || cmd.rexec_running) { // dbg_print("Sending %s signal",strsignal(cmd.rexec_running ? SIGHUP : cmd.kill_running ? SIGTERM : 0)); KILL_FROM_PID_ERRORS(kill_from_pid_file(cmd.pid_file, cmd.rexec_running ? SIGHUP : SIGTERM)); exit(EXIT_SUCCESS); } if(cmd.logs_enable) if(reopen_files() == -1) { wrn_print("reopen_files failed"); terminate(); } dbg_print("%s started executinon", argv[0]); dbg_print("Running as uid:%ld gid:%ld", (long)getuid(), (long)getgid()); if(cmd.need_daemon) { dbg_print("Demonizing.."); if(become_daemon()) { wrn_print("Can't demonize because of error"); terminate(); } } if(cmd.pid_file) { switch (create_pid_file(cmd.pid_file)) { case -1: wrn_print("creat_pid_file failed"); terminate(); case -2: wrn_print("An instance of %s is already running",cmd.path); exit(EXIT_SUCCESS); } } if(cmd.exit_after_dump) exit(EXIT_SUCCESS); block_all_sigs(); main_loop(); wrn_print("suspicious main_loop return..."); exit(EXIT_FAILURE); }
int move_file(struct flist * file, off_t wsize) { char *from, *to, *buf; off_t size; FILE *input, *output; int ret, dir_id; struct utimbuf ftime = {0}; struct statvfs svf; fsblkcnt_t space; struct stat st; mhdd_debug(MHDD_MSG, "move_file: %s\n", file->real_name); /* TODO: it would be nice to contrive something alter */ flist_wrlock_locked(); from=file->real_name; /* We need to check if already moved */ if (statvfs(from, &svf) != 0) return -errno; space = svf.f_bsize; space *= svf.f_bavail; /* get file size */ if (fstat(file->fh, &st) != 0) { mhdd_debug(MHDD_MSG, "move_file: error stat %s: %s\n", from, strerror(errno)); return -errno; } /* Hard link support is limited to a single device, and files with >1 hardlinks cannot be moved between devices since this would (a) result in partial files on the source device (b) not free the space from the source device during unlink. */ if (st.st_nlink > 1) { mhdd_debug(MHDD_MSG, "move_file: cannot move " "files with >1 hardlinks\n"); return -ENOTSUP; } size = st.st_size; if (size < wsize) size=wsize; if (space > size) { mhdd_debug(MHDD_MSG, "move_file: we have enough space\n"); return 0; } if ((dir_id=find_free_space(size)) == -1) { mhdd_debug(MHDD_MSG, "move_file: can not find space\n"); return -1; } if (!(input = fopen(from, "r"))) return -errno; create_parent_dirs(dir_id, file->name); to = create_path(mhdd.dirs[dir_id], file->name); if (!(output = fopen(to, "w+"))) { ret = -errno; mhdd_debug(MHDD_MSG, "move_file: error create %s: %s\n", to, strerror(errno)); free(to); fclose(input); return(ret); } mhdd_debug(MHDD_MSG, "move_file: move %s to %s\n", from, to); // move data buf=(char *)calloc(sizeof(char), MOVE_BLOCK_SIZE); while((size = fread(buf, sizeof(char), MOVE_BLOCK_SIZE, input))) { if (size != fwrite(buf, sizeof(char), size, output)) { mhdd_debug(MHDD_MSG, "move_file: error move data to %s: %s\n", to, strerror(errno)); fclose(output); fclose(input); free(buf); unlink(to); free(to); return -1; } } free(buf); mhdd_debug(MHDD_MSG, "move_file: done move data\n"); fclose(input); // owner/group/permissions fchmod(fileno(output), st.st_mode); fchown(fileno(output), st.st_uid, st.st_gid); fclose(output); // time ftime.actime = st.st_atime; ftime.modtime = st.st_mtime; utime(to, &ftime); #ifndef WITHOUT_XATTR // extended attributes if (copy_xattrs(from, to) == -1) mhdd_debug(MHDD_MSG, "copy_xattrs: error copying xattrs from %s to %s\n", from, to); #endif from = strdup(from); if ((ret = reopen_files(file, to)) == 0) unlink(from); else unlink(to); mhdd_debug(MHDD_MSG, "move_file: %s -> %s: done, code=%d\n", from, to, ret); free(to); free(from); return ret; }