contrib/lar:
authorJo-Philipp Wich <jow@openwrt.org>
Mon, 6 Apr 2009 14:28:17 +0000 (14:28 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Mon, 6 Apr 2009 14:28:17 +0000 (14:28 +0000)
- add lar_find_archive() and lar_find_member() to get a member handle from a packagename
- extend cli to cover new operations

contrib/lar/cli.c
contrib/lar/lar.c
contrib/lar/lar.h

index 2b5e08891274c0a5d0dd583819ebcb96a00ca92d..e8daf63a53934be7f5eaeb5c611ee4ff3e8f8566 100644 (file)
@@ -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;
 }
-
index 255bc01503e8f8e768ff1a098b199173b750db85..57a16e9ff7d957f60bf341ab7310a9bafa1c87f2 100644 (file)
@@ -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);
+}
index a4379f1eb9b0fa18d36167e8398013d126e586b3..c5b429b9ff42346766512fe4f372d97dc7cb2abd 100644 (file)
@@ -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 );