copy_file_chunk uses streams now.

This commit is contained in:
Glenn L McGrath 2001-04-11 16:23:35 +00:00
parent 5b20d02ea9
commit 4949faf4b2
14 changed files with 269 additions and 274 deletions

View File

@ -237,16 +237,16 @@ LIBBB = libbb
LIBBB_LIB = libbb.a LIBBB_LIB = libbb.a
LIBBB_CSRC= ask_confirmation.c check_wildcard_match.c chomp.c \ LIBBB_CSRC= ask_confirmation.c check_wildcard_match.c chomp.c \
concat_path_file.c copy_file.c copy_file_chunk.c create_path.c daemon.c \ concat_path_file.c copy_file.c copy_file_chunk.c create_path.c daemon.c \
deb_extract.c device_open.c error_msg.c error_msg_and_die.c \ deb_extract.c device_open.c error_msg.c error_msg_and_die.c find_mount_point.c \
find_mount_point.c find_pid_by_name.c find_root_device.c full_read.c \ find_pid_by_name.c find_root_device.c full_read.c full_write.c \
full_write.c get_ar_headers.c get_console.c get_last_path_component.c \ get_ar_headers.c get_console.c get_last_path_component.c get_line_from_file.c \
get_line_from_file.c gz_open.c human_readable.c inode_hash.c \ gz_open.c human_readable.c inode_hash.c isdirectory.c kernel_version.c loop.c \
isdirectory.c kernel_version.c loop.c mode_string.c module_syscalls.c mtab.c \ mode_string.c module_syscalls.c mtab.c mtab_file.c my_getgrnam.c my_getgrgid.c \
mtab_file.c my_getgrnam.c my_getgrgid.c my_getpwnam.c my_getpwnamegid.c \ my_getpwnam.c my_getpwnamegid.c my_getpwuid.c parse_mode.c parse_number.c \
my_getpwuid.c parse_mode.c parse_number.c perror_msg.c perror_msg_and_die.c \ perror_msg.c perror_msg_and_die.c print_file.c process_escape_sequence.c \
print_file.c process_escape_sequence.c recursive_action.c safe_read.c \ recursive_action.c safe_read.c safe_strncpy.c seek_ared_file.c syscalls.c \
safe_strncpy.c syscalls.c syslog_msg_with_name.c time_string.c trim.c unzip.c \ syslog_msg_with_name.c time_string.c trim.c untar.c unzip.c vdprintf.c \
vdprintf.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c verror_msg.c vperror_msg.c wfopen.c xfuncs.c xgetcwd.c xregcomp.c
LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC)) LIBBB_OBJS=$(patsubst %.c,$(LIBBB)/%.o, $(LIBBB_CSRC))
LIBBB_CFLAGS = -I$(LIBBB) LIBBB_CFLAGS = -I$(LIBBB)

30
ar.c
View File

