diff options
author | Tim Kientzle <kientzle@gmail.com> | 2009-03-29 00:31:57 -0400 |
---|---|---|
committer | Tim Kientzle <kientzle@gmail.com> | 2009-03-29 00:31:57 -0400 |
commit | 854445c448659aa7549d616571b4cf9e244da083 (patch) | |
tree | 7202e6559bebda30d17bdb8e29e489bebee19d2f /libarchive/archive_write_open_filename.c | |
parent | e79064781dbbc31cbe8596c84675ac77d8c1a9da (diff) | |
download | libarchive-854445c448659aa7549d616571b4cf9e244da083.tar.gz |
Improve test coverage:
* Add new tests of archive_{read,write}_open_{fd,FILE,filename}
* Refactor the corresponding code to reduce the number of unreachable
cases and eliminate a lot of duplicated code. In particular,
{read,write}_open_filename now simply invoke {read,write}_open_fd to
handle handle the fallback case of reading stdin when called with an
empty filename.
* Eliminate the unnecessary "open" callbacks; "open" callbacks are
never needed (I plan to eliminate them from the API someday).
SVN-Revision: 868
Diffstat (limited to 'libarchive/archive_write_open_filename.c')
-rw-r--r-- | libarchive/archive_write_open_filename.c | 47 |
1 files changed, 15 insertions, 32 deletions
diff --git a/libarchive/archive_write_open_filename.c b/libarchive/archive_write_open_filename.c index 72eeb540..b9ae1747 100644 --- a/libarchive/archive_write_open_filename.c +++ b/libarchive/archive_write_open_filename.c @@ -71,24 +71,18 @@ archive_write_open_filename(struct archive *a, const char *filename) { struct write_file_data *mine; - if (filename == NULL || filename[0] == '\0') { - mine = (struct write_file_data *)malloc(sizeof(*mine)); - if (mine == NULL) { - archive_set_error(a, ENOMEM, "No memory"); - return (ARCHIVE_FATAL); - } - mine->filename[0] = '\0'; /* Record that we're using stdout. */ - } else { - mine = (struct write_file_data *)malloc(sizeof(*mine) + strlen(filename)); - if (mine == NULL) { - archive_set_error(a, ENOMEM, "No memory"); - return (ARCHIVE_FATAL); - } - strcpy(mine->filename, filename); + if (filename == NULL || filename[0] == '\0') + return (archive_write_open_fd(a, 0)); + + mine = (struct write_file_data *)malloc(sizeof(*mine) + strlen(filename)); + if (mine == NULL) { + archive_set_error(a, ENOMEM, "No memory"); + return (ARCHIVE_FATAL); } + strcpy(mine->filename, filename); mine->fd = -1; return (archive_write_open(a, mine, - file_open, file_write, file_close)); + file_open, file_write, file_close)); } static int @@ -104,21 +98,11 @@ file_open(struct archive *a, void *client_data) /* * Open the file. */ - if (mine->filename[0] != '\0') { - mine->fd = open(mine->filename, flags, 0666); - if (mine->fd < 0) { - archive_set_error(a, errno, "Failed to open '%s'", - mine->filename); - return (ARCHIVE_FATAL); - } - } else { - /* - * NULL filename is stdout. - */ - mine->fd = 1; - /* By default, pad archive when writing to stdout. */ - if (archive_write_get_bytes_in_last_block(a) < 0) - archive_write_set_bytes_in_last_block(a, 0); + mine->fd = open(mine->filename, flags, 0666); + if (mine->fd < 0) { + archive_set_error(a, errno, "Failed to open '%s'", + mine->filename); + return (ARCHIVE_FATAL); } if (fstat(mine->fd, &st) != 0) { @@ -172,8 +156,7 @@ file_close(struct archive *a, void *client_data) struct write_file_data *mine = (struct write_file_data *)client_data; (void)a; /* UNUSED */ - if (mine->filename[0] != '\0') - close(mine->fd); + close(mine->fd); free(mine); return (ARCHIVE_OK); } |