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@gmail.com>
Acked-by: Peter Stuge <peter@stuge.se>


git-svn-id: svn://coreboot.org/repository/coreboot-v3@623 f3766cd6-281f-0410-b1cd-43a5c92072e9
This commit is contained in:
Myles Watson 2008-02-28 14:56:43 +00:00
parent 775f57d4c3
commit 66843bacbf
4 changed files with 99 additions and 60 deletions

View file

@ -130,8 +130,9 @@ int create_lar(const char *archivename, struct file *files)
} }
for( ; files; files = files->next) { for( ; files; files = files->next) {
if (lar_add_file(lar, files->name)) { if (lar_add_file(lar, files)) {
fprintf(stderr, "Error adding %s to the LAR.\n", files->name); fprintf(stderr, "Error adding %s:%s (%d) to the LAR.\n",
files->name, files->pathname, files->algo);
lar_close_archive(lar); lar_close_archive(lar);
exit(1); exit(1);
} }
@ -157,8 +158,9 @@ int add_lar(const char *archivename, struct file *files)
} }
for( ; files; files = files->next) { for( ; files; files = files->next) {
if (lar_add_file(lar, files->name)) { if (lar_add_file(lar, files)) {
fprintf(stderr, "Error adding %s to the LAR.\n", files->name); fprintf(stderr, "Error adding %s:%s (%d) to the LAR.\n",
files->name, files->pathname, files->algo);
lar_close_archive(lar); lar_close_archive(lar);
exit(1); exit(1);
} }
@ -377,7 +379,12 @@ int main(int argc, char *argv[])
while (optind < argc) { while (optind < argc) {
if (larmode == CREATE || larmode == ADD) { 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 } else
add_file_or_directory(argv[optind++]); add_file_or_directory(argv[optind++]);
} }

View file

@ -85,8 +85,8 @@ void do_no_uncompress(char *dst, int dst_len, char *src, int src_len)
memcpy(dst, src, dst_len); memcpy(dst, src, dst_len);
else else
{ {
fprintf(stderr,"%s: src_len(%d)!=dst_len(%d)\n", fprintf(stderr, "%s: src_len(%d)!=dst_len(%d)\n",
__FUNCTION__,src_len,dst_len); __FUNCTION__, src_len, dst_len);
exit(1); exit(1);
} }
} }
@ -181,7 +181,8 @@ done:
return ret; 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 n;
int ret = -1; int ret = -1;
@ -193,23 +194,52 @@ static int handle_directory(const char *name)
fprintf(stderr, "Could not enter directory %s\n", name); fprintf(stderr, "Could not enter directory %s\n", name);
} else { } else {
while (n--) { while (n--) {
char fullname[MAX_PATH]; char fullname[MAX_PATHLEN+1];
char fullpathname[MAX_PATHLEN+1];
fullname[0] = '\0'; int len;
if (strncmp("..", namelist[n]->d_name, 3) && if (strncmp("..", namelist[n]->d_name, 3) &&
strncmp(".", namelist[n]->d_name, 2)) { 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] != '/') { if (len > MAX_PATHLEN) {
strncat(fullname, "/", MAX_PATH); fprintf(stderr,
"%s: %s+%s exceeds MAX_PATHLEN.\n",
__FUNCTION__, name,
namelist[n]->d_name);
return -1;
} }
strncat(fullname, namelist[n]->d_name, strcpy(fullname, name);
MAX_PATH); 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]); 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. * 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; struct stat filestat;
int ret = -1; int ret = -1;
char *realname;
char *c; char *c;
if (strstr(name, "nocompress:") == name) { if (verbose())
realname = strdup(name + 11); printf("%s: %s:%s\n", __FUNCTION__, filename, pathname);
} else {
realname = strdup(name);
}
if (realname == NULL) { if (stat(filename, &filestat) == -1) {
fprintf(stderr, "Out of memory.\n"); fprintf(stderr, "Error getting file attributes of %s\n", filename);
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);
return -1; return -1;
} }
if (S_ISCHR(filestat.st_mode) || S_ISBLK(filestat.st_mode)) { 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)) { 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)) { 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)) { 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)) { 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)) { if (S_ISREG(filestat.st_mode)) {
struct file *tmpfile; struct file *tmpfile;
/* printf("... adding %s\n", name); */ if (verbose())
printf("... adding %s\n", filename);
tmpfile = malloc(sizeof(struct file)); tmpfile = malloc(sizeof(struct file));
if (!tmpfile) { if (!tmpfile) {
fprintf(stderr, "Out of memory.\n"); fprintf(stderr, "Out of memory.\n");
exit(1); exit(1);
} }
tmpfile->name = strdup(name); tmpfile->name = strdup(filename);
if (!tmpfile->name) { if (!tmpfile->name) {
fprintf(stderr, "Out of memory.\n"); fprintf(stderr, "Out of memory.\n");
exit(1); exit(1);
} }
tmpfile->pathname = strdup(pathname);
if (!tmpfile->pathname) {
fprintf(stderr, "Out of memory.\n");
exit(1);
}
tmpfile->algo = thisalgo;
tmpfile->next = files; tmpfile->next = files;
files = tmpfile; files = tmpfile;
ret = 0; ret = 0;
} }
free(realname);
return ret; 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. * 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->next = files;
tmpfile->pathname = NULL;
tmpfile->algo = ALGO_INVALID;
files = tmpfile; files = tmpfile;
return 0; return 0;
@ -336,6 +366,8 @@ void free_files(void)
temp = files; temp = files;
files = files->next; files = files->next;
free(temp->name); free(temp->name);
if (temp->pathname!=NULL)
free(temp->pathname);
free(temp); free(temp);
} }
} }
@ -347,7 +379,10 @@ int list_files(void)
printf("File list:\n"); printf("File list:\n");
while (walk) { 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; walk = walk->next;
} }
printf("-----\n"); printf("-----\n");

