From 4c1fc3e404d648c70bd2f50ac50aeac6ece8872e Mon Sep 17 00:00:00 2001 From: Daniel Mack Date: Mon, 6 Jul 2015 19:19:25 -0400 Subject: fileio: consolidate write_string_file*() Merge write_string_file(), write_string_file_no_create() and write_string_file_atomic() into write_string_file() and provide a flags mask that allows combinations of atomic writing, newline appending and automatic file creation. Change all users accordingly. --- src/test/test-btrfs.c | 2 +- src/test/test-copy.c | 6 +++--- src/test/test-fileio.c | 10 +++++----- src/test/test-util.c | 20 ++++++++++---------- 4 files changed, 19 insertions(+), 19 deletions(-) (limited to 'src/test') diff --git a/src/test/test-btrfs.c b/src/test/test-btrfs.c index 838ffcba3d..e4771c9dd7 100644 --- a/src/test/test-btrfs.c +++ b/src/test/test-btrfs.c @@ -68,7 +68,7 @@ int main(int argc, char *argv[]) { if (r < 0) log_error_errno(r, "Failed to make subvolume: %m"); - r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha"); + r = write_string_file("/xxxtest/afile", "ljsadhfljasdkfhlkjdsfha", WRITE_STRING_FILE_CREATE); if (r < 0) log_error_errno(r, "Failed to write file: %m"); diff --git a/src/test/test-copy.c b/src/test/test-copy.c index b1385b8b87..b73c958ec5 100644 --- a/src/test/test-copy.c +++ b/src/test/test-copy.c @@ -43,7 +43,7 @@ static void test_copy_file(void) { assert_se(fd >= 0); close(fd); - assert_se(write_string_file(fn, "foo bar bar bar foo") == 0); + assert_se(write_string_file(fn, "foo bar bar bar foo", WRITE_STRING_FILE_CREATE) == 0); assert_se(copy_file(fn, fn_copy, 0, 0644, 0) == 0); @@ -67,7 +67,7 @@ static void test_copy_file_fd(void) { out_fd = mkostemp_safe(out_fn, O_RDWR); assert_se(out_fd >= 0); - assert_se(write_string_file(in_fn, text) == 0); + assert_se(write_string_file(in_fn, text, WRITE_STRING_FILE_CREATE) == 0); assert_se(copy_file_fd("/a/file/which/does/not/exist/i/guess", out_fd, true) < 0); assert_se(copy_file_fd(in_fn, out_fd, true) >= 0); assert_se(lseek(out_fd, SEEK_SET, 0) == 0); @@ -94,7 +94,7 @@ static void test_copy_tree(void) { char *f = strjoina(original_dir, *p); assert_se(mkdir_parents(f, 0755) >= 0); - assert_se(write_string_file(f, "file") == 0); + assert_se(write_string_file(f, "file", WRITE_STRING_FILE_CREATE) == 0); } STRV_FOREACH_PAIR(link, p, links) { diff --git a/src/test/test-fileio.c b/src/test/test-fileio.c index 725c2fab4a..be3a87958f 100644 --- a/src/test/test-fileio.c +++ b/src/test/test-fileio.c @@ -334,7 +334,7 @@ static void test_write_string_file(void) { fd = mkostemp_safe(fn, O_RDWR); assert_se(fd >= 0); - assert_se(write_string_file(fn, "boohoo") == 0); + assert_se(write_string_file(fn, "boohoo", WRITE_STRING_FILE_CREATE) == 0); assert_se(read(fd, buf, sizeof(buf)) == 7); assert_se(streq(buf, "boohoo\n")); @@ -350,8 +350,8 @@ static void test_write_string_file_no_create(void) { fd = mkostemp_safe(fn, O_RDWR); assert_se(fd >= 0); - assert_se(write_string_file_no_create("/a/file/which/does/not/exists/i/guess", "boohoo") < 0); - assert_se(write_string_file_no_create(fn, "boohoo") == 0); + assert_se(write_string_file("/a/file/which/does/not/exists/i/guess", "boohoo", 0) < 0); + assert_se(write_string_file(fn, "boohoo", 0) == 0); assert_se(read(fd, buf, sizeof(buf)) == strlen("boohoo\n")); assert_se(streq(buf, "boohoo\n")); @@ -377,8 +377,8 @@ static void test_load_env_file_pairs(void) { "ANSI_COLOR=\"0;36\"\n" "HOME_URL=\"https://www.archlinux.org/\"\n" "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n" - "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n" - ); + "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n", + WRITE_STRING_FILE_CREATE); assert_se(r == 0); f = fdopen(fd, "r"); diff --git a/src/test/test-util.c b/src/test/test-util.c index ad9ea3bcce..9fbfece14f 100644 --- a/src/test/test-util.c +++ b/src/test/test-util.c @@ -565,14 +565,14 @@ static void test_read_hostname_config(void) { close(fd); /* simple hostname */ - write_string_file(path, "foo"); + write_string_file(path, "foo", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(streq(hostname, "foo")); free(hostname); hostname = NULL; /* with comment */ - write_string_file(path, "# comment\nfoo"); + write_string_file(path, "# comment\nfoo", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); @@ -580,7 +580,7 @@ static void test_read_hostname_config(void) { hostname = NULL; /* with comment and extra whitespace */ - write_string_file(path, "# comment\n\n foo "); + write_string_file(path, "# comment\n\n foo ", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foo")); @@ -588,7 +588,7 @@ static void test_read_hostname_config(void) { hostname = NULL; /* cleans up name */ - write_string_file(path, "!foo/bar.com"); + write_string_file(path, "!foo/bar.com", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == 0); assert_se(hostname); assert_se(streq(hostname, "foobar.com")); @@ -597,7 +597,7 @@ static void test_read_hostname_config(void) { /* no value set */ hostname = (char*) 0x1234; - write_string_file(path, "# nothing here\n"); + write_string_file(path, "# nothing here\n", WRITE_STRING_FILE_CREATE); assert_se(read_hostname_config(path, &hostname) == -ENOENT); assert_se(hostname == (char*) 0x1234); /* does not touch argument on error */ @@ -1191,11 +1191,11 @@ static void test_execute_directory(void) { masked = strjoina(template_lo, "/masked"); mask = strjoina(template_hi, "/masked"); - assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works") == 0); - assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2") == 0); - assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed") == 0); - assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0") == 0); - assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed") == 0); + assert_se(write_string_file(name, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works", WRITE_STRING_FILE_CREATE) == 0); + assert_se(write_string_file(name2, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/it_works2", WRITE_STRING_FILE_CREATE) == 0); + assert_se(write_string_file(overridden, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0); + assert_se(write_string_file(override, "#!/bin/sh\necho 'Executing '$0", WRITE_STRING_FILE_CREATE) == 0); + assert_se(write_string_file(masked, "#!/bin/sh\necho 'Executing '$0\ntouch $(dirname $0)/failed", WRITE_STRING_FILE_CREATE) == 0); assert_se(symlink("/dev/null", mask) == 0); assert_se(chmod(name, 0755) == 0); assert_se(chmod(name2, 0755) == 0); -- cgit v1.2.3-54-g00ecf