mirror of
https://github.com/fail0verflow/switch-coreboot.git
synced 2025-05-04 01:39:18 -04:00
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:
parent
775f57d4c3
commit
66843bacbf
4 changed files with 99 additions and 60 deletions
|
@ -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++]);
|
||||
}
|
||||
|
|
117
util/lar/lib.c
117
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");
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Add table
Reference in a new issue