diff options
author | Jo-Philipp Wich <jow@openwrt.org> | 2009-04-06 14:28:17 +0000 |
---|---|---|
committer | Jo-Philipp Wich <jow@openwrt.org> | 2009-04-06 14:28:17 +0000 |
commit | 50ccdfccce792d5bd013cae9eb93588a16a7ec62 (patch) | |
tree | 4476042099bc0933c82f9339ef50533af41bc5ca /contrib/lar | |
parent | 1744f962b0c0d324aa9a5ebbbcfb4f98e312a217 (diff) |
contrib/lar:
- add lar_find_archive() and lar_find_member() to get a member handle from a packagename
- extend cli to cover new operations
Diffstat (limited to 'contrib/lar')
-rw-r--r-- | contrib/lar/cli.c | 62 | ||||
-rw-r--r-- | contrib/lar/lar.c | 54 | ||||
-rw-r--r-- | contrib/lar/lar.h | 2 |
3 files changed, 100 insertions, 18 deletions
diff --git a/contrib/lar/cli.c b/contrib/lar/cli.c index 2b5e08891..e8daf63a5 100644 --- a/contrib/lar/cli.c +++ b/contrib/lar/cli.c @@ -27,7 +27,28 @@ int do_print_index( lar_archive *ar ) index = index->next; } - return 0; + return 0; +} + +int do_require( const char *package ) +{ + int stat = 1; + lar_archive *ar; + lar_member *mb; + + if( (ar = lar_find_archive(package)) != NULL ) + { + if( (mb = lar_find_member(ar, package)) != NULL ) + { + write(fileno(stdout), mb->data, mb->length); + lar_close_member(mb); + stat = 0; + } + + lar_close(ar); + } + + return stat; } int main( int argc, const char* argv[] ) @@ -37,27 +58,40 @@ int main( int argc, const char* argv[] ) if( argv[1] != NULL ) { - if( (ar = lar_open(argv[1])) != NULL ) + switch(argv[1][0]) { - if( argv[2] ) - stat = do_print_member(ar, argv[2]); - else - stat = do_print_index(ar); + case 's': + if( (ar = lar_open(argv[2])) != NULL ) + { + if( argv[3] != NULL ) + stat = do_print_member(ar, argv[3]); + else + stat = do_print_index(ar); - lar_close(ar); - return stat; - } - else - { - LAR_DIE("Failed to open archive"); + lar_close(ar); + } + else + { + LAR_DIE("Failed to open archive"); + } + + break; + + case 'r': + stat = do_require(argv[2]); + break; } + + return stat; } else { - printf("Usage: lar <archive> [<member>]\n"); + printf("Usage:\n"); + printf("\tlar show <archive> [<member>]\n"); + printf("\tlar require <package>\n"); + return 1; } return 0; } - diff --git a/contrib/lar/lar.c b/contrib/lar/lar.c index 255bc0150..57a16e9ff 100644 --- a/contrib/lar/lar.c +++ b/contrib/lar/lar.c @@ -46,7 +46,7 @@ lar_index * lar_get_index( lar_archive *ar ) idx_map = NULL; for( i = 0; i < idx_length; \ - i += (sizeof(lar_index) - sizeof(char)) + i += (sizeof(lar_index) - sizeof(char)) ) { idx_ptr = (lar_index *)malloc(sizeof(lar_index)); @@ -65,7 +65,7 @@ lar_index * lar_get_index( lar_archive *ar ) } uint32_t lar_get_filename( lar_archive *ar, - lar_index *idx_ptr, char *filename + lar_index *idx_ptr, char *filename ) { if( idx_ptr->nlength >= LAR_FNAME_BUFFER ) LAR_DIE("Filename exceeds maximum allowed length"); @@ -111,7 +111,7 @@ lar_member * lar_open_member( lar_archive *ar, const char *name ) member->data = &memberdata[idx_ptr->foffset % pgsz]; member->mmap = memberdata; - member->mlen = idx_ptr->flength + ( idx_ptr->foffset % pgsz ); + member->mlen = idx_ptr->flength + ( idx_ptr->foffset % pgsz ); return member; } @@ -135,7 +135,7 @@ lar_archive * lar_open( const char *filename ) int fd; struct stat as; lar_archive *ar; - + if( stat(filename, &as) == -1 ) return NULL; @@ -174,3 +174,49 @@ int lar_close( lar_archive *ar ) return 0; } +lar_archive * lar_find_archive( const char *package ) +{ + int seg = 1; + int i, j, len; + struct stat s; + LAR_FNAME(buffer); + + for( len = 0; package[len] != '\0'; len++ ) + if( package[len] == '.' ) seg++; + + while( seg > 0 ) + { + for( i = 0, j = 1; (i < len) && (j <= seg); i++ ) + { + if( package[i] == '.' ) { + if( j < seg ) j++; else break; + } + + buffer[i] = ( package[i] == '.' ) ? '/' : package[i]; + } + + buffer[i+0] = '.'; buffer[i+1] = 'l'; buffer[i+2] = 'a'; + buffer[i+3] = 'r'; buffer[i+4] = '\0'; + + if( (stat(buffer, &s) > -1) && (s.st_mode & S_IFREG) ) + return lar_open(buffer); + + seg--; + } + + return NULL; +} + +lar_member * lar_find_member( lar_archive *ar, const char *package ) +{ + int len; + LAR_FNAME(buffer); + + for( len = 0; package[len] != '\0'; len++ ) + buffer[len] = ( package[len] == '.' ) ? '/' : package[len]; + + buffer[len+0] = '.'; buffer[len+1] = 'l'; buffer[len+2] = 'u'; + buffer[len+3] = 'a'; buffer[len+4] = '\0'; + + return lar_open_member(ar, buffer); +} diff --git a/contrib/lar/lar.h b/contrib/lar/lar.h index a4379f1eb..c5b429b9f 100644 --- a/contrib/lar/lar.h +++ b/contrib/lar/lar.h @@ -73,4 +73,6 @@ lar_archive * lar_open( const char *filename ); int lar_close( lar_archive *ar ); +lar_archive * lar_find_archive( const char *package ); +lar_member * lar_find_member( lar_archive *ar, const char *package ); |