@ -36,12 +36,13 @@ extern int ar_main(int argc, char **argv)
const int extract_to_file = 8; /* extract contents of archive */ const int extract_to_file = 8; /* extract contents of archive */
const int extract_to_stdout = 16; /* extract to stdout */ const int extract_to_stdout = 16; /* extract to stdout */
FILE *src_file = NULL, *dst_file = NULL;
int funct = 0, opt=0; int funct = 0, opt=0;
int srcFd=0, dstFd=0;
ar_headers_t head, *extract_list=NULL; ar_headers_t *head, *extract_list=NULL;
extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
while ((opt = getopt(argc, argv, "ovtpx")) != -1) { while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
switch (opt) { switch (opt) {
@ -66,21 +67,22 @@ extern int ar_main(int argc, char **argv)
} }
/* check the src filename was specified */ /* check the src filename was specified */
if (optind == argc) if (optind == argc) {
show_usage(); show_usage();
}
if ( (srcFd = open(argv[optind], O_RDONLY)) < 0) if ( (src_file = wfopen(argv[optind], "r")) < 0) {
error_msg_and_die("Cannot read %s", argv[optind]); error_msg_and_die("Cannot read %s", argv[optind]);
}
optind++; optind++;
head = get_ar_headers(srcFd); head = get_ar_headers(src_file);
/* find files to extract or display */ /* find files to extract or display */
/* search through argv and build extract list */ /* search through argv and build extract list */
for (;optind < argc; optind++) { for (;optind < argc; optind++) {
ar_headers_t *ar_entry; ar_headers_t *ar_entry;
ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
ar_entry = &head; ar_entry = head;
while (ar_entry->next != NULL) { while (ar_entry->next != NULL) {
if (strcmp(argv[optind], ar_entry->name) == 0) { if (strcmp(argv[optind], ar_entry->name) == 0) {
ar_headers_t *tmp; ar_headers_t *tmp;
@ -96,20 +98,20 @@ extern int ar_main(int argc, char **argv)
/* if individual files not found extract all files */ /* if individual files not found extract all files */
if (extract_list->next==NULL) { if (extract_list->next==NULL) {
extract_list = &head; extract_list = head;
} }
/* find files to extract or display */ /* find files to extract or display */
while (extract_list->next != NULL) { while (extract_list->next != NULL) {
if (funct & extract_to_file) { if (funct & extract_to_file) {
dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode); dst_file = wfopen(extract_list->name, "w");
} }
else if (funct & extract_to_stdout) { else if (funct & extract_to_stdout) {
dstFd = fileno(stdout); dst_file = stdout;
} }
if ((funct & extract_to_file) || (funct & extract_to_stdout)) { if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
lseek(srcFd, extract_list->offset, SEEK_SET); fseek(src_file, extract_list->offset, SEEK_SET);
copy_file_chunk(srcFd, dstFd, (off_t) extract_list->size); copy_file_chunk(src_file, dst_file, (off_t) extract_list->size);
} }
if (funct & verbose) { if (funct & verbose) {
printf("%s %d/%d %8d %s ", mode_string(extract_list->mode), printf("%s %d/%d %8d %s ", mode_string(extract_list->mode),

View File

@ -36,12 +36,13 @@ extern int ar_main(int argc, char **argv)
const int extract_to_file = 8; /* extract contents of archive */ const int extract_to_file = 8; /* extract contents of archive */
const int extract_to_stdout = 16; /* extract to stdout */ const int extract_to_stdout = 16; /* extract to stdout */
FILE *src_file = NULL, *dst_file = NULL;
int funct = 0, opt=0; int funct = 0, opt=0;
int srcFd=0, dstFd=0;
ar_headers_t head, *extract_list=NULL; ar_headers_t *head, *extract_list=NULL;
extract_list = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); extract_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
head = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
while ((opt = getopt(argc, argv, "ovtpx")) != -1) { while ((opt = getopt(argc, argv, "ovtpx")) != -1) {
switch (opt) { switch (opt) {
@ -66,21 +67,22 @@ extern int ar_main(int argc, char **argv)
} }
/* check the src filename was specified */ /* check the src filename was specified */
if (optind == argc) if (optind == argc) {
show_usage(); show_usage();
}
if ( (srcFd = open(argv[optind], O_RDONLY)) < 0) if ( (src_file = wfopen(argv[optind], "r")) < 0) {
error_msg_and_die("Cannot read %s", argv[optind]); error_msg_and_die("Cannot read %s", argv[optind]);
}
optind++; optind++;
head = get_ar_headers(srcFd); head = get_ar_headers(src_file);
/* find files to extract or display */ /* find files to extract or display */
/* search through argv and build extract list */ /* search through argv and build extract list */
for (;optind < argc; optind++) { for (;optind < argc; optind++) {
ar_headers_t *ar_entry; ar_headers_t *ar_entry;
ar_entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
ar_entry = &head; ar_entry = head;
while (ar_entry->next != NULL) { while (ar_entry->next != NULL) {
if (strcmp(argv[optind], ar_entry->name) == 0) { if (strcmp(argv[optind], ar_entry->name) == 0) {
ar_headers_t *tmp; ar_headers_t *tmp;
@ -96,20 +98,20 @@ extern int ar_main(int argc, char **argv)
/* if individual files not found extract all files */ /* if individual files not found extract all files */
if (extract_list->next==NULL) { if (extract_list->next==NULL) {
extract_list = &head; extract_list = head;
} }
/* find files to extract or display */ /* find files to extract or display */
while (extract_list->next != NULL) { while (extract_list->next != NULL) {
if (funct & extract_to_file) { if (funct & extract_to_file) {
dstFd = open(extract_list->name, O_WRONLY | O_CREAT, extract_list->mode); dst_file = wfopen(extract_list->name, "w");
} }
else if (funct & extract_to_stdout) { else if (funct & extract_to_stdout) {
dstFd = fileno(stdout); dst_file = stdout;
} }
if ((funct & extract_to_file) || (funct & extract_to_stdout)) { if ((funct & extract_to_file) || (funct & extract_to_stdout)) {
lseek(srcFd, extract_list->offset, SEEK_SET); fseek(src_file, extract_list->offset, SEEK_SET);
copy_file_chunk(srcFd, dstFd, (off_t) extract_list->size); copy_file_chunk(src_file, dst_file, (off_t) extract_list->size);
} }
if (funct & verbose) { if (funct & verbose) {
printf("%s %d/%d %8d %s ", mode_string(extract_list->mode), printf("%s %d/%d %8d %s ", mode_string(extract_list->mode),

View File

@ -565,8 +565,8 @@ static int dpkg_dounpack(package_t *pkg)
int r = 0, i; int r = 0, i;
int status = TRUE; int status = TRUE;
char *cwd = xgetcwd(0); char *cwd = xgetcwd(0);
char *src_file = NULL; char *src_filename = NULL;
char *dst_file = NULL; char *dst_filename = NULL;
// char *lst_file = NULL; // char *lst_file = NULL;
char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst", char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
"conffiles", "md5sums", "shlibs", "templates" }; "conffiles", "md5sums", "shlibs", "templates" };
@ -576,37 +576,37 @@ static int dpkg_dounpack(package_t *pkg)
if(cwd==NULL) if(cwd==NULL)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
chdir("/"); chdir("/");
deb_extract(dpkg_deb_extract, "/", pkg->file); deb_extract(pkg->file, dpkg_deb_extract, "/");
/* Installs the package scripts into the info directory */ /* Installs the package scripts into the info directory */
for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) { for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
struct stat src_stat_buf; struct stat src_stat_buf;
int src_fd = 0, dst_fd = 0; FILE *src_file = NULL, *dst_file = NULL;
/* The full path of the current location of the admin file */ /* The full path of the current location of the admin file */
src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); src_filename = xrealloc(src_filename, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
sprintf(src_file, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]); sprintf(src_filename, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
/* the full path of where we want the file to be copied to */ /* the full path of where we want the file to be copied to */
dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); dst_filename = xrealloc(dst_filename, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
sprintf(dst_file, "%s%s.%s", infodir, pkg->package, adminscripts[i]); sprintf(dst_filename, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
/* /*
* copy admin file to permanent home * copy admin file to permanent home
* NOTE: Maybe merge this behaviour into libb/copy_file.c * NOTE: Maybe merge this behaviour into libb/copy_file.c
*/ */
if (lstat(src_file, &src_stat_buf) == 0) { if (lstat(src_filename, &src_stat_buf) == 0) {
if ((src_fd = open(src_file, O_RDONLY)) != -1) { if ((src_file = wfopen(src_filename, "r")) != NULL) {
if ((dst_fd = open(dst_file, O_WRONLY | O_CREAT, 0644)) == -1) { if ((dst_file = wfopen(dst_filename, "w")) == NULL) {
status = FALSE; status = FALSE;
perror_msg("Opening %s", dst_file); perror_msg("Opening %s", dst_filename);
} }
copy_file_chunk(src_fd, dst_fd, src_stat_buf.st_size); copy_file_chunk(src_file, dst_file, src_stat_buf.st_size);
close(src_fd); fclose(src_file);
close(dst_fd); fclose(dst_file);
} else { } else {
status = FALSE; status = FALSE;
error_msg("couldnt open [%s]\n", src_file); error_msg("couldnt open [%s]\n", src_filename);
} }
} }
} }
@ -671,7 +671,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
strcat(tmp_name, pkg->package); strcat(tmp_name, pkg->package);
/* extract control.tar.gz to the full extraction path */ /* extract control.tar.gz to the full extraction path */
deb_extract(dpkg_deb_control, tmp_name, pkg->file); deb_extract(pkg->file, dpkg_deb_control, tmp_name);
/* parse the extracted control file */ /* parse the extracted control file */
strcat(tmp_name, "/control"); strcat(tmp_name, "/control");

View File

@ -68,7 +68,7 @@ extern int dpkg_deb_main(int argc, char **argv)
strcpy(target_dir, argv[optind + 1]); strcpy(target_dir, argv[optind + 1]);
} }
} }
deb_extract(optflag, target_dir, argv[optind]); deb_extract(argv[optind], optflag, target_dir);
/* else if (optflag & dpkg_deb_info) { /* else if (optflag & dpkg_deb_info) {
extract_flag = TRUE; extract_flag = TRUE;
extract_to_stdout = TRUE; extract_to_stdout = TRUE;

View File

@ -147,7 +147,7 @@ cp_mv_Action(const char *fileName, struct stat *statbuf, void* junk)
add_to_ino_dev_hashtable(statbuf, destName); add_to_ino_dev_hashtable(statbuf, destName);
} }
} }
return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag); return copy_file(fileName, destName, preserveFlag, followLinks, forceFlag, FALSE);
} }
static int static int

34
dpkg.c
View File

@ -565,8 +565,8 @@ static int dpkg_dounpack(package_t *pkg)
int r = 0, i; int r = 0, i;
int status = TRUE; int status = TRUE;
char *cwd = xgetcwd(0); char *cwd = xgetcwd(0);
char *src_file = NULL; char *src_filename = NULL;
char *dst_file = NULL; char *dst_filename = NULL;
// char *lst_file = NULL; // char *lst_file = NULL;
char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst", char *adminscripts[] = { "prerm", "postrm", "preinst", "postinst",
"conffiles", "md5sums", "shlibs", "templates" }; "conffiles", "md5sums", "shlibs", "templates" };
@ -576,37 +576,37 @@ static int dpkg_dounpack(package_t *pkg)
if(cwd==NULL) if(cwd==NULL)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
chdir("/"); chdir("/");
deb_extract(dpkg_deb_extract, "/", pkg->file); deb_extract(pkg->file, dpkg_deb_extract, "/");
/* Installs the package scripts into the info directory */ /* Installs the package scripts into the info directory */
for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) { for (i = 0; i < sizeof(adminscripts) / sizeof(adminscripts[0]); i++) {
struct stat src_stat_buf; struct stat src_stat_buf;
int src_fd = 0, dst_fd = 0; FILE *src_file = NULL, *dst_file = NULL;
/* The full path of the current location of the admin file */ /* The full path of the current location of the admin file */
src_file = xrealloc(src_file, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); src_filename = xrealloc(src_filename, strlen(dpkgcidir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
sprintf(src_file, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]); sprintf(src_filename, "%s%s/%s", dpkgcidir, pkg->package, adminscripts[i]);
/* the full path of where we want the file to be copied to */ /* the full path of where we want the file to be copied to */
dst_file = xrealloc(dst_file, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1); dst_filename = xrealloc(dst_filename, strlen(infodir) + strlen(pkg->package) + strlen(adminscripts[i]) + 1);
sprintf(dst_file, "%s%s.%s", infodir, pkg->package, adminscripts[i]); sprintf(dst_filename, "%s%s.%s", infodir, pkg->package, adminscripts[i]);
/* /*
* copy admin file to permanent home * copy admin file to permanent home
* NOTE: Maybe merge this behaviour into libb/copy_file.c * NOTE: Maybe merge this behaviour into libb/copy_file.c
*/ */
if (lstat(src_file, &src_stat_buf) == 0) { if (lstat(src_filename, &src_stat_buf) == 0) {
if ((src_fd = open(src_file, O_RDONLY)) != -1) { if ((src_file = wfopen(src_filename, "r")) != NULL) {
if ((dst_fd = open(dst_file, O_WRONLY | O_CREAT, 0644)) == -1) { if ((dst_file = wfopen(dst_filename, "w")) == NULL) {
status = FALSE; status = FALSE;
perror_msg("Opening %s", dst_file); perror_msg("Opening %s", dst_filename);
} }
copy_file_chunk(src_fd, dst_fd, src_stat_buf.st_size); copy_file_chunk(src_file, dst_file, src_stat_buf.st_size);
close(src_fd); fclose(src_file);
close(dst_fd); fclose(dst_file);
} else { } else {
status = FALSE; status = FALSE;
error_msg("couldnt open [%s]\n", src_file); error_msg("couldnt open [%s]\n", src_filename);
} }
} }
} }
@ -671,7 +671,7 @@ static int dpkg_unpackcontrol(package_t *pkg)
strcat(tmp_name, pkg->package); strcat(tmp_name, pkg->package);
/* extract control.tar.gz to the full extraction path */ /* extract control.tar.gz to the full extraction path */
deb_extract(dpkg_deb_control, tmp_name, pkg->file); deb_extract(pkg->file, dpkg_deb_control, tmp_name);
/* parse the extracted control file */ /* parse the extracted control file */
strcat(tmp_name, "/control"); strcat(tmp_name, "/control");

View File

@ -68,7 +68,7 @@ extern int dpkg_deb_main(int argc, char **argv)
strcpy(target_dir, argv[optind + 1]); strcpy(target_dir, argv[optind + 1]);
} }
} }
deb_extract(optflag, target_dir, argv[optind]); deb_extract(argv[optind], optflag, target_dir);
/* else if (optflag & dpkg_deb_info) { /* else if (optflag & dpkg_deb_info) {
extract_flag = TRUE; extract_flag = TRUE;
extract_to_stdout = TRUE; extract_to_stdout = TRUE;

View File

@ -93,9 +93,9 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
void reset_ino_dev_hashtable(void); void reset_ino_dev_hashtable(void);
int copy_file(const char *srcName, const char *destName, int copy_file(const char *src_name, const char *dst_name,
int setModes, int followLinks, int forceFlag); int set_modes, int follow_links, int force_flag, int quiet_flag);
int copy_file_chunk(int srcFd, int dstFd, off_t remaining); int copy_file_chunk(FILE *src_file, FILE *dst_file, off_t chunksize);
char *buildName(const char *dirName, const char *fileName); char *buildName(const char *dirName, const char *fileName);
int makeString(int argc, const char **argv, char *buf, int bufLen); int makeString(int argc, const char **argv, char *buf, int bufLen);
char *getChunk(int size); char *getChunk(int size);
@ -225,10 +225,12 @@ typedef struct ar_headers_s {
off_t offset; off_t offset;
struct ar_headers_s *next; struct ar_headers_s *next;
} ar_headers_t; } ar_headers_t;
extern ar_headers_t get_ar_headers(int srcFd); extern ar_headers_t *get_ar_headers(FILE *in_file);
extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename); extern int seek_ared_file(FILE *in_file, ar_headers_t *headers, const char *tar_gz_file);
extern int deb_extract(const char *package_filename, const int function, char *target_dir);
extern int untar(FILE *src_tar_file, int untar_function, char *base_path);
extern int unzip(FILE *l_in_file, FILE *l_out_file); extern int unzip(FILE *l_in_file, FILE *l_out_file);
extern void gz_close(int gunzip_pid); extern void gz_close(int gunzip_pid);
extern int gz_open(FILE *compressed_file, int *pid); extern int gz_open(FILE *compressed_file, int *pid);
#endif /* __LIBBB_H__ */ #endif /* __LIBBB_H__ */

View File

@ -41,49 +41,54 @@
* -Erik Andersen * -Erik Andersen
*/ */
int int
copy_file(const char *srcName, const char *destName, copy_file(const char *src_name, const char *dst_name,
int setModes, int followLinks, int forceFlag) int set_modes, int follow_links, int force_flag, int quiet_flag)
{ {
int rfd; FILE *src_file = NULL;
int wfd; FILE *dst_file = NULL;
int status;
struct stat srcStatBuf; struct stat srcStatBuf;
struct stat dstStatBuf; struct stat dstStatBuf;
struct utimbuf times; struct utimbuf times;
int src_status;
int dst_status;
if (followLinks == TRUE) if (follow_links == TRUE) {
status = stat(srcName, &srcStatBuf); src_status = stat(src_name, &srcStatBuf);
else dst_status = stat(dst_name, &dstStatBuf);
status = lstat(srcName, &srcStatBuf); } else {
src_status = lstat(src_name, &srcStatBuf);
dst_status = lstat(dst_name, &dstStatBuf);
}
if (status < 0) { if (src_status < 0) {
perror_msg("%s", srcName); if (!quiet_flag) {
perror_msg("%s", src_name);
}
return FALSE; return FALSE;
} }
if (followLinks == TRUE) if ((dst_status < 0) || force_flag) {
status = stat(destName, &dstStatBuf); unlink(dst_name);
else
status = lstat(destName, &dstStatBuf);
if (status < 0 || forceFlag==TRUE) {
unlink(destName);
dstStatBuf.st_ino = -1; dstStatBuf.st_ino = -1;
dstStatBuf.st_dev = -1; dstStatBuf.st_dev = -1;
} }
if ((srcStatBuf.st_dev == dstStatBuf.st_dev) && if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
(srcStatBuf.st_ino == dstStatBuf.st_ino)) { (srcStatBuf.st_ino == dstStatBuf.st_ino)) {
error_msg("Copying file \"%s\" to itself", srcName); if (!quiet_flag) {
error_msg("Copying file \"%s\" to itself", src_name);
}
return FALSE; return FALSE;
} }
if (S_ISDIR(srcStatBuf.st_mode)) { if (S_ISDIR(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying directory %s to %s\n", srcName, destName); //fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
/* Make sure the directory is writable */ /* Make sure the directory is writable */
status = mkdir(destName, 0777777 ^ umask(0)); dst_status = create_path(dst_name, 0777777 ^ umask(0));
if (status < 0 && errno != EEXIST) { if ((dst_status < 0) && (errno != EEXIST)) {
perror_msg("%s", destName); if (!quiet_flag) {
perror_msg("%s", dst_name);
}
return FALSE; return FALSE;
} }
} else if (S_ISLNK(srcStatBuf.st_mode)) { } else if (S_ISLNK(srcStatBuf.st_mode)) {
@ -92,80 +97,92 @@ copy_file(const char *srcName, const char *destName,
//fprintf(stderr, "copying link %s to %s\n", srcName, destName); //fprintf(stderr, "copying link %s to %s\n", srcName, destName);
/* Warning: This could possibly truncate silently, to BUFSIZ chars */ /* Warning: This could possibly truncate silently, to BUFSIZ chars */
link_size = readlink(srcName, &link_val[0], BUFSIZ); link_size = readlink(src_name, &link_val[0], BUFSIZ);
if (link_size < 0) { if (link_size < 0) {
perror_msg("%s", srcName); if (quiet_flag) {
perror_msg("%s", src_name);
}
return FALSE; return FALSE;
} }
link_val[link_size] = '\0'; link_val[link_size] = '\0';
status = symlink(link_val, destName); src_status = symlink(link_val, dst_name);
if (status < 0) { if (src_status < 0) {
perror_msg("%s", destName); if (!quiet_flag) {
perror_msg("%s", dst_name);
}
return FALSE; return FALSE;
} }
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1) #if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
if (setModes == TRUE) { if (set_modes == TRUE) {
/* Try to set owner, but fail silently like GNU cp */ /* Try to set owner, but fail silently like GNU cp */
lchown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid); lchown(dst_name, srcStatBuf.st_uid, srcStatBuf.st_gid);
} }
#endif #endif
return TRUE; return TRUE;
} else if (S_ISFIFO(srcStatBuf.st_mode)) { } else if (S_ISFIFO(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying fifo %s to %s\n", srcName, destName); //fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
if (mkfifo(destName, 0644) < 0) { if (mkfifo(dst_name, 0644) < 0) {
perror_msg("%s", destName); if (!quiet_flag) {
perror_msg("%s", dst_name);
}
return FALSE; return FALSE;
} }
} else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode) } else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
|| S_ISSOCK(srcStatBuf.st_mode)) { || S_ISSOCK(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName); //fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) { if (mknod(dst_name, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
perror_msg("%s", destName); if (!quiet_flag) {
perror_msg("%s", dst_name);
}
return FALSE; return FALSE;
} }
} else if (S_ISREG(srcStatBuf.st_mode)) { } else if (S_ISREG(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying regular file %s to %s\n", srcName, destName); //fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
rfd = open(srcName, O_RDONLY); src_file = fopen(src_name, "r");
if (rfd < 0) { if (src_file == NULL) {
perror_msg("%s", srcName); if (!quiet_flag) {
perror_msg("%s", src_name);
}
return FALSE; return FALSE;
} }
wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC, dst_file = fopen(dst_name, "w");
srcStatBuf.st_mode); if (dst_file == NULL) {
if (wfd < 0) { if (!quiet_flag) {
perror_msg("%s", destName); perror_msg("%s", dst_name);
close(rfd); }
fclose(src_file);
return FALSE; return FALSE;
} }
if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE) if (copy_file_chunk(src_file, dst_file, srcStatBuf.st_size)==FALSE) {
goto error_exit; goto error_exit;
}
close(rfd); fclose(src_file);
if (close(wfd) < 0) { if (fclose(dst_file) < 0) {
return FALSE; return FALSE;
} }
} }
if (setModes == TRUE) { if (set_modes == TRUE) {
/* This is fine, since symlinks never get here */ /* This is fine, since symlinks never get here */
if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0) if (chown(dst_name, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
perror_msg_and_die("%s", destName); perror_msg("%s", dst_name);
if (chmod(destName, srcStatBuf.st_mode) < 0) if (chmod(dst_name, srcStatBuf.st_mode) < 0)
perror_msg_and_die("%s", destName); perror_msg("%s", dst_name);
times.actime = srcStatBuf.st_atime; times.actime = srcStatBuf.st_atime;
times.modtime = srcStatBuf.st_mtime; times.modtime = srcStatBuf.st_mtime;
if (utime(destName, &times) < 0) if (utime(dst_name, &times) < 0)
perror_msg_and_die("%s", destName); perror_msg("%s", dst_name);
} }
return TRUE; return TRUE;
error_exit: error_exit:
perror_msg("%s", destName); perror_msg("%s", dst_name);
close(rfd); fclose(src_file);
close(wfd); fclose(dst_file);
return FALSE; return FALSE;
} }

View File

@ -32,24 +32,29 @@
/* /*
* Copy chunksize bytes between two file descriptors * Copy chunksize bytes between two file descriptors
*/ */
int copy_file_chunk(int srcfd, int dstfd, off_t chunksize) extern int copy_file_chunk(FILE *src_file, FILE *dst_file, off_t chunksize)
{ {
off_t size; off_t size, amount_written;
char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */ char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
clearerr(src_file);
clearerr(dst_file);
while (chunksize > 0) { while (chunksize > 0) {
if (chunksize > BUFSIZ) if (chunksize > BUFSIZ) {
size = BUFSIZ; size = BUFSIZ;
else } else {
size = chunksize; size = chunksize;
if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size) }
amount_written = fwrite(buffer, 1, fread(buffer, 1, size, src_file), dst_file);
if (amount_written != size) {
error_msg("Couldnt write correct amount");
return(FALSE); return(FALSE);
chunksize -= size; }
chunksize -= amount_written;
} }
return (TRUE); return (TRUE);
} }
/* END CODE */ /* END CODE */
/* /*
Local Variables: Local Variables:

View File

@ -29,94 +29,53 @@
#include <signal.h> #include <signal.h>
#include "libbb.h" #include "libbb.h"
/* From gunzip.c */
extern int gz_open(FILE *compressed_file, int *pid);
extern void gz_close(int gunzip_pid);
extern int tar_unzip_init(int tarFd);
extern int readTarFile(int tarFd, int extractFlag, int listFlag,
int tostdoutFlag, int verboseFlag, char** extractList, char** excludeList);
extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename)
{
const int dpkg_deb_contents = 1; const int dpkg_deb_contents = 1;
const int dpkg_deb_control = 2; const int dpkg_deb_control = 2;
// const int dpkg_deb_info = 4; const int dpkg_deb_info = 4;
const int dpkg_deb_extract = 8; const int dpkg_deb_extract = 8;
const int dpkg_deb_verbose_extract = 16; const int dpkg_deb_verbose_extract = 16;
const int dpkg_deb_list = 32; const int dpkg_deb_list = 32;
char **extract_list = NULL; extern int deb_extract(const char *package_filename, const int function, char *target_dir)
ar_headers_t *ar_headers = NULL; {
char ar_filename[15];
int extract_flag = FALSE;
int list_flag = FALSE;
int verbose_flag = FALSE;
int extract_to_stdout = FALSE;
int srcFd = 0;
int status;
pid_t pid;
FILE *comp_file = NULL;
if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) { FILE *deb_file, *uncompressed_file;
strcpy(ar_filename, "data.tar.gz"); ar_headers_t *headers = NULL;
verbose_flag = TRUE; char *ared_file;
list_flag = TRUE; int gunzip_pid;
}
if (dpkg_deb_list == (dpkg_deb_list & optflags)) { if ((function == dpkg_deb_info) || (function == dpkg_deb_control)) {
strcpy(ar_filename, "data.tar.gz"); ared_file = xstrdup("control.tar.gz");
list_flag = TRUE; } else {
} ared_file = xstrdup("data.tar.gz");
if (dpkg_deb_control == (dpkg_deb_control & optflags)) {
strcpy(ar_filename, "control.tar.gz");
extract_flag = TRUE;
}
if (dpkg_deb_extract == (dpkg_deb_extract & optflags)) {
strcpy(ar_filename, "data.tar.gz");
extract_flag = TRUE;
}
if (dpkg_deb_verbose_extract == (dpkg_deb_verbose_extract & optflags)) {
strcpy(ar_filename, "data.tar.gz");
extract_flag = TRUE;
list_flag = TRUE;
} }
ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t)); /* open the debian package to be worked on */
srcFd = open(deb_filename, O_RDONLY); deb_file = wfopen(package_filename, "r");
*ar_headers = get_ar_headers(srcFd); headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
if (ar_headers->next == NULL) {
error_msg_and_die("Couldnt find %s in %s", ar_filename, deb_filename); /* get a linked list of all ar entries */
if ((headers = get_ar_headers(deb_file)) == NULL) {
error_msg("Couldnt get ar headers\n");
return(EXIT_FAILURE);
} }
while (ar_headers->next != NULL) { /* seek to the start of the .tar.gz file within the ar file*/
if (strcmp(ar_headers->name, ar_filename) == 0) { if (seek_ared_file(deb_file, headers, ared_file) == EXIT_FAILURE) {
break; error_msg("Couldnt seek to ar file");
} }
ar_headers = ar_headers->next;
} /* open a stream of decompressed data */
lseek(srcFd, ar_headers->offset, SEEK_SET); uncompressed_file = fdopen(gz_open(deb_file, &gunzip_pid), "r");
/* Uncompress the file */
comp_file = fdopen(srcFd, "r"); /* get a list of all tar headers inside the .gz file */
if ((srcFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) { untar(uncompressed_file, dpkg_deb_extract, target_dir);
error_msg_and_die("Couldnt unzip file");
}
if ( dir_name != NULL) {
if (is_directory(dir_name, TRUE, NULL)==FALSE) {
mkdir(dir_name, 0755);
}
if (chdir(dir_name)==-1) {
error_msg_and_die("Cannot change to dir %s", dir_name);
}
}
status = readTarFile(srcFd, extract_flag, list_flag,
extract_to_stdout, verbose_flag, NULL, extract_list);
/* we are deliberately terminating the child so we can safely ignore this */ /* we are deliberately terminating the child so we can safely ignore this */
signal(SIGTERM, SIG_IGN); signal(SIGTERM, SIG_IGN);
gz_close(pid); gz_close(gunzip_pid);
close(srcFd); fclose(deb_file);
fclose(comp_file); fclose(uncompressed_file);
return(EXIT_SUCCESS);
return status;
} }

View File

@ -30,7 +30,7 @@
#include <unistd.h> #include <unistd.h>
#include "libbb.h" #include "libbb.h"
extern ar_headers_t get_ar_headers(int srcFd) extern ar_headers_t *get_ar_headers(FILE *in_file)
{ {
typedef struct raw_ar_header_s { /* Byte Offset */ typedef struct raw_ar_header_s { /* Byte Offset */
char name[16]; /* 0-15 */ char name[16]; /* 0-15 */
@ -44,23 +44,25 @@ extern ar_headers_t get_ar_headers(int srcFd)
raw_ar_header_t raw_ar_header; raw_ar_header_t raw_ar_header;
ar_headers_t *head, *entry; ar_headers_t *ar_list, *ar_tmp, *ar_entry;
char ar_magic[8]; char ar_magic[8];
char *long_name=NULL; char *long_name=NULL;
head = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
/* check ar magic */ /* check ar magic */
if (full_read(srcFd, ar_magic, 8) != 8) { if (fread(ar_magic, 1, 8, in_file) != 8) {
error_msg_and_die("cannot read magic"); error_msg("cannot read magic");
return(NULL);
} }
if (strncmp(ar_magic,"!<arch>",7) != 0) { if (strncmp(ar_magic,"!<arch>",7) != 0) {
error_msg_and_die("invalid magic"); error_msg("invalid magic");
return(NULL);
} }
while (full_read(srcFd, (char *) &raw_ar_header, 60)==60) { ar_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
while (fread((char *) &raw_ar_header, 1, 60, in_file) == 60) {
ar_entry = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
/* check the end of header markers are valid */ /* check the end of header markers are valid */
if ((raw_ar_header.fmag[0] != '`') || (raw_ar_header.fmag[1] != '\n')) { if ((raw_ar_header.fmag[0] != '`') || (raw_ar_header.fmag[1] != '\n')) {
char newline; char newline;
@ -68,7 +70,7 @@ extern ar_headers_t get_ar_headers(int srcFd)
break; break;
} }
/* some version of ar, have an extra '\n' after each entry */ /* some version of ar, have an extra '\n' after each entry */
read(srcFd, &newline, 1); fread(&newline, 1, 1, in_file);
if (newline!='\n') { if (newline!='\n') {
break; break;
} }
@ -77,42 +79,46 @@ extern ar_headers_t get_ar_headers(int srcFd)
/* dont worry about adding the last '\n', we dont need it now */ /* dont worry about adding the last '\n', we dont need it now */
} }
entry->size = (off_t) atoi(raw_ar_header.size); ar_entry->size = (size_t) atoi(raw_ar_header.size);
/* long filenames have '/' as the first character */ /* long filenames have '/' as the first character */
if (raw_ar_header.name[0] == '/') { if (raw_ar_header.name[0] == '/') {
if (raw_ar_header.name[1] == '/') { if (raw_ar_header.name[1] == '/') {
/* multiple long filenames are stored as data in one entry */ /* multiple long filenames are stored as data in one entry */
long_name = (char *) xrealloc(long_name, entry->size); long_name = (char *) xrealloc(long_name, ar_entry->size);
full_read(srcFd, long_name, entry->size); fread(long_name, 1, ar_entry->size, in_file);
continue; continue;
} }
else { else {
/* The number after the '/' indicates the offset in the ar data section /* The number after the '/' indicates the offset in the ar data section
(saved in variable long_name) that conatains the real filename */ (saved in variable long_name) that conatains the real filename */
const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]); const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]);
entry->name = xmalloc(strlen(&long_name[long_name_offset])); ar_entry->name = xmalloc(strlen(&long_name[long_name_offset]));
strcpy(entry->name, &long_name[long_name_offset]); strcpy(ar_entry->name, &long_name[long_name_offset]);
} }
} }
else { else {
/* short filenames */ /* short filenames */
entry->name = xmalloc(16); ar_entry->name = xmalloc(16);
strncpy(entry->name, raw_ar_header.name, 16); ar_entry->name = strncpy(ar_entry->name, raw_ar_header.name, 16);
} }
entry->name[strcspn(entry->name, " /")]='\0'; ar_entry->name[strcspn(ar_entry->name, " /")]='\0';
/* convert the rest of the now valid char header to its typed struct */ /* convert the rest of the now valid char header to its typed struct */
parse_mode(raw_ar_header.mode, &entry->mode); parse_mode(raw_ar_header.mode, &ar_entry->mode);
entry->mtime = atoi(raw_ar_header.date); ar_entry->mtime = atoi(raw_ar_header.date);
entry->uid = atoi(raw_ar_header.uid); ar_entry->uid = atoi(raw_ar_header.uid);
entry->gid = atoi(raw_ar_header.gid); ar_entry->gid = atoi(raw_ar_header.gid);
entry->offset = lseek(srcFd, 0, SEEK_CUR); ar_entry->offset = ftell(in_file);
ar_entry->next = NULL;
/* add this entries header to our combined list */ fseek(in_file, (off_t) ar_entry->size, SEEK_CUR);
entry->next = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
*entry->next = *head; ar_tmp = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
*head = *entry; *ar_tmp = *ar_list;
lseek(srcFd, (off_t) entry->size, SEEK_CUR); *ar_list = *ar_entry;
free(ar_entry);
ar_list->next = ar_tmp;
} }
return(*head);
return(ar_list);
} }

