static bool fs_sis_try_link(struct sis_fs_file *file) { const char *path = fs_file_path(&file->file); const struct stat *st; struct stat st2; st = i_stream_stat(file->hash_input, FALSE); /* we can use the existing file */ if (fs_link(file->super->fs, file->hash_path, path) < 0) { if (errno != ENOENT && errno != EMLINK) i_error("fs-sis: %s", fs_last_error(file->super->fs)); /* failed to use link(), continue as if it hadn't been equal */ return FALSE; } if (fs_stat(file->super->fs, path, &st2) < 0) { i_error("fs-sis: %s", fs_last_error(file->super->fs)); if (fs_unlink(file->super->fs, path) < 0) i_error("fs-sis: %s", fs_last_error(file->super->fs)); return FALSE; } if (st->st_ino != st2.st_ino) { /* the hashes/ file was already replaced with something else */ if (fs_unlink(file->super->fs, path) < 0) i_error("fs-sis: %s", fs_last_error(file->super->fs)); return FALSE; } return TRUE; }
static void fs_sis_replace_hash_file(struct sis_fs_file *file) { const char *hash_fname, *path = fs_file_path(&file->file); struct fs *super_fs = file->super->fs; string_t *temp_path; int ret; if (file->hash_input == NULL) { /* hash file didn't exist previously. we should be able to create it with link() */ if (fs_link(super_fs, path, file->hash_path) < 0) { if (errno == EEXIST) { /* the file was just created. it's probably a duplicate, but it's too much trouble trying to deduplicate it anymore */ } else { i_error("fs-sis: %s", fs_last_error(super_fs)); } } return; } temp_path = t_str_new(256); hash_fname = strrchr(file->hash_path, '/'); if (hash_fname == NULL) hash_fname = file->hash_path; else { str_append_n(temp_path, file->hash_path, (hash_fname-file->hash_path) + 1); hash_fname++; } str_printfa(temp_path, "%s%s.tmp", super_fs->set.temp_file_prefix, hash_fname); /* replace existing hash file atomically */ ret = fs_link(super_fs, path, str_c(temp_path)); if (ret < 0 && errno == EEXIST) { /* either someone's racing us or it's a stale file. try to continue. */ if (fs_unlink(super_fs, str_c(temp_path)) < 0 && errno != ENOENT) i_error("fs-sis: %s", fs_last_error(super_fs)); ret = fs_link(super_fs, path, str_c(temp_path)); } if (ret < 0) { i_error("fs-sis: %s", fs_last_error(super_fs)); return; } if (fs_rename(super_fs, str_c(temp_path), file->hash_path) < 0) { if (errno == ENOENT) { /* apparently someone else just renamed it. ignore. */ } else { i_error("fs-sis: %s", fs_last_error(super_fs)); } (void)fs_unlink(super_fs, str_c(temp_path)); } }
void fs_sis_try_unlink_hash_file(struct fs *fs, struct fs *super, const char *path) { struct stat st1, st2; const char *dir, *hash, *hash_path, *hash_dir; if (fs_sis_path_parse(fs, path, &dir, &hash) == 0 && fs_stat(super, path, &st1) == 0 && st1.st_nlink == 2) { /* this may be the last link. if hashes/ file is the same, delete it. */ hash_path = t_strdup_printf("%s/"HASH_DIR_NAME"/%s", dir, hash); if (fs_stat(super, hash_path, &st2) == 0 && st1.st_ino == st2.st_ino && CMP_DEV_T(st1.st_dev, st2.st_dev)) { if (fs_unlink(super, hash_path) < 0) i_error("%s", fs_last_error(super)); else { /* try to rmdir the hashes/ directory */ hash_dir = t_strdup_printf("%s/"HASH_DIR_NAME, dir); (void)fs_rmdir(super, hash_dir); } } } }
static int fs_sis_open(struct fs *_fs, const char *path, enum fs_open_mode mode, enum fs_open_flags flags, struct fs_file **file_r) { struct sis_fs *fs = (struct sis_fs *)_fs; struct sis_fs_file *file; struct fs_file *super; const char *dir, *hash; if (mode == FS_OPEN_MODE_APPEND) { fs_set_error(_fs, "APPEND mode not supported"); return -1; } if (fs_open(fs->super, path, mode | flags, &super) < 0) { fs_sis_copy_error(fs); return -1; } switch (mode) { case FS_OPEN_MODE_RDONLY: *file_r = super; return 0; case FS_OPEN_MODE_CREATE: case FS_OPEN_MODE_REPLACE: break; case FS_OPEN_MODE_APPEND: i_unreached(); } if (fs_sis_path_parse(_fs, path, &dir, &hash) < 0) return -1; file = i_new(struct sis_fs_file, 1); file->file.fs = _fs; file->file.path = i_strdup(fs_file_path(super)); file->super = super; file->open_mode = mode; file->hash = i_strdup(hash); /* if hashes/<hash> already exists, open it */ file->hash_path = i_strdup_printf("%s/"HASH_DIR_NAME"/%s", dir, hash); if (fs_open(fs->super, file->hash_path, FS_OPEN_MODE_RDONLY, &file->hash_file) < 0 && errno != ENOENT) { i_error("fs-sis: Couldn't open hash file: %s", fs_last_error(fs->super)); } if (file->hash_file != NULL) { file->hash_input = fs_read_stream(file->hash_file, IO_BLOCK_SIZE); } *file_r = &file->file; return 0; }
void fs_sis_try_unlink_hash_file(struct fs *sis_fs, struct fs_file *super_file) { struct fs_file *hash_file; struct stat st1, st2; const char *dir, *hash, *hash_path; if (fs_sis_path_parse(sis_fs, super_file->path, &dir, &hash) == 0 && fs_stat(super_file, &st1) == 0 && st1.st_nlink == 2) { /* this may be the last link. if hashes/ file is the same, delete it. */ hash_path = t_strdup_printf("%s/"HASH_DIR_NAME"/%s", dir, hash); hash_file = fs_file_init(super_file->fs, hash_path, FS_OPEN_MODE_READONLY); if (fs_stat(hash_file, &st2) == 0 && st1.st_ino == st2.st_ino && CMP_DEV_T(st1.st_dev, st2.st_dev)) { if (fs_delete(hash_file) < 0) i_error("%s", fs_last_error(hash_file->fs)); } fs_file_deinit(&hash_file); } }
static void fs_sis_queue_copy_error(struct sis_queue_fs *fs) { fs_set_error(&fs->fs, "%s", fs_last_error(fs->super)); }
static void fs_sis_copy_error(struct sis_fs *fs) { fs_set_error(&fs->fs, "%s", fs_last_error(fs->fs.parent)); }