summaryrefslogtreecommitdiff
path: root/libbb
diff options
context:
space:
mode:
Diffstat (limited to 'libbb')
-rw-r--r--libbb/copy_file.c133
-rw-r--r--libbb/copy_file_chunk.c35
-rw-r--r--libbb/deb_extract.c113
-rw-r--r--libbb/get_ar_headers.c70
-rw-r--r--libbb/libbb.h14
5 files changed, 177 insertions, 188 deletions
diff --git a/libbb/copy_file.c b/libbb/copy_file.c
index 64b65b9..eb9cb1a 100644
--- a/libbb/copy_file.c
+++ b/libbb/copy_file.c
@@ -41,49 +41,54 @@
* -Erik Andersen
*/
int
-copy_file(const char *srcName, const char *destName,
- int setModes, int followLinks, int forceFlag)
+copy_file(const char *src_name, const char *dst_name,
+ int set_modes, int follow_links, int force_flag, int quiet_flag)
{
- int rfd;
- int wfd;
- int status;
+ FILE *src_file = NULL;
+ FILE *dst_file = NULL;
struct stat srcStatBuf;
struct stat dstStatBuf;
struct utimbuf times;
+ int src_status;
+ int dst_status;
+
+ if (follow_links == TRUE) {
+ src_status = stat(src_name, &srcStatBuf);
+ dst_status = stat(dst_name, &dstStatBuf);
+ } else {
+ src_status = lstat(src_name, &srcStatBuf);
+ dst_status = lstat(dst_name, &dstStatBuf);
+ }
- if (followLinks == TRUE)
- status = stat(srcName, &srcStatBuf);
- else
- status = lstat(srcName, &srcStatBuf);
-
- if (status < 0) {
- perror_msg("%s", srcName);
+ if (src_status < 0) {
+ if (!quiet_flag) {
+ perror_msg("%s", src_name);
+ }
return FALSE;
}
- if (followLinks == TRUE)
- status = stat(destName, &dstStatBuf);
- else
- status = lstat(destName, &dstStatBuf);
-
- if (status < 0 || forceFlag==TRUE) {
- unlink(destName);
+ if ((dst_status < 0) || force_flag) {
+ unlink(dst_name);
dstStatBuf.st_ino = -1;
dstStatBuf.st_dev = -1;
}
if ((srcStatBuf.st_dev == dstStatBuf.st_dev) &&
(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;
}
if (S_ISDIR(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying directory %s to %s\n", srcName, destName);
/* Make sure the directory is writable */
- status = mkdir(destName, 0777777 ^ umask(0));
- if (status < 0 && errno != EEXIST) {
- perror_msg("%s", destName);
+ dst_status = create_path(dst_name, 0777777 ^ umask(0));
+ if ((dst_status < 0) && (errno != EEXIST)) {
+ if (!quiet_flag) {
+ perror_msg("%s", dst_name);
+ }
return FALSE;
}
} 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);
/* 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) {
- perror_msg("%s", srcName);
+ if (quiet_flag) {
+ perror_msg("%s", src_name);
+ }
return FALSE;
}
link_val[link_size] = '\0';
- status = symlink(link_val, destName);
- if (status < 0) {
- perror_msg("%s", destName);
+ src_status = symlink(link_val, dst_name);
+ if (src_status < 0) {
+ if (!quiet_flag) {
+ perror_msg("%s", dst_name);
+ }
return FALSE;
}
#if (__GLIBC__ >= 2) && (__GLIBC_MINOR__ >= 1)
- if (setModes == TRUE) {
+ if (set_modes == TRUE) {
/* 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
return TRUE;
} else if (S_ISFIFO(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying fifo %s to %s\n", srcName, destName);
- if (mkfifo(destName, 0644) < 0) {
- perror_msg("%s", destName);
+ if (mkfifo(dst_name, 0644) < 0) {
+ if (!quiet_flag) {
+ perror_msg("%s", dst_name);
+ }
return FALSE;
}
} else if (S_ISBLK(srcStatBuf.st_mode) || S_ISCHR(srcStatBuf.st_mode)
|| S_ISSOCK(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying soc, blk, or chr %s to %s\n", srcName, destName);
- if (mknod(destName, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
- perror_msg("%s", destName);
+ if (mknod(dst_name, srcStatBuf.st_mode, srcStatBuf.st_rdev) < 0) {
+ if (!quiet_flag) {
+ perror_msg("%s", dst_name);
+ }
return FALSE;
}
} else if (S_ISREG(srcStatBuf.st_mode)) {
//fprintf(stderr, "copying regular file %s to %s\n", srcName, destName);
- rfd = open(srcName, O_RDONLY);
- if (rfd < 0) {
- perror_msg("%s", srcName);
+ src_file = fopen(src_name, "r");
+ if (src_file == NULL) {
+ if (!quiet_flag) {
+ perror_msg("%s", src_name);
+ }
return FALSE;
}
- wfd = open(destName, O_WRONLY | O_CREAT | O_TRUNC,
- srcStatBuf.st_mode);
- if (wfd < 0) {
- perror_msg("%s", destName);
- close(rfd);
+ dst_file = fopen(dst_name, "w");
+ if (dst_file == NULL) {
+ if (!quiet_flag) {
+ perror_msg("%s", dst_name);
+ }
+ fclose(src_file);
return FALSE;
}
- if (copy_file_chunk(rfd, wfd, srcStatBuf.st_size)==FALSE)
- goto error_exit;
-
- close(rfd);
- if (close(wfd) < 0) {
+ if (copy_file_chunk(src_file, dst_file, srcStatBuf.st_size)==FALSE) {
+ goto error_exit;
+ }
+
+ fclose(src_file);
+ if (fclose(dst_file) < 0) {
return FALSE;
}
}
- if (setModes == TRUE) {
+ if (set_modes == TRUE) {
/* This is fine, since symlinks never get here */
- if (chown(destName, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
- perror_msg_and_die("%s", destName);
- if (chmod(destName, srcStatBuf.st_mode) < 0)
- perror_msg_and_die("%s", destName);
+ if (chown(dst_name, srcStatBuf.st_uid, srcStatBuf.st_gid) < 0)
+ perror_msg("%s", dst_name);
+ if (chmod(dst_name, srcStatBuf.st_mode) < 0)
+ perror_msg("%s", dst_name);
times.actime = srcStatBuf.st_atime;
times.modtime = srcStatBuf.st_mtime;
- if (utime(destName, &times) < 0)
- perror_msg_and_die("%s", destName);
+ if (utime(dst_name, &times) < 0)
+ perror_msg("%s", dst_name);
}
return TRUE;
- error_exit:
- perror_msg("%s", destName);
- close(rfd);
- close(wfd);
+error_exit:
+ perror_msg("%s", dst_name);
+ fclose(src_file);
+ fclose(dst_file);
return FALSE;
}
diff --git a/libbb/copy_file_chunk.c b/libbb/copy_file_chunk.c
index bf0b06e..e887cbe 100644
--- a/libbb/copy_file_chunk.c
+++ b/libbb/copy_file_chunk.c
@@ -32,24 +32,29 @@
/*
* 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;
- char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
-
- while (chunksize > 0) {
- if (chunksize > BUFSIZ)
- size = BUFSIZ;
- else
- size = chunksize;
- if (full_write(dstfd, buffer, full_read(srcfd, buffer, size)) < size)
- return(FALSE);
- chunksize -= size;
- }
- return (TRUE);
+ off_t size, amount_written;
+ char buffer[BUFSIZ]; /* BUFSIZ is declared in stdio.h */
+
+ clearerr(src_file);
+ clearerr(dst_file);
+ while (chunksize > 0) {
+ if (chunksize > BUFSIZ) {
+ size = BUFSIZ;
+ } else {
+ size = chunksize;
+ }
+ 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);
+ }
+ chunksize -= amount_written;
+ }
+ return (TRUE);
}
-
/* END CODE */
/*
Local Variables:
diff --git a/libbb/deb_extract.c b/libbb/deb_extract.c
index 358c4d0..35ff996 100644
--- a/libbb/deb_extract.c
+++ b/libbb/deb_extract.c
@@ -29,94 +29,53 @@
#include <signal.h>
#include "libbb.h"
-/* From gunzip.c */
-extern int gz_open(FILE *compressed_file, int *pid);
-extern void gz_close(int gunzip_pid);
+const int dpkg_deb_contents = 1;
+const int dpkg_deb_control = 2;
+const int dpkg_deb_info = 4;
+const int dpkg_deb_extract = 8;
+const int dpkg_deb_verbose_extract = 16;
+const int dpkg_deb_list = 32;
-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)
+extern int deb_extract(const char *package_filename, const int function, char *target_dir)
{
- const int dpkg_deb_contents = 1;
- const int dpkg_deb_control = 2;
-// const int dpkg_deb_info = 4;
- const int dpkg_deb_extract = 8;
- const int dpkg_deb_verbose_extract = 16;
- const int dpkg_deb_list = 32;
- char **extract_list = NULL;
- 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;
+ FILE *deb_file, *uncompressed_file;
+ ar_headers_t *headers = NULL;
+ char *ared_file;
+ int gunzip_pid;
- if (dpkg_deb_contents == (dpkg_deb_contents & optflags)) {
- strcpy(ar_filename, "data.tar.gz");
- verbose_flag = TRUE;
- list_flag = TRUE;
- }
- if (dpkg_deb_list == (dpkg_deb_list & optflags)) {
- strcpy(ar_filename, "data.tar.gz");
- list_flag = TRUE;
- }
- 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;
+ if ((function == dpkg_deb_info) || (function == dpkg_deb_control)) {
+ ared_file = xstrdup("control.tar.gz");
+ } else {
+ ared_file = xstrdup("data.tar.gz");
}
- ar_headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
- srcFd = open(deb_filename, O_RDONLY);
+ /* open the debian package to be worked on */
+ deb_file = wfopen(package_filename, "r");
+
+ headers = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
- *ar_headers = get_ar_headers(srcFd);
- 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) {
- if (strcmp(ar_headers->name, ar_filename) == 0) {
- break;
- }
- ar_headers = ar_headers->next;
+ /* seek to the start of the .tar.gz file within the ar file*/
+ if (seek_ared_file(deb_file, headers, ared_file) == EXIT_FAILURE) {
+ error_msg("Couldnt seek to ar file");
}
- lseek(srcFd, ar_headers->offset, SEEK_SET);
- /* Uncompress the file */
- comp_file = fdopen(srcFd, "r");
- if ((srcFd = gz_open(comp_file, &pid)) == EXIT_FAILURE) {
- 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);
+
+ /* open a stream of decompressed data */
+ uncompressed_file = fdopen(gz_open(deb_file, &gunzip_pid), "r");
+
+ /* get a list of all tar headers inside the .gz file */
+ untar(uncompressed_file, dpkg_deb_extract, target_dir);
/* we are deliberately terminating the child so we can safely ignore this */
signal(SIGTERM, SIG_IGN);
- gz_close(pid);
- close(srcFd);
- fclose(comp_file);
-
- return status;
+ gz_close(gunzip_pid);
+ fclose(deb_file);
+ fclose(uncompressed_file);
+ return(EXIT_SUCCESS);
} \ No newline at end of file
diff --git a/libbb/get_ar_headers.c b/libbb/get_ar_headers.c
index cf7de5c..151caaf 100644
--- a/libbb/get_ar_headers.c
+++ b/libbb/get_ar_headers.c
@@ -30,7 +30,7 @@
#include <unistd.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 */
char name[16]; /* 0-15 */
@@ -44,31 +44,33 @@ extern ar_headers_t get_ar_headers(int srcFd)
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 *long_name=NULL;
- head = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
- entry = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
-
/* check ar magic */
- if (full_read(srcFd, ar_magic, 8) != 8) {
- error_msg_and_die("cannot read magic");
+ if (fread(ar_magic, 1, 8, in_file) != 8) {
+ error_msg("cannot read magic");
+ return(NULL);
}
if (strncmp(ar_magic,"!<arch>",7) != 0) {
- error_msg_and_die("invalid magic");
+ error_msg("invalid magic");
+ return(NULL);
}
+
+ ar_list = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
- while (full_read(srcFd, (char *) &raw_ar_header, 60)==60) {
+ 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 */
- 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;
- if (raw_ar_header.fmag[1]!='`') {
+ if (raw_ar_header.fmag[1] != '`') {
break;
}
/* some version of ar, have an extra '\n' after each entry */
- read(srcFd, &newline, 1);
+ fread(&newline, 1, 1, in_file);
if (newline!='\n') {
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 */
}
- 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 */
if (raw_ar_header.name[0] == '/') {
if (raw_ar_header.name[1] == '/') {
/* multiple long filenames are stored as data in one entry */
- long_name = (char *) xrealloc(long_name, entry->size);
- full_read(srcFd, long_name, entry->size);
+ long_name = (char *) xrealloc(long_name, ar_entry->size);
+ fread(long_name, 1, ar_entry->size, in_file);
continue;
}
else {
/* The number after the '/' indicates the offset in the ar data section
(saved in variable long_name) that conatains the real filename */
const int long_name_offset = (int) atoi((char *) &raw_ar_header.name[1]);
- entry->name = xmalloc(strlen(&long_name[long_name_offset]));
- strcpy(entry->name, &long_name[long_name_offset]);
+ ar_entry->name = xmalloc(strlen(&long_name[long_name_offset]));
+ strcpy(ar_entry->name, &long_name[long_name_offset]);
}
}
else {
/* short filenames */
- entry->name = xmalloc(16);
- strncpy(entry->name, raw_ar_header.name, 16);
+ ar_entry->name = xmalloc(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 */
- parse_mode(raw_ar_header.mode, &entry->mode);
- entry->mtime = atoi(raw_ar_header.date);
- entry->uid = atoi(raw_ar_header.uid);
- entry->gid = atoi(raw_ar_header.gid);
- entry->offset = lseek(srcFd, 0, SEEK_CUR);
+ parse_mode(raw_ar_header.mode, &ar_entry->mode);
+ ar_entry->mtime = atoi(raw_ar_header.date);
+ ar_entry->uid = atoi(raw_ar_header.uid);
+ ar_entry->gid = atoi(raw_ar_header.gid);
+ ar_entry->offset = ftell(in_file);
+ ar_entry->next = NULL;
- /* add this entries header to our combined list */
- entry->next = (ar_headers_t *) xmalloc(sizeof(ar_headers_t));
- *entry->next = *head;
- *head = *entry;
- lseek(srcFd, (off_t) entry->size, SEEK_CUR);
+ fseek(in_file, (off_t) ar_entry->size, SEEK_CUR);
+
+ ar_tmp = (ar_headers_t *) xcalloc(1, sizeof(ar_headers_t));
+ *ar_tmp = *ar_list;
+ *ar_list = *ar_entry;
+ free(ar_entry);
+ ar_list->next = ar_tmp;
}
- return(*head);
-} \ No newline at end of file
+
+ return(ar_list);
+}
diff --git a/libbb/libbb.h b/libbb/libbb.h
index 279b4e7..54e8525 100644
--- a/libbb/libbb.h
+++ b/libbb/libbb.h
@@ -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 reset_ino_dev_hashtable(void);
-int copy_file(const char *srcName, const char *destName,
- int setModes, int followLinks, int forceFlag);
-int copy_file_chunk(int srcFd, int dstFd, off_t remaining);
+int copy_file(const char *src_name, const char *dst_name,
+ int set_modes, int follow_links, int force_flag, int quiet_flag);
+int copy_file_chunk(FILE *src_file, FILE *dst_file, off_t chunksize);
char *buildName(const char *dirName, const char *fileName);
int makeString(int argc, const char **argv, char *buf, int bufLen);
char *getChunk(int size);
@@ -225,10 +225,12 @@ typedef struct ar_headers_s {
off_t offset;
struct ar_headers_s *next;
} ar_headers_t;
-extern ar_headers_t get_ar_headers(int srcFd);
-extern int deb_extract(int optflags, const char *dir_name, const char *deb_filename);
-
+extern ar_headers_t *get_ar_headers(FILE *in_file);
+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 void gz_close(int gunzip_pid);
extern int gz_open(FILE *compressed_file, int *pid);
+
#endif /* __LIBBB_H__ */