/* copy contents of directory from_root into to_root */ void dir_copy_files(const char *from_root, const char *to_root) { int i; parray *files = parray_new(); /* don't copy root directory */ dir_list_file(files, from_root, NULL, true, false); for (i = 0; i < parray_num(files); i++) { pgFile *file = (pgFile *) parray_get(files, i); if (S_ISDIR(file->mode)) { char to_path[MAXPGPATH]; join_path_components(to_path, to_root, file->path + strlen(from_root) + 1); if (verbose && !check) printf(_("create directory \"%s\"\n"), file->path + strlen(from_root) + 1); if (!check) { dir_create_dir(to_path, DIR_PERMISSION); } continue; } else if(S_ISREG(file->mode)) { if (verbose && !check) printf(_("copy \"%s\"\n"), file->path + strlen(from_root) + 1); if (!check) copy_file(from_root, to_root, file, NO_COMPRESSION); } } /* cleanup */ parray_walk(files, pgFileFree); parray_free(files); }
/* * Delete backup files of the backup and update the status of the backup to * BACKUP_STATUS_DELETED. */ static int pgBackupDeleteFiles(pgBackup *backup) { int i; char path[MAXPGPATH]; char timestamp[20]; parray *files; /* * If the backup was deleted already, there is nothing to do. */ if (backup->status == BACKUP_STATUS_DELETED) return 0; time2iso(timestamp, lengthof(timestamp), backup->start_time); elog(INFO, "delete: %s", timestamp); /* * Update STATUS to BACKUP_STATUS_DELETING in preparation for the case which * the error occurs before deleting all backup files. */ if (!check) { backup->status = BACKUP_STATUS_DELETING; pgBackupWriteIni(backup); } /* list files to be deleted */ files = parray_new(); pgBackupGetPath(backup, path, lengthof(path), DATABASE_DIR); dir_list_file(files, path, NULL, true, true); /* delete leaf node first */ parray_qsort(files, pgFileComparePathDesc); for (i = 0; i < parray_num(files); i++) { pgFile *file = (pgFile *) parray_get(files, i); /* print progress */ elog(LOG, "delete file(%d/%lu) \"%s\"", i + 1, (unsigned long) parray_num(files), file->path); /* skip actual deletion in check mode */ if (!check) { if (remove(file->path)) { elog(WARNING, "can't remove \"%s\": %s", file->path, strerror(errno)); parray_walk(files, pgFileFree); parray_free(files); return 1; } } } /* * After deleting all of the backup files, update STATUS to * BACKUP_STATUS_DELETED. */ if (!check) { backup->status = BACKUP_STATUS_DELETED; pgBackupWriteIni(backup); } parray_walk(files, pgFileFree); parray_free(files); return 0; }