diff options
author | kientzle <kientzle@FreeBSD.org> | 2009-04-17 00:50:00 +0000 |
---|---|---|
committer | kientzle <kientzle@FreeBSD.org> | 2009-04-17 00:50:00 +0000 |
commit | ef74aa99b9472fb8e958a399ff6b19fd337b724d (patch) | |
tree | c31e0bf4145168c53a8e70873f148e6872051759 /lib/libarchive/archive_read_open_fd.c | |
parent | 5387456c70c14b686b55a811d0f071abd3a49fd8 (diff) | |
download | FreeBSD-src-ef74aa99b9472fb8e958a399ff6b19fd337b724d.zip FreeBSD-src-ef74aa99b9472fb8e958a399ff6b19fd337b724d.tar.gz |
Don't use the open callback, which is deprecated (because it's
never necessary). Also, simplify just a tad by delegating
to read_open_fd() when we know the file descriptor, instead
of duplicating that logic.
Diffstat (limited to 'lib/libarchive/archive_read_open_fd.c')
-rw-r--r-- | lib/libarchive/archive_read_open_fd.c | 59 |
1 files changed, 24 insertions, 35 deletions
diff --git a/lib/libarchive/archive_read_open_fd.c b/lib/libarchive/archive_read_open_fd.c index 13afe63..a7c2e29 100644 --- a/lib/libarchive/archive_read_open_fd.c +++ b/lib/libarchive/archive_read_open_fd.c @@ -52,7 +52,6 @@ struct read_fd_data { }; static int file_close(struct archive *, void *); -static int file_open(struct archive *, void *); static ssize_t file_read(struct archive *, void *, const void **buff); #if ARCHIVE_API_VERSION < 2 static ssize_t file_skip(struct archive *, void *, size_t request); @@ -63,50 +62,41 @@ static off_t file_skip(struct archive *, void *, off_t request); int archive_read_open_fd(struct archive *a, int fd, size_t block_size) { + struct stat st; struct read_fd_data *mine; + void *b; - mine = (struct read_fd_data *)malloc(sizeof(*mine)); - if (mine == NULL) { - archive_set_error(a, ENOMEM, "No memory"); + if (fstat(fd, &st) != 0) { + archive_set_error(a, errno, "Can't stat fd %d", fd); return (ARCHIVE_FATAL); } - mine->block_size = block_size; - mine->buffer = malloc(mine->block_size); - if (mine->buffer == NULL) { + + mine = (struct read_fd_data *)malloc(sizeof(*mine)); + b = malloc(block_size); + if (mine == NULL || b == NULL) { archive_set_error(a, ENOMEM, "No memory"); free(mine); + free(b); return (ARCHIVE_FATAL); } + mine->block_size = block_size; + mine->buffer = b; mine->fd = fd; - /* lseek() hardly ever works, so disable it by default. See below. */ - mine->can_skip = 0; - return (archive_read_open2(a, mine, file_open, file_read, file_skip, file_close)); -} - -static int -file_open(struct archive *a, void *client_data) -{ - struct read_fd_data *mine = (struct read_fd_data *)client_data; - struct stat st; - - if (fstat(mine->fd, &st) != 0) { - archive_set_error(a, errno, "Can't stat fd %d", mine->fd); - return (ARCHIVE_FATAL); - } - + /* + * Skip support is a performance optimization for anything + * that supports lseek(). On FreeBSD, only regular files and + * raw disk devices support lseek() and there's no portable + * way to determine if a device is a raw disk device, so we + * only enable this optimization for regular files. + */ if (S_ISREG(st.st_mode)) { archive_read_extract_set_skip_file(a, st.st_dev, st.st_ino); - /* - * Enabling skip here is a performance optimization for - * anything that supports lseek(). On FreeBSD, only - * regular files and raw disk devices support lseek() and - * there's no portable way to determine if a device is - * a raw disk device, so we only enable this optimization - * for regular files. - */ mine->can_skip = 1; - } - return (ARCHIVE_OK); + } else + mine->can_skip = 0; + + return (archive_read_open2(a, mine, + NULL, file_read, file_skip, file_close)); } static ssize_t @@ -180,8 +170,7 @@ file_close(struct archive *a, void *client_data) struct read_fd_data *mine = (struct read_fd_data *)client_data; (void)a; /* UNUSED */ - if (mine->buffer != NULL) - free(mine->buffer); + free(mine->buffer); free(mine); return (ARCHIVE_OK); } |