View file

@ -25,6 +25,8 @@
struct file { struct file {
char *name; char *name;
char *pathname;
enum compalgo algo;
struct file *next; struct file *next;
}; };
@ -46,7 +48,8 @@ char *get_bootblock(void);
/* prototypes for lib.c functions */ /* prototypes for lib.c functions */
int mkdirp_below(const char *parent, const char *dirpath, mode_t mode); 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); int add_file_or_directory(const char *name);
struct file *get_files(void); 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_close_archive(struct lar *lar);
void lar_list_files(struct lar *lar, struct file *files); 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_add_bootblock(struct lar *lar, const char *bootblock);
int lar_extract_files(struct lar *lar, struct file *files); int lar_extract_files(struct lar *lar, struct file *files);

View file

@ -929,28 +929,22 @@ int lar_add_entry(struct lar *lar, char *pathname, void *data,
* @param name The name of the file to add * @param name The name of the file to add
* @return 0 on success, or -1 on failure * @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 *ptr, *temp;
char *pathname;
enum compalgo thisalgo;
struct lar_header *header; struct lar_header *header;
int ret, hlen; int ret, hlen;
u32 complen; u32 complen;
int pathlen;
u32 size; u32 size;
thisalgo = algo; ptr = mapfile(file->name, &size);
lar_process_name(name, &filename, &pathname, &thisalgo);
ptr = mapfile(filename, &size);
if (ptr == MAP_FAILED) if (ptr == MAP_FAILED)
return -1; return -1;
if (elfparse() && iself(ptr)) { if (elfparse() && iself(ptr)) {
output_elf_segments(lar, pathname, ptr, size, thisalgo); output_elf_segments(lar, file->pathname, ptr, size, file->algo);
return 0; return 0;
} }
@ -969,11 +963,11 @@ int lar_add_file(struct lar *lar, char *name)
return -1; return -1;
} }
complen = lar_compress(ptr, size, temp, &thisalgo); complen = lar_compress(ptr, size, temp, &file->algo);
munmap(ptr, size); 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); free(temp);
return ret; return ret;