Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2f07895fae | ||
|
|
f75db195bd |
@@ -102,6 +102,7 @@ partial, alternate, and planned behavior.
|
||||
| `--max-size <n>` | Skip files larger than n bytes |
|
||||
| `--min-size <n>` | Skip files smaller than n bytes |
|
||||
| `--incremental` | Skip files unchanged since last transfer (size + mtime). Auto-enables `--preserve`. Incompatible with `-s`. |
|
||||
| `--existing` | Skip files not already present at the destination; update existing files normally. |
|
||||
| `--bwlimit <KB/s>` | Bandwidth limit in kilobytes per second |
|
||||
| `--chunk-size <n>` | Chunk size in bytes (default: 10485760) |
|
||||
| `--timeout <sec>` | I/O timeout in seconds (default: 30) |
|
||||
|
||||
+6
-6
@@ -6,11 +6,11 @@ This document maps rsync's full feature set to FastSync's current implementation
|
||||
|
||||
| Status | Count | Description |
|
||||
|--------|-------|-------------|
|
||||
| ✅ Implemented | 34 | Feature works end-to-end |
|
||||
| ✅ Implemented | 35 | Feature works end-to-end |
|
||||
| 🔀 Alt Arg | 3 | Functionality exists but under different flag/semantics |
|
||||
| ⚠️ Partial | 1 | Flag parsed/stored but behavior incomplete |
|
||||
| ❌ Not Implemented | 98 | Flag not recognized or no behavior |
|
||||
| **Total** | **136** | |
|
||||
| ⚠️ Partial | 3 | Flag parsed/stored but behavior incomplete |
|
||||
| ❌ Not Implemented | 97 | Flag not recognized or no behavior |
|
||||
| **Total** | **138** | |
|
||||
|
||||
---
|
||||
|
||||
@@ -60,7 +60,7 @@ This document maps rsync's full feature set to FastSync's current implementation
|
||||
| `-I`, `--ignore-times` | Don't skip files matching size+time | ❌ Not Implemented | |
|
||||
| `--size-only` | Skip based on size only | ❌ Not Implemented | |
|
||||
| `-@`, `--modify-window=NUM` | Mod-time comparison accuracy | ❌ Not Implemented | |
|
||||
| `--existing` | Skip creating new files on receiver | ❌ Not Implemented | |
|
||||
| `--existing` | Skip creating new files on receiver | ✅ Implemented | Existing destination files continue through normal update handling |
|
||||
| `--ignore-existing` | Skip updating existing files | ❌ Not Implemented | |
|
||||
| `--remove-source-files` | Sender removes synced files | ❌ Not Implemented | |
|
||||
|
||||
@@ -247,7 +247,7 @@ Ranked by user demand, implementation complexity, and interoperability impact:
|
||||
| 1 | `--whole-file` / `-W` | Low | High — users expect opt-out of delta |
|
||||
| 2 | `--ignore-times` / `-I` | Low | Medium — useful for forcing re-transfer |
|
||||
| 3 | `--size-only` | Low | Medium — common migration scenario |
|
||||
| 4 | `--existing` / `--ignore-existing` | Low | Medium — common sync patterns |
|
||||
| 4 | `--ignore-existing` | Low | Medium — common sync patterns |
|
||||
| 5 | `--remove-source-files` | Low | High — common for moves/backup |
|
||||
| 6 | `--delete-during` | Medium | High — performance improvement |
|
||||
| 7 | `--delay-updates` | Medium | High — atomic updates |
|
||||
|
||||
@@ -153,7 +153,7 @@ static const OptionEntry OPTION_TABLE[] = {
|
||||
{"--sparse", "-S", OPT_FLAG, offsetof(Config, preserve_sparse)},
|
||||
{"--inplace", NULL, OPT_FLAG, offsetof(Config, inplace)},
|
||||
{"--checksum", NULL, OPT_FLAG, offsetof(Config, checksum)},
|
||||
{"--ignore-existing", NULL, OPT_FLAG, offsetof(Config, ignore_existing)},
|
||||
{"--existing", NULL, OPT_FLAG, offsetof(Config, existing)},
|
||||
|
||||
{"--source-dir", NULL, OPT_STRING, offsetof(Config, send_directory)},
|
||||
{"--dest-dir", NULL, OPT_STRING, offsetof(Config, receive_root_directory)},
|
||||
|
||||
+1
-1
@@ -21,7 +21,6 @@ void print_usage(void) {
|
||||
printf(" -p <port> SSH port (default: 22)\n");
|
||||
printf(" --progress Show transfer progress\n");
|
||||
printf(" --delete Delete files on receiver not in source\n");
|
||||
printf(" --ignore-existing Skip files that already exist on receiver\n");
|
||||
printf(" --exclude <pattern> Exclude files matching pattern\n");
|
||||
printf(" --include <pattern> Only include files matching pattern\n");
|
||||
printf(" --exclude-from <file> Read exclude patterns from file\n");
|
||||
@@ -29,6 +28,7 @@ void print_usage(void) {
|
||||
printf(" --max-size <n> Skip files larger than n bytes\n");
|
||||
printf(" --min-size <n> Skip files smaller than n bytes\n");
|
||||
printf(" --incremental Skip files unchanged since last transfer\n");
|
||||
printf(" --existing Skip files not already present at destination\n");
|
||||
printf(" --delta Delta transfer for changed files (requires --incremental)\n");
|
||||
printf(" --delta-block <n> Delta block size in bytes (default: %d)\n",
|
||||
DELTA_BLOCK_SIZE_DEFAULT);
|
||||
|
||||
+5
-5
@@ -68,7 +68,7 @@ static void config_set_defaults(Config* config) {
|
||||
config->debug_level = 0;
|
||||
config->list_only = false;
|
||||
config->human_readable = false;
|
||||
config->ignore_existing = false;
|
||||
config->existing = false;
|
||||
config->update = false;
|
||||
config->inplace = false;
|
||||
config->append = false;
|
||||
@@ -124,7 +124,7 @@ static bool validate_received_config(const Config* config) {
|
||||
valid_wire_bool(config->copy_unsafe_links) &&
|
||||
valid_wire_bool(config->preserve_hard_links) && valid_wire_bool(config->preserve_acls) &&
|
||||
valid_wire_bool(config->preserve_xattrs) && valid_wire_bool(config->preserve_devices) &&
|
||||
valid_wire_bool(config->preserve_sparse) && valid_wire_bool(config->ignore_existing) &&
|
||||
valid_wire_bool(config->preserve_sparse) && valid_wire_bool(config->existing) &&
|
||||
valid_wire_bool(config->update) && valid_wire_bool(config->inplace) &&
|
||||
valid_wire_bool(config->append) && valid_wire_bool(config->append_verify) &&
|
||||
valid_wire_bool(config->delete_excluded) && valid_wire_bool(config->delete_after) &&
|
||||
@@ -245,7 +245,7 @@ static bool send_file_options(int fd, const Config* c) {
|
||||
}
|
||||
|
||||
static bool send_selection_options(int fd, const Config* c) {
|
||||
return send_int(fd, c->ignore_existing) && send_int(fd, c->update) && send_int(fd, c->inplace) &&
|
||||
return send_int(fd, c->existing) && send_int(fd, c->update) && send_int(fd, c->inplace) &&
|
||||
send_int(fd, c->append) && send_int(fd, c->append_verify) &&
|
||||
send_int(fd, c->delete_excluded) && send_int(fd, c->delete_after) &&
|
||||
send_n_data(fd, &c->max_delete, sizeof(c->max_delete)) && send_int(fd, c->relative) &&
|
||||
@@ -307,8 +307,8 @@ static bool receive_file_options(int fd, Config* c) {
|
||||
}
|
||||
|
||||
static bool receive_selection_options(int fd, Config* c) {
|
||||
bool* flags[] = {&c->ignore_existing, &c->update, &c->inplace, &c->append,
|
||||
&c->append_verify, &c->delete_excluded, &c->delete_after};
|
||||
bool* flags[] = {&c->existing, &c->update, &c->inplace, &c->append,
|
||||
&c->append_verify, &c->delete_excluded, &c->delete_after};
|
||||
for (size_t i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
|
||||
if (!receive_wire_bool(fd, flags[i]))
|
||||
return false;
|
||||
|
||||
+1
-1
@@ -76,7 +76,7 @@ typedef struct Config {
|
||||
bool human_readable;
|
||||
|
||||
// Issue #127: Transfer modes
|
||||
bool ignore_existing;
|
||||
bool existing;
|
||||
bool update;
|
||||
bool inplace;
|
||||
bool append;
|
||||
|
||||
+5
-38
@@ -172,17 +172,8 @@ bool file_set_authorized_root(int fd, const char* canonical_path) {
|
||||
}
|
||||
|
||||
bool file_path_exists_secure(const char* path) {
|
||||
if (!path)
|
||||
return false;
|
||||
char* leaf = NULL;
|
||||
int parent_fd = file_open_secure_parent(path, &leaf, false);
|
||||
if (parent_fd < 0)
|
||||
return false;
|
||||
struct stat st;
|
||||
bool exists = fstatat(parent_fd, leaf, &st, AT_SYMLINK_NOFOLLOW) == 0;
|
||||
close(parent_fd);
|
||||
free(leaf);
|
||||
return exists;
|
||||
return file_stat_secure(path, &st);
|
||||
}
|
||||
|
||||
bool file_stat_secure(const char* path, struct stat* st) {
|
||||
@@ -311,9 +302,8 @@ bool file_rename_secure(const char* old_path, const char* new_path) {
|
||||
return ok;
|
||||
}
|
||||
|
||||
static bool file_to_disk_secure_impl(const char* path, const void* data,
|
||||
unsigned long long data_size, bool inplace, bool sparse,
|
||||
const FileMetadata* metadata, bool no_replace) {
|
||||
bool file_to_disk_secure(const char* path, const void* data, unsigned long long data_size,
|
||||
bool inplace, bool sparse, const FileMetadata* metadata) {
|
||||
char* leaf = NULL;
|
||||
int dirfd = file_open_secure_parent(path, &leaf, true);
|
||||
if (dirfd < 0)
|
||||
@@ -344,20 +334,8 @@ static bool file_to_disk_secure_impl(const char* path, const void* data,
|
||||
if (close(fd) != 0)
|
||||
ok = false;
|
||||
fd = -1;
|
||||
if (ok) {
|
||||
if (no_replace) {
|
||||
/* The probe and commit cannot be one operation. A concurrent
|
||||
creator may win; EEXIST is then the requested skip. */
|
||||
if (linkat(dirfd, tmp, dirfd, leaf, 0) == 0 || errno == EEXIST) {
|
||||
if (unlinkat(dirfd, tmp, 0) != 0 && errno != ENOENT)
|
||||
ok = false;
|
||||
} else {
|
||||
ok = false;
|
||||
}
|
||||
} else if (renameat(dirfd, tmp, dirfd, leaf) != 0) {
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
if (ok && renameat(dirfd, tmp, dirfd, leaf) != 0)
|
||||
ok = false;
|
||||
if (!ok)
|
||||
unlinkat(dirfd, tmp, 0);
|
||||
}
|
||||
@@ -369,17 +347,6 @@ static bool file_to_disk_secure_impl(const char* path, const void* data,
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool file_to_disk_secure(const char* path, const void* data, unsigned long long data_size,
|
||||
bool inplace, bool sparse, const FileMetadata* metadata) {
|
||||
return file_to_disk_secure_impl(path, data, data_size, inplace, sparse, metadata, false);
|
||||
}
|
||||
|
||||
bool file_to_disk_secure_no_replace(const char* path, const void* data,
|
||||
unsigned long long data_size, bool sparse,
|
||||
const FileMetadata* metadata) {
|
||||
return file_to_disk_secure_impl(path, data, data_size, false, sparse, metadata, true);
|
||||
}
|
||||
|
||||
bool file_write_to_disk(const char* path, const void* data, unsigned long long data_size,
|
||||
bool inplace, bool sparse) {
|
||||
if (!path || (!data && data_size != 0) || has_path_traversal(path))
|
||||
|
||||
@@ -32,8 +32,5 @@ bool file_ensure_directory_secure(const char* path);
|
||||
bool file_rename_secure(const char* old_path, const char* new_path);
|
||||
bool file_to_disk_secure(const char* path, const void* data, unsigned long long data_size,
|
||||
bool inplace, bool sparse, const FileMetadata* metadata);
|
||||
bool file_to_disk_secure_no_replace(const char* path, const void* data,
|
||||
unsigned long long data_size, bool sparse,
|
||||
const FileMetadata* metadata);
|
||||
|
||||
#endif
|
||||
|
||||
+20
-21
@@ -22,15 +22,14 @@
|
||||
#define MAX_FILE_DATA_SIZE MAX_RECEIVE_FILE_SIZE
|
||||
|
||||
bool file_save_to_disk(const char* root_directory, const File* file, const Config* config) {
|
||||
/* Backups are incompatible with ignore-existing: moving the entry first
|
||||
would make a concurrent no-replace commit overwrite its old name. */
|
||||
bool backup_enabled = config && config->backup && !config->ignore_existing;
|
||||
bool backup_enabled = config && config->backup;
|
||||
bool inplace = config && config->inplace;
|
||||
bool sparse = config && config->preserve_sparse;
|
||||
const char* backup_suffix = (config && config->suffix) ? config->suffix : "~";
|
||||
const char* backup_dir = (config && config->backup_dir) ? config->backup_dir : NULL;
|
||||
const char* partial_dir = (config && config->partial_dir) ? config->partial_dir : NULL;
|
||||
char *confined_backup = NULL, *confined_partial = NULL, *disk_path = NULL;
|
||||
char* destination_path = NULL;
|
||||
char *backup_path = NULL, *parent_copy = NULL;
|
||||
|
||||
if (!file || !file->path || !file->data || (file->data->size != 0 && !file->data->data) ||
|
||||
@@ -56,26 +55,26 @@ bool file_save_to_disk(const char* root_directory, const File* file, const Confi
|
||||
|
||||
const char* actual_root =
|
||||
(partial_dir && config && config->partial) ? confined_partial : root_directory;
|
||||
destination_path = path_cat(root_directory, file->path);
|
||||
disk_path = path_cat(actual_root, file->path);
|
||||
if (disk_path == NULL) {
|
||||
if (destination_path == NULL || disk_path == NULL) {
|
||||
free(confined_backup);
|
||||
free(confined_partial);
|
||||
free(destination_path);
|
||||
free(disk_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
/* --ignore-existing checks the final destination before partial files or
|
||||
overwrite policies can modify it. */
|
||||
if (config && config->ignore_existing) {
|
||||
char* final_path = path_cat(root_directory, file->path);
|
||||
bool exists = final_path && file_path_exists_secure(final_path);
|
||||
free(final_path);
|
||||
if (exists) {
|
||||
free(confined_backup);
|
||||
free(confined_partial);
|
||||
free(disk_path);
|
||||
return true;
|
||||
}
|
||||
/* --existing checks the final destination, not a temporary partial path. */
|
||||
if (config && config->existing && !file_path_exists_secure(destination_path)) {
|
||||
free(confined_backup);
|
||||
free(confined_partial);
|
||||
free(destination_path);
|
||||
free(disk_path);
|
||||
return true;
|
||||
}
|
||||
free(destination_path);
|
||||
destination_path = NULL;
|
||||
|
||||
/* --update is receiver-side policy: never replace a newer destination. */
|
||||
if (config && config->update) {
|
||||
@@ -84,6 +83,7 @@ bool file_save_to_disk(const char* root_directory, const File* file, const Confi
|
||||
destination_stat.st_mtime > file->metadata->mtime_sec) {
|
||||
free(confined_backup);
|
||||
free(confined_partial);
|
||||
free(destination_path);
|
||||
free(disk_path);
|
||||
return true;
|
||||
}
|
||||
@@ -119,15 +119,13 @@ bool file_save_to_disk(const char* root_directory, const File* file, const Confi
|
||||
}
|
||||
}
|
||||
|
||||
bool ok = config && config->ignore_existing
|
||||
? file_to_disk_secure_no_replace(disk_path, file->data->data, file->data->size,
|
||||
sparse, file->metadata)
|
||||
: file_to_disk_secure(disk_path, file->data->data, file->data->size, inplace,
|
||||
sparse, file->metadata);
|
||||
bool ok = file_to_disk_secure(disk_path, file->data->data, file->data->size, inplace, sparse,
|
||||
file->metadata);
|
||||
free(parent_copy);
|
||||
free(backup_path);
|
||||
free(confined_backup);
|
||||
free(confined_partial);
|
||||
free(destination_path);
|
||||
free(disk_path);
|
||||
return ok;
|
||||
|
||||
@@ -136,6 +134,7 @@ fail:
|
||||
free(backup_path);
|
||||
free(confined_backup);
|
||||
free(confined_partial);
|
||||
free(destination_path);
|
||||
free(disk_path);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -214,31 +214,32 @@ class TestIncremental:
|
||||
assert f.read() == b"hello world\n"
|
||||
|
||||
|
||||
class TestIgnoreExisting:
|
||||
def test_ignore_existing_preserves_existing_and_transfers_new(self, shared_server):
|
||||
class TestExisting:
|
||||
def test_existing_updates_existing_and_skips_new(self, shared_server):
|
||||
clean_dir(DEST_DIR)
|
||||
result, _ = run_client(SOURCE_DIR, DEST_DIR, port=shared_server.port)
|
||||
assert result.returncode == 0
|
||||
result, _ = run_client(SOURCE_DIR, DEST_DIR, flags=["-M"], port=shared_server.port)
|
||||
assert result.returncode == 0, f"Initial sync failed: {(result.stderr or result.stdout)[:200]}"
|
||||
|
||||
received = get_dest_received_dir(DEST_DIR, SOURCE_DIR)
|
||||
existing_file = os.path.join(received, "small.txt")
|
||||
with open(existing_file, "wb") as f:
|
||||
f.write(b"destination content\n")
|
||||
new_source = os.path.join(SOURCE_DIR, "new.txt")
|
||||
try:
|
||||
with open(new_source, "wb") as f:
|
||||
f.write(b"new file\n")
|
||||
source_file = os.path.join(SOURCE_DIR, "small.txt")
|
||||
new_source_file = os.path.join(SOURCE_DIR, "new-existing-test.txt")
|
||||
with open(source_file, "wb") as f:
|
||||
f.write(b"updated existing content\n")
|
||||
with open(new_source_file, "wb") as f:
|
||||
f.write(b"this file must not be created\n")
|
||||
|
||||
try:
|
||||
result, _ = run_client(SOURCE_DIR, DEST_DIR,
|
||||
flags=["--ignore-existing"], port=shared_server.port)
|
||||
assert result.returncode == 0, f"Sync failed: {(result.stderr or result.stdout)[:200]}"
|
||||
with open(existing_file, "rb") as f:
|
||||
assert f.read() == b"destination content\n"
|
||||
with open(os.path.join(received, "new.txt"), "rb") as f:
|
||||
assert f.read() == b"new file\n"
|
||||
flags=["-M", "--existing"], port=shared_server.port)
|
||||
assert result.returncode == 0, f"--existing sync failed: {(result.stderr or result.stdout)[:200]}"
|
||||
|
||||
with open(os.path.join(received, "small.txt"), "rb") as f:
|
||||
assert f.read() == b"updated existing content\n"
|
||||
assert not os.path.exists(os.path.join(received, "new-existing-test.txt"))
|
||||
finally:
|
||||
if os.path.lexists(new_source):
|
||||
os.unlink(new_source)
|
||||
os.unlink(new_source_file)
|
||||
with open(source_file, "wb") as f:
|
||||
f.write(b"hello world\n")
|
||||
|
||||
|
||||
class TestDelete:
|
||||
|
||||
+12
-14
@@ -181,19 +181,6 @@ static void test_parse_args_valid_port() {
|
||||
config_delete(cfg);
|
||||
}
|
||||
|
||||
static void test_parse_args_ignore_existing() {
|
||||
Config* cfg = config_create();
|
||||
char* argv[] = {"fastsync", "--ignore-existing", "/src", "/dst"};
|
||||
int positional_args[2];
|
||||
int positional_count = 0;
|
||||
|
||||
int ret = parse_args(cfg, 4, argv, positional_args, &positional_count);
|
||||
EXPECT_EQ_INT(ret, 0);
|
||||
EXPECT_TRUE(cfg->ignore_existing);
|
||||
|
||||
config_delete(cfg);
|
||||
}
|
||||
|
||||
/* Test parse_args rejects port > 65535 */
|
||||
static void test_parse_args_invalid_port() {
|
||||
Config* cfg = config_create();
|
||||
@@ -352,6 +339,17 @@ static void test_parse_args_archive() {
|
||||
config_delete(cfg);
|
||||
}
|
||||
|
||||
static void test_parse_args_existing() {
|
||||
Config* cfg = config_create();
|
||||
char* argv[] = {"fastsync", "--existing", "/src", "/dst"};
|
||||
int positional_args[2];
|
||||
int positional_count = 0;
|
||||
|
||||
EXPECT_EQ_INT(parse_args(cfg, 4, argv, positional_args, &positional_count), 0);
|
||||
EXPECT_TRUE(cfg->existing);
|
||||
config_delete(cfg);
|
||||
}
|
||||
|
||||
void test_client_cli() {
|
||||
test_validate_config_required_paths();
|
||||
test_validate_config_incompatible_options();
|
||||
@@ -365,7 +363,6 @@ void test_client_cli() {
|
||||
test_parse_args_help();
|
||||
test_parse_args_version();
|
||||
test_parse_args_valid_port();
|
||||
test_parse_args_ignore_existing();
|
||||
test_parse_args_invalid_port();
|
||||
test_parse_args_non_numeric_port();
|
||||
test_parse_args_invalid_server_port();
|
||||
@@ -374,4 +371,5 @@ void test_client_cli() {
|
||||
test_parse_args_unknown_option();
|
||||
test_parse_args_rejects_unimplemented_options();
|
||||
test_parse_args_archive();
|
||||
test_parse_args_existing();
|
||||
}
|
||||
|
||||
+4
-4
@@ -126,7 +126,7 @@ static void test_config_send_receive() {
|
||||
send_cfg->use_metadata = true;
|
||||
send_cfg->compression_level = 5;
|
||||
send_cfg->chunk_size = 1024;
|
||||
send_cfg->ignore_existing = true;
|
||||
send_cfg->existing = true;
|
||||
|
||||
/* Use socketpair for bidirectional communication */
|
||||
int p[2];
|
||||
@@ -161,7 +161,7 @@ static void test_config_send_receive() {
|
||||
ok = false;
|
||||
if (recv_cfg->chunk_size != 1024)
|
||||
ok = false;
|
||||
if (!recv_cfg->ignore_existing)
|
||||
if (!recv_cfg->existing)
|
||||
ok = false;
|
||||
}
|
||||
config_delete(recv_cfg);
|
||||
@@ -188,11 +188,11 @@ static void test_config_send_receive() {
|
||||
}
|
||||
|
||||
static void test_config_send_receive_version_mismatch() {
|
||||
/* A peer using the previous wire format must be rejected. */
|
||||
/* Create a config with a different protocol version */
|
||||
Config* cfg = config_create();
|
||||
EXPECT_NOT_NULL(cfg);
|
||||
free(cfg->version);
|
||||
cfg->version = str_dup("2.2.0");
|
||||
cfg->version = str_dup("0.0");
|
||||
cfg->send_directory = str_dup("/src");
|
||||
cfg->receive_root_directory = str_dup("/dst");
|
||||
|
||||
|
||||
+35
-85
@@ -88,96 +88,47 @@ static void test_file_save_to_disk() {
|
||||
rmdir("test_save_tmp");
|
||||
}
|
||||
|
||||
static void test_file_save_to_disk_ignore_existing() {
|
||||
const char* path = "test_ignore_existing_tmp/existing.txt";
|
||||
EXPECT_TRUE(file_write_to_disk(path, "old", 3, false, false));
|
||||
static void test_file_save_to_disk_existing() {
|
||||
const char* root = "test_existing_tmp";
|
||||
const char* existing_path = "test_existing_tmp/existing.txt";
|
||||
const char* missing_path = "test_existing_tmp/missing.txt";
|
||||
EXPECT_TRUE(file_write_to_disk(existing_path, "old", 3, false, false));
|
||||
|
||||
File* file = file_create("existing.txt");
|
||||
EXPECT_NOT_NULL(file);
|
||||
file->data->data = malloc(3);
|
||||
EXPECT_NOT_NULL(file->data->data);
|
||||
memcpy(file->data->data, "new", 3);
|
||||
file->data->size = 3;
|
||||
Config* cfg = config_create();
|
||||
EXPECT_NOT_NULL(cfg);
|
||||
cfg->existing = true;
|
||||
|
||||
Config* config = config_create();
|
||||
EXPECT_NOT_NULL(config);
|
||||
config->ignore_existing = true;
|
||||
EXPECT_TRUE(file_save_to_disk("test_ignore_existing_tmp", file, config));
|
||||
File* existing = file_create("existing.txt");
|
||||
EXPECT_NOT_NULL(existing);
|
||||
existing->data->data = malloc(3);
|
||||
EXPECT_NOT_NULL(existing->data->data);
|
||||
memcpy(existing->data->data, "new", 3);
|
||||
existing->data->size = 3;
|
||||
EXPECT_TRUE(file_save_to_disk(root, existing, cfg));
|
||||
file_destroy(existing);
|
||||
|
||||
FILE* stream = fopen(path, "rb");
|
||||
File* missing = file_create("missing.txt");
|
||||
EXPECT_NOT_NULL(missing);
|
||||
missing->data->data = malloc(7);
|
||||
EXPECT_NOT_NULL(missing->data->data);
|
||||
memcpy(missing->data->data, "skipped", 7);
|
||||
missing->data->size = 7;
|
||||
EXPECT_TRUE(file_save_to_disk(root, missing, cfg));
|
||||
file_destroy(missing);
|
||||
|
||||
FILE* fp = fopen(existing_path, "rb");
|
||||
char content[4] = {0};
|
||||
EXPECT_NOT_NULL(stream);
|
||||
EXPECT_NOT_NULL(fp);
|
||||
// cppcheck-suppress knownConditionTrueFalse
|
||||
if (stream) {
|
||||
EXPECT_EQ_INT((int)fread(content, 1, 3, stream), 3);
|
||||
fclose(stream);
|
||||
if (fp) {
|
||||
EXPECT_EQ_INT((int)fread(content, 1, 3, fp), 3);
|
||||
fclose(fp);
|
||||
}
|
||||
EXPECT_EQ_STR(content, "old");
|
||||
EXPECT_EQ_STR(content, "new");
|
||||
EXPECT_EQ_INT(access(missing_path, F_OK), -1);
|
||||
|
||||
file_destroy(file);
|
||||
config_delete(config);
|
||||
unlink(path);
|
||||
rmdir("test_ignore_existing_tmp");
|
||||
}
|
||||
|
||||
static void test_file_save_to_disk_ignore_existing_entry_types() {
|
||||
const char* root = "test_ignore_existing_entries_tmp";
|
||||
const char* directory = "test_ignore_existing_entries_tmp/directory";
|
||||
const char* link = "test_ignore_existing_entries_tmp/link";
|
||||
const char* target = "test_ignore_existing_entries_tmp/target";
|
||||
const char* backup = "test_ignore_existing_entries_tmp/backup.txt~";
|
||||
const char* backup_file = "test_ignore_existing_entries_tmp/backup.txt";
|
||||
Config* config = config_create();
|
||||
File* file = file_create("unused");
|
||||
|
||||
unlink(link);
|
||||
unlink(target);
|
||||
unlink(backup);
|
||||
unlink(backup_file);
|
||||
rmdir(directory);
|
||||
rmdir(root);
|
||||
EXPECT_NOT_NULL(config);
|
||||
EXPECT_NOT_NULL(file);
|
||||
// cppcheck-suppress knownConditionTrueFalse
|
||||
if (!config || !file)
|
||||
return;
|
||||
config->ignore_existing = true;
|
||||
config->backup = true;
|
||||
file->data->data = malloc(3);
|
||||
EXPECT_NOT_NULL(file->data->data);
|
||||
// cppcheck-suppress knownConditionTrueFalse
|
||||
if (!file->data->data) {
|
||||
file_destroy(file);
|
||||
config_delete(config);
|
||||
return;
|
||||
}
|
||||
memcpy(file->data->data, "new", 3);
|
||||
file->data->size = 3;
|
||||
|
||||
EXPECT_EQ_INT(mkdir(root, 0755), 0);
|
||||
EXPECT_EQ_INT(mkdir(directory, 0755), 0);
|
||||
EXPECT_TRUE(file_write_to_disk(target, "old", 3, false, false));
|
||||
EXPECT_EQ_INT(symlink("target", link), 0);
|
||||
free(file->path);
|
||||
file->path = str_dup("directory");
|
||||
EXPECT_TRUE(file_save_to_disk(root, file, config));
|
||||
free(file->path);
|
||||
file->path = str_dup("link");
|
||||
EXPECT_TRUE(file_save_to_disk(root, file, config));
|
||||
|
||||
free(file->path);
|
||||
file->path = str_dup("backup.txt");
|
||||
EXPECT_TRUE(file_write_to_disk(backup_file, "old", 3, false, false));
|
||||
EXPECT_TRUE(file_save_to_disk(root, file, config));
|
||||
EXPECT_TRUE(file_path_exists_secure(backup_file));
|
||||
EXPECT_FALSE(file_path_exists_secure(backup));
|
||||
|
||||
file_destroy(file);
|
||||
config_delete(config);
|
||||
unlink(link);
|
||||
unlink(target);
|
||||
unlink(backup_file);
|
||||
rmdir(directory);
|
||||
config_delete(cfg);
|
||||
unlink(existing_path);
|
||||
rmdir(root);
|
||||
}
|
||||
|
||||
@@ -551,8 +502,7 @@ void test_file() {
|
||||
test_file_load_data();
|
||||
test_file_load_data_missing_file();
|
||||
test_file_save_to_disk();
|
||||
test_file_save_to_disk_ignore_existing();
|
||||
test_file_save_to_disk_ignore_existing_entry_types();
|
||||
test_file_save_to_disk_existing();
|
||||
test_file_write_to_disk_basic();
|
||||
test_file_write_to_disk_creates_dirs();
|
||||
test_file_write_to_disk_does_not_follow_symlink();
|
||||
|
||||
Reference in New Issue
Block a user