Changeset View
Changeset View
Standalone View
Standalone View
usr.bin/ar/read.c
Show First 20 Lines • Show All 41 Lines • ▼ Show 20 Lines | |||||
#include <string.h> | #include <string.h> | ||||
#include "ar.h" | #include "ar.h" | ||||
/* | /* | ||||
* Handle read modes: 'x', 't' and 'p'. | * Handle read modes: 'x', 't' and 'p'. | ||||
*/ | */ | ||||
int | int | ||||
ar_read_archive(struct bsdar *bsdar, int mode) | ar_read_archive(struct bsdar *bsdar, int mode, FILE *out) | ||||
{ | { | ||||
struct archive *a; | struct archive *a; | ||||
struct archive_entry *entry; | struct archive_entry *entry; | ||||
struct stat sb; | struct stat sb; | ||||
struct tm *tp; | struct tm *tp; | ||||
const char *bname; | const char *bname; | ||||
const char *name; | const char *name; | ||||
mode_t md; | mode_t md; | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | for (;;) { | ||||
if (mode == 't') { | if (mode == 't') { | ||||
if (bsdar->options & AR_V) { | if (bsdar->options & AR_V) { | ||||
md = archive_entry_mode(entry); | md = archive_entry_mode(entry); | ||||
uid = archive_entry_uid(entry); | uid = archive_entry_uid(entry); | ||||
gid = archive_entry_gid(entry); | gid = archive_entry_gid(entry); | ||||
size = archive_entry_size(entry); | size = archive_entry_size(entry); | ||||
mtime = archive_entry_mtime(entry); | mtime = archive_entry_mtime(entry); | ||||
(void)strmode(md, buf); | (void)strmode(md, buf); | ||||
(void)fprintf(stdout, "%s %6d/%-6d %8ju ", | (void)fprintf(out, "%s %6d/%-6d %8ju ", | ||||
buf + 1, uid, gid, (uintmax_t)size); | buf + 1, uid, gid, (uintmax_t)size); | ||||
tp = localtime(&mtime); | tp = localtime(&mtime); | ||||
(void)strftime(buf, sizeof(buf), | (void)strftime(buf, sizeof(buf), | ||||
"%b %e %H:%M %Y", tp); | "%b %e %H:%M %Y", tp); | ||||
(void)fprintf(stdout, "%s %s", buf, name); | (void)fprintf(out, "%s %s", buf, name); | ||||
} else | } else | ||||
(void)fprintf(stdout, "%s", name); | (void)fprintf(out, "%s", name); | ||||
r = archive_read_data_skip(a); | r = archive_read_data_skip(a); | ||||
if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY || | if (r == ARCHIVE_WARN || r == ARCHIVE_RETRY || | ||||
r == ARCHIVE_FATAL) { | r == ARCHIVE_FATAL) { | ||||
(void)fprintf(stdout, "\n"); | (void)fprintf(out, "\n"); | ||||
bsdar_warnc(bsdar, archive_errno(a), "%s", | bsdar_warnc(bsdar, archive_errno(a), "%s", | ||||
archive_error_string(a)); | archive_error_string(a)); | ||||
} | } | ||||
if (r == ARCHIVE_FATAL) | if (r == ARCHIVE_FATAL) | ||||
break; | break; | ||||
(void)fprintf(stdout, "\n"); | (void)fprintf(out, "\n"); | ||||
} else { | } else { | ||||
/* mode == 'x' || mode = 'p' */ | /* mode == 'x' || mode = 'p' */ | ||||
if (mode == 'p') { | if (mode == 'p') { | ||||
if (bsdar->options & AR_V) { | if (bsdar->options & AR_V) { | ||||
(void)fprintf(stdout, "\n<%s>\n\n", | (void)fprintf(out, "\n<%s>\n\n", | ||||
name); | name); | ||||
fflush(stdout); | fflush(out); | ||||
} | } | ||||
r = archive_read_data_into_fd(a, 1); | r = archive_read_data_into_fd(a, 1); | ||||
} else { | } else { | ||||
/* mode == 'x' */ | /* mode == 'x' */ | ||||
if (stat(name, &sb) != 0) { | if (stat(name, &sb) != 0) { | ||||
if (errno != ENOENT) { | if (errno != ENOENT) { | ||||
bsdar_warnc(bsdar, 0, | bsdar_warnc(bsdar, 0, | ||||
"stat %s failed", | "stat %s failed", | ||||
bsdar->filename); | bsdar->filename); | ||||
continue; | continue; | ||||
} | } | ||||
} else { | } else { | ||||
/* stat success, file exist */ | /* stat success, file exist */ | ||||
if (bsdar->options & AR_CC) | if (bsdar->options & AR_CC) | ||||
continue; | continue; | ||||
if (bsdar->options & AR_U && | if (bsdar->options & AR_U && | ||||
archive_entry_mtime(entry) <= | archive_entry_mtime(entry) <= | ||||
sb.st_mtime) | sb.st_mtime) | ||||
continue; | continue; | ||||
} | } | ||||
if (bsdar->options & AR_V) | if (bsdar->options & AR_V) | ||||
(void)fprintf(stdout, "x - %s\n", name); | (void)fprintf(out, "x - %s\n", name); | ||||
/* Disallow absolute paths. */ | /* Disallow absolute paths. */ | ||||
if (name[0] == '/') { | if (name[0] == '/') { | ||||
bsdar_warnc(bsdar, 0, | bsdar_warnc(bsdar, 0, | ||||
"Absolute path '%s'", name); | "Absolute path '%s'", name); | ||||
continue; | continue; | ||||
} | } | ||||
/* Basic path security flags. */ | /* Basic path security flags. */ | ||||
flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS | | flags = ARCHIVE_EXTRACT_SECURE_SYMLINKS | | ||||
Show All 23 Lines |