From d65fdcd0f7d076126211f15ed23bf3675167ea74 Mon Sep 17 00:00:00 2001 From: Tim Kientzle Date: Tue, 4 Aug 2009 01:24:31 -0400 Subject: [PATCH] Convert most open()/read()/write()/close() to use assertMakeFile(), stdio, and slurpfile(), as appropriate. SVN-Revision: 1337 --- cpio/test/test_option_a.c | 14 ++++---------- cpio/test/test_option_l.c | 6 +----- cpio/test/test_option_lzma.c | 6 +----- cpio/test/test_option_u.c | 11 ++--------- cpio/test/test_option_y.c | 6 +----- cpio/test/test_option_z.c | 6 +----- cpio/test/test_passthrough_dotdot.c | 17 +++++++---------- cpio/test/test_passthrough_reverse.c | 17 +++++++---------- 8 files changed, 24 insertions(+), 59 deletions(-) diff --git a/cpio/test/test_option_a.c b/cpio/test/test_option_a.c index d1fe5ff0a..a597bd8d6 100644 --- a/cpio/test/test_option_a.c +++ b/cpio/test/test_option_a.c @@ -57,19 +57,15 @@ test_create(void) struct utimbuf times; static const int numfiles = sizeof(files) / sizeof(files[0]); int i; - int fd; for (i = 0; i < numfiles; ++i) { - fd = open(files[i].name, O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); /* * Note: Have to write at least one byte to the file. * cpio doesn't bother reading the file if it's zero length, * so the atime never gets changed in that case, which * makes the tests below rather pointless. */ - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile(files[i].name, 0644, "a"); /* If utime() isn't supported on your platform, just * #ifdef this section out. Most of the test below is @@ -97,16 +93,14 @@ DEFINE_TEST(test_option_a) { struct stat st; int r; - int f; - char buff[64]; + char *p; /* Create all of the test files. */ test_create(); /* Sanity check; verify that atimes really do get modified. */ - f = open(files[0].name, O_RDONLY); - assertEqualInt(1, read(f,buff, 1)); - assertEqualInt(0, close(f)); + assert((p = slurpfile(NULL, "f0")) != NULL); + free(p); assertEqualInt(0, stat("f0", &st)); if (st.st_atime == files[0].atime_sec) { skipping("Cannot verify -a option\n" diff --git a/cpio/test/test_option_l.c b/cpio/test/test_option_l.c index c6110fa02..81135d58d 100644 --- a/cpio/test/test_option_l.c +++ b/cpio/test/test_option_l.c @@ -28,14 +28,10 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_l) { struct stat st, st2; - int fd; int r; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Stat it. */ assertEqualInt(0, stat("f", &st)); diff --git a/cpio/test/test_option_lzma.c b/cpio/test/test_option_lzma.c index 132cba1da..c6e335301 100644 --- a/cpio/test/test_option_lzma.c +++ b/cpio/test/test_option_lzma.c @@ -28,15 +28,11 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_lzma) { char *p; - int fd; int r; size_t s; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Archive it with lzma compression. */ r = systemf("echo f | %s -o --lzma >archive.out 2>archive.err", diff --git a/cpio/test/test_option_u.c b/cpio/test/test_option_u.c index 7d2edfff5..769134cbd 100644 --- a/cpio/test/test_option_u.c +++ b/cpio/test/test_option_u.c @@ -35,14 +35,10 @@ DEFINE_TEST(test_option_u) struct utimbuf times; char *p; size_t s; - int fd; int r; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Copy the file to the "copy" dir. */ r = systemf("echo f | %s -pd copy >copy.out 2>copy.err", @@ -55,10 +51,7 @@ DEFINE_TEST(test_option_u) assertEqualMem(p, "a", 1); /* Recreate the file with a single "b" */ - fd = open("f", O_CREAT | O_TRUNC | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "b", 1)); - close(fd); + assertMakeFile("f", 0644, "b"); /* Set the mtime to the distant past. */ memset(×, 0, sizeof(times)); diff --git a/cpio/test/test_option_y.c b/cpio/test/test_option_y.c index a8a5936dc..58734966c 100644 --- a/cpio/test/test_option_y.c +++ b/cpio/test/test_option_y.c @@ -28,15 +28,11 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_option_y.c,v 1.2 2008/08/24 06:21 DEFINE_TEST(test_option_y) { char *p; - int fd; int r; size_t s; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Archive it with bzip2 compression. */ r = systemf("echo f | %s -oy >archive.out 2>archive.err", diff --git a/cpio/test/test_option_z.c b/cpio/test/test_option_z.c index c4bc480e9..91d37ac19 100644 --- a/cpio/test/test_option_z.c +++ b/cpio/test/test_option_z.c @@ -28,15 +28,11 @@ __FBSDID("$FreeBSD$"); DEFINE_TEST(test_option_z) { char *p; - int fd; int r; size_t s; /* Create a file. */ - fd = open("f", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(1, write(fd, "a", 1)); - close(fd); + assertMakeFile("f", 0644, "a"); /* Archive it with gzip compression. */ r = systemf("echo f | %s -oz >archive.out 2>archive.err", diff --git a/cpio/test/test_passthrough_dotdot.c b/cpio/test/test_passthrough_dotdot.c index 9dc8dbb0d..72f8562e3 100644 --- a/cpio/test/test_passthrough_dotdot.c +++ b/cpio/test/test_passthrough_dotdot.c @@ -32,31 +32,28 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_passthrough_dotdot.c,v 1.4 2008/0 DEFINE_TEST(test_passthrough_dotdot) { struct stat st; - int fd, r; - int filelist; + int r; + FILE *filelist; assertUmask(0); /* * Create an assortment of files on disk. */ - filelist = open("filelist", O_CREAT | O_WRONLY, 0644); + filelist = fopen("filelist", "w"); /* Directory. */ assertMakeDir("dir", 0755); assertChdir("dir"); - write(filelist, ".\n", 2); + fprintf(filelist, ".\n"); /* File with 10 bytes content. */ - fd = open("file", O_CREAT | O_WRONLY, 0642); - assert(fd >= 0); - assertEqualInt(10, write(fd, "123456789", 10)); - close(fd); - write(filelist, "file\n", 5); + assertMakeFile("file", 0642, "1234567890"); + fprintf(filelist, "file\n"); /* All done. */ - close(filelist); + fclose(filelist); /* diff --git a/cpio/test/test_passthrough_reverse.c b/cpio/test/test_passthrough_reverse.c index 25ba73b56..8a4a5193e 100644 --- a/cpio/test/test_passthrough_reverse.c +++ b/cpio/test/test_passthrough_reverse.c @@ -37,31 +37,28 @@ __FBSDID("$FreeBSD: src/usr.bin/cpio/test/test_passthrough_reverse.c,v 1.2 2008/ DEFINE_TEST(test_passthrough_reverse) { struct stat st; - int fd, r; - int filelist; + int r; + FILE *filelist; assertUmask(0); /* * Create an assortment of files on disk. */ - filelist = open("filelist", O_CREAT | O_WRONLY, 0644); + filelist = fopen("filelist", "w"); /* Directory. */ assertMakeDir("dir", 0743); /* File with 10 bytes content. */ - fd = open("dir/file", O_CREAT | O_WRONLY, 0644); - assert(fd >= 0); - assertEqualInt(10, write(fd, "123456789", 10)); - close(fd); - write(filelist, "dir/file\n", 9); + assertMakeFile("dir/file", 0644, "1234567890"); + fprintf(filelist, "dir/file\n"); /* Write dir last. */ - write(filelist, "dir\n", 4); + fprintf(filelist, "dir\n"); /* All done. */ - close(filelist); + fclose(filelist); /* -- 2.47.3