View File

@ -93,9 +93,9 @@ int is_in_ino_dev_hashtable(const struct stat *statbuf, char **name);
void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name); void add_to_ino_dev_hashtable(const struct stat *statbuf, const char *name);
void reset_ino_dev_hashtable(void); void reset_ino_dev_hashtable(void);
int copy_file(const char *srcName, const char *destName, int copy_file(const char *src_name, const char *dst_name,
int setModes, int followLinks, int forceFlag); int set_modes, int follow_links, int force_flag, int quiet_flag);
int copy_file_chunk(int srcFd, int dstFd, off_t remaining); int copy_file_chunk(FILE *src_file, FILE *dst_file, off_t chunksize);
char *buildName(const char *dirName, const char *fileName); char *buildName(const char *dirName, const char *fileName);
int makeString(int argc, const char **argv, char *buf, int bufLen); int makeString(int argc, const char **argv, char *buf, int bufLen);
char *getChunk(int size); char *getChunk(int size);
@ -225,10 +225,12 @@ typedef struct ar_headers_s {
off_t offset; off_t offset;
struct ar_headers_s *next; struct ar_headers_s *next;
} ar_headers_t; } ar_headers_t;
extern ar_headers_t get_ar_headers(int srcFd); extern ar_headers_t *get_ar_headers(FILE *in_file);
extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename); extern int seek_ared_file(FILE *in_file, ar_headers_t *headers, const char *tar_gz_file);
extern int deb_extract(const char *package_filename, const int function, char *target_dir);
extern int untar(FILE *src_tar_file, int untar_function, char *base_path);
extern int unzip(FILE *l_in_file, FILE *l_out_file); extern int unzip(FILE *l_in_file, FILE *l_out_file);
extern void gz_close(int gunzip_pid); extern void gz_close(int gunzip_pid);
extern int gz_open(FILE *compressed_file, int *pid); extern int gz_open(FILE *compressed_file, int *pid);
#endif /* __LIBBB_H__ */ #endif /* __LIBBB_H__ */