diff --git a/util/lar/lar.c b/util/lar/lar.c index fd8c942658..dd726704ad 100644 --- a/util/lar/lar.c +++ b/util/lar/lar.c @@ -130,8 +130,9 @@ int create_lar(const char *archivename, struct file *files) } for( ; files; files = files->next) { - if (lar_add_file(lar, files->name)) { - fprintf(stderr, "Error adding %s to the LAR.\n", files->name); + if (lar_add_file(lar, files)) { + fprintf(stderr, "Error adding %s:%s (%d) to the LAR.\n", + files->name, files->pathname, files->algo); lar_close_archive(lar); exit(1); } @@ -157,8 +158,9 @@ int add_lar(const char *archivename, struct file *files) } for( ; files; files = files->next) { - if (lar_add_file(lar, files->name)) { - fprintf(stderr, "Error adding %s to the LAR.\n", files->name); + if (lar_add_file(lar, files)) { + fprintf(stderr, "Error adding %s:%s (%d) to the LAR.\n", + files->name, files->pathname, files->algo); lar_close_archive(lar); exit(1); } @@ -377,7 +379,12 @@ int main(int argc, char *argv[]) while (optind < argc) { if (larmode == CREATE || larmode == ADD) { - add_files(argv[optind++]); + char *name=argv[optind++], *filename, *pathname; + enum compalgo file_algo=algo; + if (lar_process_name(name, &filename, + &pathname, &file_algo)) + exit(1); + add_files(filename,pathname, file_algo); } else add_file_or_directory(argv[optind++]); } diff --git a/util/lar/lib.c b/util/lar/lib.c index fd6154fc3e..2283753099 100644 --- a/util/lar/lib.c +++ b/util/lar/lib.c @@ -85,8 +85,8 @@ void do_no_uncompress(char *dst, int dst_len, char *src, int src_len) memcpy(dst, src, dst_len); else { - fprintf(stderr,"%s: src_len(%d)!=dst_len(%d)\n", - __FUNCTION__,src_len,dst_len); + fprintf(stderr, "%s: src_len(%d)!=dst_len(%d)\n", + __FUNCTION__, src_len, dst_len); exit(1); } } @@ -181,7 +181,8 @@ done: return ret; } -static int handle_directory(const char *name) +static int handle_directory(const char *name, const char *pathname, + const enum compalgo thisalgo) { int n; int ret = -1; @@ -193,23 +194,52 @@ static int handle_directory(const char *name) fprintf(stderr, "Could not enter directory %s\n", name); } else { while (n--) { - char fullname[MAX_PATH]; - - fullname[0] = '\0'; + char fullname[MAX_PATHLEN+1]; + char fullpathname[MAX_PATHLEN+1]; + int len; if (strncmp("..", namelist[n]->d_name, 3) && strncmp(".", namelist[n]->d_name, 2)) { - strncpy(fullname, name, MAX_PATH); + len = strlen(name); + len += (name[len-1]=='/' ? 1 : 0); + len += strlen(namelist[n]->d_name); - if (name[(strlen(name)) - 1] != '/') { - strncat(fullname, "/", MAX_PATH); + if (len > MAX_PATHLEN) { + fprintf(stderr, + "%s: %s+%s exceeds MAX_PATHLEN.\n", + __FUNCTION__, name, + namelist[n]->d_name); + return -1; } - strncat(fullname, namelist[n]->d_name, - MAX_PATH); + strcpy(fullname, name); + if (name[(strlen(name)) - 1] != '/') { + strcat(fullname, "/"); + } - add_files(fullname); + strcat(fullname, namelist[n]->d_name); + + len = strlen(pathname); + len += (pathname[len-1]=='/'?1:0); + len += strlen(namelist[n]->d_name); + + if (len > MAX_PATHLEN) { + fprintf(stderr, + "%s: %s+%s exceeds MAX_PATHLEN.\n", + __FUNCTION__, pathname, + namelist[n]->d_name); + return -1; + } + + strcpy(fullpathname, pathname); + if (pathname[(strlen(pathname)) - 1] != '/') { + strcat(fullpathname, "/"); + } + + strcat(fullpathname, namelist[n]->d_name); + + add_files(fullname, fullpathname, thisalgo); } free(namelist[n]); @@ -222,82 +252,80 @@ static int handle_directory(const char *name) } /* - * Add physically existing files to the file list. + * Add physically existing files to the file list. * This function is used when an archive is created or added to. */ -int add_files(const char *name) +int add_files(const char *filename, const char * pathname, + const enum compalgo thisalgo) { struct stat filestat; int ret = -1; - char *realname; char *c; - if (strstr(name, "nocompress:") == name) { - realname = strdup(name + 11); - } else { - realname = strdup(name); - } + if (verbose()) + printf("%s: %s:%s\n", __FUNCTION__, filename, pathname); - if (realname == NULL) { - fprintf(stderr, "Out of memory.\n"); - exit(1); - } - - /* printf("... add_files %s\n", name); */ - if (stat(realname, &filestat) == -1) { - fprintf(stderr, "Error getting file attributes of %s\n", name); - free(realname); + if (stat(filename, &filestat) == -1) { + fprintf(stderr, "Error getting file attributes of %s\n", filename); return -1; } if (S_ISCHR(filestat.st_mode) || S_ISBLK(filestat.st_mode)) { - fprintf(stderr, "Device files are not supported: %s\n", name); + fprintf(stderr, "Device files are not supported: %s\n", filename); } if (S_ISFIFO(filestat.st_mode)) { - fprintf(stderr, "FIFOs are not supported: %s\n", name); + fprintf(stderr, "FIFOs are not supported: %s\n", filename); } if (S_ISSOCK(filestat.st_mode)) { - fprintf(stderr, "Sockets are not supported: %s\n", name); + fprintf(stderr, "Sockets are not supported: %s\n", filename); } if (S_ISLNK(filestat.st_mode)) { - fprintf(stderr, "Symbolic links are not supported: %s\n", name); + fprintf(stderr, "Symbolic links are not supported: %s\n", filename); } - // Is it a directory? + /* Is it a directory? */ if (S_ISDIR(filestat.st_mode)) { - ret = handle_directory(realname); + ret = handle_directory(filename, pathname, thisalgo); } - // Is it a regular file? + /* Is it a regular file? */ if (S_ISREG(filestat.st_mode)) { struct file *tmpfile; - /* printf("... adding %s\n", name); */ + if (verbose()) + printf("... adding %s\n", filename); tmpfile = malloc(sizeof(struct file)); if (!tmpfile) { fprintf(stderr, "Out of memory.\n"); exit(1); } - tmpfile->name = strdup(name); + tmpfile->name = strdup(filename); if (!tmpfile->name) { fprintf(stderr, "Out of memory.\n"); exit(1); } + tmpfile->pathname = strdup(pathname); + if (!tmpfile->pathname) { + fprintf(stderr, "Out of memory.\n"); + exit(1); + } + + tmpfile->algo = thisalgo; + tmpfile->next = files; files = tmpfile; ret = 0; } - free(realname); return ret; } /* - * Add files or directories as specified to the file list. + * Add files or directories as specified to the file list. * This function is used when an archive is listed or extracted. */ @@ -318,6 +346,8 @@ int add_file_or_directory(const char *name) } tmpfile->next = files; + tmpfile->pathname = NULL; + tmpfile->algo = ALGO_INVALID; files = tmpfile; return 0; @@ -336,6 +366,8 @@ void free_files(void) temp = files; files = files->next; free(temp->name); + if (temp->pathname!=NULL) + free(temp->pathname); free(temp); } } @@ -347,7 +379,10 @@ int list_files(void) printf("File list:\n"); while (walk) { - printf("- %s\n", walk->name); + if (temp->pathname==NULL) + printf("- %s\n", walk->name); + else + printf("- %s:%s\n", walk->name, walk->pathname); walk = walk->next; } printf("-----\n"); diff --git a/util/lar/lib.h b/util/lar/lib.h index 6c201254b1..785c7fae66 100644 --- a/util/lar/lib.h +++ b/util/lar/lib.h @@ -25,6 +25,8 @@ struct file { char *name; + char *pathname; + enum compalgo algo; struct file *next; }; @@ -46,7 +48,8 @@ char *get_bootblock(void); /* prototypes for lib.c functions */ int mkdirp_below(const char *parent, const char *dirpath, mode_t mode); -int add_files(const char *name); +int add_files(const char *name, const char *pathname_in, + const enum compalgo algo_in); int add_file_or_directory(const char *name); struct file *get_files(void); @@ -71,7 +74,7 @@ struct lar * lar_open_archive(const char *archive); void lar_close_archive(struct lar *lar); void lar_list_files(struct lar *lar, struct file *files); -int lar_add_file(struct lar *lar, char *name); +int lar_add_file(struct lar *lar, struct file* file); int lar_add_bootblock(struct lar *lar, const char *bootblock); int lar_extract_files(struct lar *lar, struct file *files); diff --git a/util/lar/stream.c b/util/lar/stream.c index bb563aa389..d6dabd401a 100644 --- a/util/lar/stream.c +++ b/util/lar/stream.c @@ -929,28 +929,22 @@ int lar_add_entry(struct lar *lar, char *pathname, void *data, * @param name The name of the file to add * @return 0 on success, or -1 on failure */ -int lar_add_file(struct lar *lar, char *name) +int lar_add_file(struct lar *lar, struct file* file) { - char *filename, *ptr, *temp; - char *pathname; + char *ptr, *temp; - enum compalgo thisalgo; struct lar_header *header; int ret, hlen; u32 complen; - int pathlen; u32 size; - thisalgo = algo; - lar_process_name(name, &filename, &pathname, &thisalgo); - - ptr = mapfile(filename, &size); + ptr = mapfile(file->name, &size); if (ptr == MAP_FAILED) return -1; if (elfparse() && iself(ptr)) { - output_elf_segments(lar, pathname, ptr, size, thisalgo); + output_elf_segments(lar, file->pathname, ptr, size, file->algo); return 0; } @@ -969,11 +963,11 @@ int lar_add_file(struct lar *lar, char *name) return -1; } - complen = lar_compress(ptr, size, temp, &thisalgo); + complen = lar_compress(ptr, size, temp, &file->algo); munmap(ptr, size); - ret = lar_add_entry(lar, pathname, temp, complen, size, 0, 0, thisalgo); + ret = lar_add_entry(lar, file->pathname, temp, complen, size, 0, 0, file->algo); free(temp); return ret;