getfile.c

00001 #include "common.h"
00002 
00003 int progress(u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data );
00004 void usage(void);
00005 
00006 int main (int argc, char **argv)
00007 {
00008         njb_t njbs[NJB_MAX_DEVICES], *njb;
00009         int n, opt, debug;
00010         u_int32_t id;
00011         u_int64_t size;
00012         extern int optind;
00013         extern char *optarg;
00014         char *endptr;
00015         char *file;
00016 
00017         debug= 0;
00018         size= 0;
00019         while ( (opt= getopt(argc, argv, "D:s:")) != -1 ) {
00020                 switch (opt) {
00021                 case 'D':
00022                         debug= atoi(optarg);
00023                         break;
00024                 case 's':
00025                         size= strtoul(optarg, &endptr, 10);
00026                         if ( *endptr != '\0' ) {
00027                                 fprintf(stderr, "illegal size value %s\n",
00028                                         optarg);
00029                                 return 1;
00030                         }
00031                         break;
00032                 default:
00033                         usage();
00034                         return 1;
00035                 }
00036         }
00037         argc -= optind;
00038         argv += optind;
00039 
00040         if ( argc != 2 ) {
00041                 usage();
00042                 return 1;
00043         }
00044 
00045         id = strtoul(argv[0], &endptr, 10);
00046         if ( *endptr != 0 ) {
00047                 fprintf(stderr, "illegal value %s\n", optarg);
00048                 return 1;
00049         } else if ( ! id ) {
00050                 fprintf(stderr, "bad file id %u\n", id);
00051                 return 1;
00052         }
00053 
00054         file = argv[1];
00055 
00056         if ( debug ) NJB_Set_Debug(debug);
00057 
00058 
00059         if (NJB_Discover(njbs, 0, &n) == -1) {
00060           fprintf(stderr, "could not locate any jukeboxes\n");
00061           return 1;
00062         }
00063 
00064         if ( n == 0 ) {
00065                 fprintf(stderr, "no NJB devices found\n");
00066                 return 0;
00067         } 
00068 
00069         njb = njbs;
00070 
00071         if ( NJB_Open(njb) == -1 ) {
00072                 NJB_Error_Dump(njb, stderr);
00073                 return 1;
00074         }
00075 
00076         NJB_Capture(njb);
00077 
00078         if ( ! size ) {
00079                 njb_datafile_t *df;
00080 
00081                 printf("Locating file %u\n", id);
00082                 NJB_Reset_Get_Datafile_Tag(njb);
00083                 while ( (df = NJB_Get_Datafile_Tag(njb)) ) {
00084                         if ( df->dfid == id ) {
00085                                 size = df->filesize;
00086                         }
00087                         NJB_Datafile_Destroy(df);
00088                 }
00089 
00090                 /* Dump any pending errors */
00091                 NJB_Error_Dump(njb, stderr);
00092 
00093                 if ( size ) {
00094                         printf("%Lu bytes\n", size);
00095                 } else {
00096                         fprintf(stderr, "File %u not found\n", id);
00097                 }
00098         }
00099 
00100         if ( size ) {
00101                 if ( NJB_Get_File(njb, id, (u_int32_t) size, file, progress, NULL ) == -1 ) {
00102                         NJB_Error_Dump(njb, stderr);
00103                 }
00104                 printf("\n");
00105         }
00106 
00107         NJB_Release(njb);
00108 
00109         NJB_Close(njb);
00110 
00111         return 0;
00112 }
00113 
00114 int progress (u_int64_t sent, u_int64_t total, const char* buf, unsigned len, void *data)
00115 {
00116         int percent = (sent*100)/total;
00117 #ifdef __WIN32__
00118         printf("Progress: %I64u of %I64u (%d%%)\r", sent, total, percent);
00119 #else
00120         printf("Progress: %llu of %llu (%d%%)\r", sent, total, percent);
00121 #endif
00122         fflush(stdout);
00123         return 0;
00124 }
00125 
00126 void usage (void)
00127 {
00128         fprintf(stderr, "getfile [ -s size ] <fileid> <filename>\n");
00129 }

Generated on Mon Sep 11 00:52:12 2006 for libnjb by  doxygen 1.4.7