Author: myles
Date: 2008-02-28 15:56:43 +0100 (Thu, 28 Feb 2008)
New Revision: 623
Modified:
coreboot-v3/util/lar/lar.c
coreboot-v3/util/lar/lib.c
coreboot-v3/util/lar/lib.h
coreboot-v3/util/lar/stream.c
Log:
This patch fixes lar path handling. In particular, it adds new members to the
file struct for pathname and compression, so that directories can be correctly
recursed.
file-by-file:
util/lar/lar.c:
make error messages more verbose
pass a pointer to the file structure instead of the name
parse the name here with lar_process_name
util/lar/lib.c:
change handle_directory to use a path name and respect nocompress
change add_files to use pre-processed names
use sensible defaults for new file members when listing or extracting
free pathname if allocated
util/lar/lib.h:
add new members to struct file
change prototypes of add_files and lar_add_file
util/lar/stream.c:
change lar_add_file to use pathname and compression from struct file
Signed-off-by: Myles Watson <mylesgw(a)gmail.com>
Acked-by: Peter Stuge <peter(a)stuge.se>
Modified: coreboot-v3/util/lar/lar.c
===================================================================
--- coreboot-v3/util/lar/lar.c 2008-02-27 05:27:51 UTC (rev 622)
+++ coreboot-v3/util/lar/lar.c 2008-02-28 14:56:43 UTC (rev 623)
@@ -130,8 +130,9 @@
}
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 @@
}
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 @@
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++]);
}
Modified: coreboot-v3/util/lar/lib.c
===================================================================
--- coreboot-v3/util/lar/lib.c 2008-02-27 05:27:51 UTC (rev 622)
+++ coreboot-v3/util/lar/lib.c 2008-02-28 14:56:43 UTC (rev 623)
@@ -85,8 +85,8 @@
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 @@
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 @@
fprintf(stderr, "Could not enter directory %s\n", name);
} else {
while (n--) {
- char fullname[MAX_PATH];
+ char fullname[MAX_PATHLEN+1];
+ char fullpathname[MAX_PATHLEN+1];
+ int len;
- fullname[0] = '\0';
-
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 (len > MAX_PATHLEN) {
+ fprintf(stderr,
+ "%s: %s+%s exceeds MAX_PATHLEN.\n",
+ __FUNCTION__, name,
+ namelist[n]->d_name);
+ return -1;
+ }
+
+ strcpy(fullname, name);
if (name[(strlen(name)) - 1] != '/') {
- strncat(fullname, "/", MAX_PATH);
+ strcat(fullname, "/");
}
- strncat(fullname, namelist[n]->d_name,
- MAX_PATH);
+ strcat(fullname, namelist[n]->d_name);
- add_files(fullname);
+ 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 @@
}
/*
- * 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 @@
}
tmpfile->next = files;
+ tmpfile->pathname = NULL;
+ tmpfile->algo = ALGO_INVALID;
files = tmpfile;
return 0;
@@ -336,6 +366,8 @@
temp = files;
files = files->next;
free(temp->name);
+ if (temp->pathname!=NULL)
+ free(temp->pathname);
free(temp);
}
}
@@ -347,7 +379,10 @@
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");
Modified: coreboot-v3/util/lar/lib.h
===================================================================
--- coreboot-v3/util/lar/lib.h 2008-02-27 05:27:51 UTC (rev 622)
+++ coreboot-v3/util/lar/lib.h 2008-02-28 14:56:43 UTC (rev 623)
@@ -25,6 +25,8 @@
struct file {
char *name;
+ char *pathname;
+ enum compalgo algo;
struct file *next;
};
@@ -46,7 +48,8 @@
/* 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 @@
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);
Modified: coreboot-v3/util/lar/stream.c
===================================================================
--- coreboot-v3/util/lar/stream.c 2008-02-27 05:27:51 UTC (rev 622)
+++ coreboot-v3/util/lar/stream.c 2008-02-28 14:56:43 UTC (rev 623)
@@ -929,28 +929,22 @@
* @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(file->name, &size);
- ptr = mapfile(filename, &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 @@
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;