Changeset 190

Show
Ignore:
Timestamp:
11/21/06 18:12:58 (2 years ago)
Author:
ludo
Message:

external mount + command line switches (from r3840)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/client/revimage/autorestore/autorestore.c

    • Property svn:keywords changed from Id Revision to Id
    r177 r190  
    2424 
    2525/* How it works: 
    26  * - mounts the filesystem with autorestore.sh 
    27  * - opens and interprets conf.txt 
     26 * - opens and interprets /revosave/conf.txt  
     27 * - logs and info are taken from /revoinfo/* 
    2828 */ 
    2929 
     
    3535#include <string.h> 
    3636#include <stdarg.h> 
     37#include <getopt.h> 
    3738#include <ctype.h> 
    3839#include <sys/types.h> 
     
    5758//#define TEST 1 
    5859//#define TEST_PARTONLY 1 
    59 #define LOGTXT "/revoinfo/log.restore" 
    6060 
    6161#include "zlib.h" 
     
    7171 
    7272int dnum; 
    73 /* default NFS read/write size */ 
    74 int rsize = 8192; 
    7573/* hd space checks enabled ? */ 
    7674int revonospc = 0; 
     
    7977/* cdrom restoration ? */ 
    8078int cdrom = 0; 
     79/* mtftp restoration ? */ 
     80int mtftp = 0; 
     81/* do not run LRS specific code */ 
     82int nolrs = 0; 
    8183 
    8284unsigned char buf[512]; 
     
    8486unsigned char command[120]; 
    8587 
     88/* paths mainly used  by mtftp restoration */ 
    8689unsigned char servip[40] = ""; 
    8790unsigned char servprefix[80] = ""; 
     
    8992char hostname[32] = ""; 
    9093 
     94/* paths */ 
     95char *revosave = "/revosave"; 
     96char *revoinfo = "/revoinfo"; 
     97char *revobin = "/revobin"; 
     98char *outdir = NULL; 
     99char tmppath[1024]; 
     100char logtxt[1024]; 
     101 
    91102/* do we have the bios HD map ? */ 
    92 int has_hdmap=0; 
    93 char * hdmap[65536]; 
     103int has_hdmap = 0; 
     104char *hdmap[65536]; 
    94105unsigned int exclude[65536]; 
    95106int nonewt = 0; 
     
    98109/* LDM's privhead */ 
    99110typedef struct privhead_s { 
    100   __u64     logical_disk_start;                                              
    101   __u64     logical_disk_size;                                               
    102   __u64     config_start;                                                    
    103   __u64     config_size;                                                     
     111    __u64 logical_disk_start; 
     112    __u64 logical_disk_size; 
     113    __u64 config_start; 
     114    __u64 config_size; 
    104115} privhead; 
    105116 
    106117/* */ 
    107 typedef struct params_ 
    108 
    109   int bitindex; 
    110   int fo; 
    111   __u64 offset; 
    112 
    113 PARAMS; 
     118typedef struct params_ { 
     119    int bitindex; 
     120    int fo; 
     121    __u64 offset; 
     122} PARAMS; 
    114123 
    115124 
     
    119128 
    120129/* Q&D IO abstraction layer */ 
    121 struct fops_ 
    122 
    123   int (*get)(char *fname, int filenum); 
    124   FILE * (*open)(char *fname, int filenum); 
    125   int  (*close)(FILE *stream); 
     130struct fops_ { 
     131    int (*get) (char *fname, int filenum); 
     132    FILE *(*open) (char *fname, int filenum); 
     133    int (*close) (FILE * stream); 
    126134} fops; 
    127135 
     
    132140 * printf() func with logging 
    133141 */ 
    134 void myprintf( const char *format_str, ...
     142void myprintf(const char *format_str, ...
    135143{ 
    136144    va_list ap; 
     
    138146 
    139147    /* write some info */ 
    140     foerr = fopen(LOGTXT, "a"); 
     148    foerr = fopen(logtxt, "a"); 
    141149    fprintf(foerr, "\n==== misc ====\n"); 
    142     va_start( ap, format_str ); 
    143     vfprintf( foerr, format_str, ap ); 
     150    va_start(ap, format_str); 
     151    vfprintf(foerr, format_str, ap); 
    144152    va_end(ap); 
    145153    fclose(foerr); 
     
    152160{ 
    153161    char cmd[1024]; 
    154     char *redir = " >> " LOGTXT " 2>&1"; 
    155162    FILE *foerr; 
    156163 
    157     strncpy(cmd, s, 1024 - strlen(redir) - 1); 
    158     strcat(cmd, redir); 
     164    strncpy(cmd, s, 1024 - sizeof(logtxt) - 9 - 1); 
     165    strcat(cmd, " >> "); 
     166    strcat(cmd, logtxt); 
     167    strcat(cmd, " 2>&1"); 
     168 
    159169 
    160170    /* write some info */ 
    161     foerr = fopen(LOGTXT, "a"); 
     171    foerr = fopen(logtxt, "a"); 
    162172    // ctime() 
    163173    fprintf(foerr, "\n==== %s ====\n", s); 
     
    167177#endif 
    168178    return (system(cmd)); 
    169      
     179 
    170180} 
    171181 
     
    176186{ 
    177187    char cmd[1024]; 
    178     char *redir = " 1>> " LOGTXT
     188    char *redir = " 1>> "
    179189    FILE *foerr; 
    180190 
    181     strncpy(cmd, s, 1024 - strlen(redir) - 1); 
     191    strncpy(cmd, s, 1024 - strlen(logtxt) - 5 - 1); 
    182192    strcat(cmd, redir); 
     193    strcat(cmd, logtxt); 
    183194 
    184195    /* write some info */ 
    185     foerr = fopen(LOGTXT, "a"); 
     196    foerr = fopen(logtxt, "a"); 
    186197    // ctime() 
    187198    fprintf(foerr, "\n==== %s ====\n", s); 
     
    189200 
    190201    return (system(cmd)); 
    191      
     202 
     203
     204 
     205/* 
     206 * snprintf to tmppath global var 
     207 */ 
     208char *tmprintf(const char *format_str, ...) 
     209
     210    va_list ap; 
     211 
     212    va_start(ap, format_str); 
     213    vsnprintf(tmppath, 1023, format_str, ap); 
     214    va_end(ap); 
     215 
     216    return tmppath; 
    192217} 
    193218 
     
    197222void fatal(void) 
    198223{ 
    199   system("/bin/revosendlog 8"); 
    200   while (1) 
    201     sleep(1); 
     224    if (!nolrs) 
     225        return; 
     226    system("revosendlog 8"); 
     227    while (1) 
     228        sleep(1); 
    202229} 
    203230 
     
    207234void restore_raw(char *device, char *fname) 
    208235{ 
    209   char buffer[1024]; 
    210   __u32 sect; 
    211   int fo, fp; 
    212  
    213   fo = open(device, O_WRONLY | O_LARGEFILE); 
    214   sprintf(buffer, "/revosave/%s", fname); 
    215   fp = open(buffer, O_RDONLY | O_LARGEFILE); 
    216  
    217   while (1) { 
    218     if (read(fp, buffer, 516) == 0) { 
    219       break; 
    220     } 
    221     sect = *(__u32 *) buffer; 
    222     if (lseek64(fo, (__u64)512 * (__off64_t) sect, SEEK_SET) == -1) { 
    223       DEBUG(printf("restore_raw: seek error\n")); 
    224       ui_seek_error(device, __LINE__, errno, fo, (__u64)512 * (__off64_t) sect); 
    225     } 
    226     if (write(fo, buffer+4, 512) != 512) { 
    227       DEBUG(printf("restore_raw: write error\n")); 
    228       ui_write_error(device, __LINE__, errno, fo); 
    229     } 
    230   } 
    231   close(fp); 
    232   if (ioctl(fo, BLKRRPART) < 0) { 
    233     printf("Reloading partition table failed\n"); 
    234   } 
    235   close(fo); 
     236    char buffer[1024]; 
     237    __u32 sect; 
     238    int fo, fp; 
     239 
     240    fo = open(device, O_WRONLY | O_LARGEFILE); 
     241    tmprintf("%s/%s", revosave, fname); 
     242    fp = open(tmppath, O_RDONLY | O_LARGEFILE); 
     243 
     244    while (1) { 
     245        if (read(fp, buffer, 516) == 0) { 
     246            break; 
     247        } 
     248        sect = *(__u32 *) buffer; 
     249        if (lseek64(fo, (__u64) 512 * (__off64_t) sect, SEEK_SET) == -1) { 
     250            DEBUG(printf("restore_raw: seek error\n")); 
     251            ui_seek_error(device, __LINE__, errno, fo, 
     252                          (__u64) 512 * (__off64_t) sect); 
     253        } 
     254        if (write(fo, buffer + 4, 512) != 512) { 
     255            DEBUG(printf("restore_raw: write error\n")); 
     256            ui_write_error(device, __LINE__, errno, fo); 
     257        } 
     258    } 
     259    close(fp); 
     260    if (ioctl(fo, BLKRRPART) < 0) { 
     261        myprintf("Reloading partition table failed\n"); 
     262    } 
     263    close(fo); 
    236264} 
    237265 
     
    242270int file_get(char *fname, int filenum) 
    243271{ 
    244   char f[64]; 
    245   struct stat st; 
    246   int ret; 
    247  
    248   sprintf(f, "%s%03d", fname, filenum); 
    249   DEBUG (printf ("** File: %s **", f)); 
    250 retry: 
    251   chdir("/revosave"); 
    252   ret = stat(f, &st); 
    253   if (ret == -1 && cdrom) { 
    254         /* ask to swap the media*/ 
     272    char f[64]; 
     273    struct stat st; 
     274    int ret; 
     275 
     276    sprintf(f, "%s%03d", fname, filenum); 
     277    DEBUG(printf("** File: %s **", f)); 
     278  retry: 
     279    chdir(revosave); 
     280    ret = stat(f, &st); 
     281    if (ret == -1 && cdrom) { 
     282        /* ask to swap the media */ 
    255283        chdir("/"); 
     284        /* hardcoded paths because this feature is only used in the LRS-CD */ 
    256285        system("umount /revosave"); 
    257286        /* wait */ 
    258         system("/revobin/image_error \"Please insert the next CD, and press a key\""); 
     287        system 
     288            ("/revobin/image_error \"Please insert the next CD, and press a key\""); 
    259289        getchar(); 
    260290        system("mount -t iso9660 /dev/cdrom /revosave"); 
    261291        goto retry; 
    262  
    263   return (ret); 
     292   
     293    return (ret); 
    264294} 
    265295 
    266296FILE *file_open(char *fname, int filenum) 
    267297{ 
    268   char f[64]; 
    269   FILE *fid; 
    270    
    271   sprintf(f, "%s%03d", fname, filenum); 
    272   fid = fopen(f, "r"); 
    273   return (fid); 
    274 } 
    275  
    276 int file_close(FILE *stream) 
    277 { 
    278   return fclose(stream); 
     298    char f[64]; 
     299    FILE *fid; 
     300 
     301    sprintf(f, "%s%03d", fname, filenum); 
     302    fid = fopen(f, "r"); 
     303    return (fid); 
     304} 
     305 
     306int file_close(FILE * stream) 
     307{ 
     308    return fclose(stream); 
    279309} 
    280310 
     
    284314int tftp_get(char *fname, int filenum) 
    285315{ 
    286   char f[64], cmd[512]; 
    287   struct stat st; 
    288  
    289   sprintf(f, "%s%03d", fname, filenum); 
    290   DEBUG (printf ("** File: %s **", f)); 
    291   chdir("/tmpfs"); 
    292   if (!nonewt) update_file(fname, filenum, -2, "Waiting", done); 
    293   sprintf(cmd, "/bin/revowait %s", f); 
    294   system(cmd); 
    295   if (!nonewt) update_file(fname, filenum, -2, "Downloading", done); 
    296   /* get files */ 
    297   do  
    298     { 
    299       system("rm * >/dev/null 2>&1"); 
    300       sprintf(cmd, "/bin/atftp --tftp-timeout 10 --option \"blksize 4096\" --option multicast -g -r %s/%s/%s %s 69 2>/tmp/atftp.log", servprefix, storagedir, f, servip); 
     316    char f[64], cmd[512]; 
     317    struct stat st; 
     318 
     319    sprintf(f, "%s%03d", fname, filenum); 
     320    DEBUG(printf("** File: %s **", f)); 
     321    chdir("/tmpfs"); 
     322    if (!nonewt) 
     323        update_file(fname, filenum, -2, "Waiting", done); 
     324    sprintf(cmd, "revowait %s", f); 
     325    system(cmd); 
     326    if (!nonewt) 
     327        update_file(fname, filenum, -2, "Downloading", done); 
     328    /* get files */ 
     329    do { 
     330        system("rm * >/dev/null 2>&1"); 
     331        sprintf(cmd, 
     332                "atftp --tftp-timeout 10 --option \"blksize 4096\" --option multicast -g -r %s/%s/%s %s 69 2>/tmp/atftp.log", 
     333                servprefix, storagedir, f, servip); 
    301334    } while (system(cmd)); 
    302    
    303   return (stat(f, &st)); 
     335 
     336    return (stat(f, &st)); 
    304337} 
    305338 
    306339FILE *tftp_open(char *fname, int filenum) 
    307340{ 
    308   char f[64]; 
    309    
    310   sprintf(f, "%s%03d", fname, filenum); 
    311   return (fopen(f, "r")); 
    312 
    313  
    314 int tftp_close(FILE *stream) 
    315 
    316   int ret = fclose(stream); 
    317   /* delete files */ 
    318   //system("rm * >/dev/null 2>&1"); 
    319   return ret; 
    320 
    321  
    322 /* 
    323  */ 
    324 int 
    325 eof (int fd) 
    326 
    327   __off64_t pos, end; 
    328   pos = lseek64 (fd, 0, SEEK_CUR); 
    329   if (pos < 0) 
    330     { 
    331       fprintf (stderr, "Error LSEEK : eof,pos\n"); 
    332     } 
    333   end = lseek64 (fd, 0, SEEK_END); 
    334   if (end < 0) 
    335     { 
    336       fprintf (stderr, "Error LSEEK : eof,end\n"); 
    337     } 
    338   if (lseek64 (fd, pos, SEEK_SET) < 0) 
    339     { 
    340       fprintf (stderr, "Error LSEEK, reseek\n"); 
    341     } 
    342   if (end == pos) 
    343     return 1; 
    344   return 0; 
     341    char f[64]; 
     342 
     343    sprintf(f, "%s%03d", fname, filenum); 
     344    return (fopen(f, "r")); 
     345
     346 
     347int tftp_close(FILE * stream) 
     348
     349    int ret = fclose(stream); 
     350 
     351    return ret; 
     352
     353 
     354/* 
     355 */ 
     356int eof(int fd) 
     357
     358    __off64_t pos, end; 
     359    pos = lseek64(fd, 0, SEEK_CUR); 
     360    if (pos < 0) { 
     361        fprintf(stderr, "Error LSEEK : eof,pos\n"); 
     362    } 
     363    end = lseek64(fd, 0, SEEK_END); 
     364    if (end < 0) { 
     365        fprintf(stderr, "Error LSEEK : eof,end\n"); 
     366    } 
     367    if (lseek64(fd, pos, SEEK_SET) < 0) { 
     368        fprintf(stderr, "Error LSEEK, reseek\n"); 
     369    } 
     370    if (end == pos) 
     371        return 1; 
     372    return 0; 
    345373} 
    346374 
     
    348376 * 
    349377 */ 
     378void fill(int fd, int bytes, int dir) 
     379{ 
     380    /* fills are not larger than 90MB so an 'int'should be enough */ 
     381    int err = 0; 
     382 
     383    if (lseek64(fd, bytes, dir) < 0) { 
     384        ui_write_error(__FILE__, __LINE__, errno, fd); 
     385        err = 1; 
     386    } 
     387} 
     388 
     389 
     390/* 
     391 * 
     392 */ 
    350393void 
    351 fill (int fd, int bytes, int dir) 
    352 
    353   /* fills are not larger than 90MB so an 'int'should be enough */ 
    354   int err = 0; 
    355  
    356   if (lseek64 (fd, bytes, dir) < 0) 
    357     { 
    358       ui_write_error(__FILE__, __LINE__, errno, fd); 
    359       err = 1; 
    360     } 
    361 
    362  
    363  
    364 /* 
    365  * 
    366  */ 
    367 void 
    368 flushToDisk (unsigned char *buff, unsigned char *bit, PARAMS * cp, int lg) 
    369 
    370   unsigned char *ptr = buff; 
    371   unsigned char mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; 
    372   int indx = cp->bitindex; 
    373    
     394flushToDisk(unsigned char *buff, unsigned char *bit, PARAMS * cp, int lg) 
     395
     396    unsigned char *ptr = buff; 
     397    unsigned char mask[] = 
     398        { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }; 
     399    int indx = cp->bitindex; 
     400 
    374401 
    375402// printf("Enter : bitindex -> %d\n",indx); 
    376   while (lg > 0) 
    377     { 
    378       __u64 s = 0; 
    379       while (!(bit[indx >> 3] & mask[indx & 7])) 
    380         { 
    381           indx++; 
    382           cp->offset += 512; 
    383           s += 512; 
    384         } 
    385       if (( s != 0) && (lseek64 (cp->fo, s, SEEK_CUR) < 0)) 
    386         { 
    387           ui_write_error(__FILE__, __LINE__, errno, cp->fo); 
     403    while (lg > 0) { 
     404        __u64 s = 0; 
     405        while (!(bit[indx >> 3] & mask[indx & 7])) { 
     406            indx++; 
     407            cp->offset += 512; 
     408            s += 512; 
     409        } 
     410        if ((s != 0) && (lseek64(cp->fo, s, SEEK_CUR) < 0)) { 
     411            ui_write_error(__FILE__, __LINE__, errno, cp->fo); 
    388412        } 
    389413//      printf("Write @offset : %lld\t",cp->offset); 
    390414//      {int i; for(i=0;i<15;i++) printf("%02x ",ptr[i]); printf("\n");} 
    391       if (cp->fo) 
    392         { 
    393           if (write (cp->fo, ptr, 512) != 512) 
     415        if (cp->fo) { 
     416            if (write(cp->fo, ptr, 512) != 512) { 
     417                ui_write_error(__FILE__, __LINE__, errno, cp->fo); 
     418            } 
     419        } 
     420        cp->offset += 512; 
     421        ptr += 512; 
     422        indx++; 
     423        lg -= 512; 
     424    } 
     425// printf("Exit  : bitindex -> %d\n",indx); 
     426    cp->bitindex = indx; 
     427
     428 
     429 
     430/* 
     431 * 
     432 */ 
     433void restore(char *device, unsigned int sect, char *fname) 
     434
     435    int fo;                     /* output device */ 
     436    z_stream zptr; 
     437    int state, filenum, fmax = -1; 
     438    int ret, firstpass, bitmaplg; 
     439    FILE *fi; 
     440    PARAMS currentparams; 
     441    __u64 i, starto; 
     442 
     443    currentparams.offset = 512 * (__u64) sect; 
     444    currentparams.bitindex = 0; 
     445    memset(zero, 0, 512); 
     446 
     447    // log 
     448    myprintf("restore: %s, offset %d sectors, %s\n", device, sect, fname); 
     449 
     450    // open the output device 
     451    fo = open(device, O_WRONLY | O_LARGEFILE); 
     452    currentparams.fo = fo; 
     453    DEBUG(printf("Seeking to : %lld\n", currentparams.offset)); 
     454    i = lseek64(currentparams.fo, currentparams.offset, SEEK_SET); 
     455    if (i != currentparams.offset) { 
     456        ui_seek_error(__FILE__, __LINE__, errno, currentparams.fo, 
     457                      currentparams.offset); 
     458    } 
     459    // open the data directory 
     460    filenum = 0; 
     461    while (!fops.get(fname, filenum)) { 
     462        /*      name = ep->d_name; 
     463           l = strlen(name); 
     464           if (strncmp(name, fname, strlen(fname))) continue; 
     465           if (l < 4) continue; 
     466           if (!isdigit(name[l-1]) || !isdigit(name[l-2]) || !isdigit(name[l-3]) ) continue;         
     467           DEBUG (printf ("** File: %s **", fname)); 
     468         */ 
     469      start: 
     470        if (!nonewt) 
     471            update_file(fname, filenum, fmax, device, done); 
     472 
     473        state = Z_SYNC_FLUSH; 
     474        firstpass = 1; 
     475        bitmaplg = 0; 
     476 
     477        zptr.zalloc = NULL; 
     478        zptr.zfree = NULL; 
     479 
     480        starto = lseek64(currentparams.fo, 0, SEEK_CUR);        /* save the current offset */ 
     481 
     482        fi = fops.open(fname, filenum); 
     483        if (fi == NULL) { 
     484            /*printf ("Cannot open input file\n"); */ 
     485            system("/revobin/image_error \"Cannot open input file\""); 
     486            fatal(); 
     487        } 
     488 
     489        zptr.avail_in = fread(IN, 1, INSIZE, fi); 
     490 
     491        currentparams.offset = 0; 
     492        currentparams.bitindex = 0; 
     493 
     494        zptr.next_in = (unsigned char *) IN; 
     495        zptr.next_out = (unsigned char *) BUFFER;       // was dbuf.data; 
     496        zptr.avail_out = 24064; 
     497 
     498        inflateInit(&zptr); 
     499 
     500        do { 
     501//  if (inflateSyncPoint(&zptr)) printf("#"); 
     502 
     503            ret = inflate(&zptr, state); 
     504            if (!nonewt) 
     505                update_progress(done + (zptr.total_in / 1024)); 
     506 
     507//  printf("-> %d : %d / %d\n",ret ,zptr.avail_in ,zptr.avail_out ); 
     508 
     509            if ((ret == Z_OK) && (zptr.avail_out == 0)) { 
     510                if (firstpass) { 
     511                    DEBUG(printf("Params : *%s\n", BUFFER)); 
     512                    if (strstr(BUFFER, "BLOCKS=")) { 
     513                        int i = 0; 
     514                        if (sscanf(strstr(BUFFER, "BLOCKS=") + 7, "%d", &i) 
     515                            == 1) { 
     516                            fmax = i; 
     517                        } 
     518                    } 
     519                    if (strstr(BUFFER, "ALLOCTABLELG=")) 
     520                        sscanf(strstr(BUFFER, "ALLOCTABLELG=") + 13, "%d", 
     521                               &bitmaplg); 
     522                    memcpy(Bitmap, BUFFER + 2048, 24064 - 2048); 
     523                    currentparams.bitindex = 0; 
     524                    firstpass = 0; 
     525                } else { 
     526                    flushToDisk(BUFFER, Bitmap, &currentparams, 24064); 
     527                } 
     528 
     529                zptr.next_out = (unsigned char *) BUFFER; 
     530                zptr.avail_out = 24064; 
     531            } 
     532 
     533            if ((ret == Z_OK) && (zptr.avail_in == 0)) { 
     534                zptr.avail_in = fread(IN, 1, INSIZE, fi); 
     535                zptr.next_in = (unsigned char *) IN; 
     536            } 
     537        } 
     538        while (ret == Z_OK); 
     539 
     540        if (ret == Z_STREAM_END) { 
    394541            { 
    395               ui_write_error(__FILE__, __LINE__, errno, cp->fo); 
    396             } 
    397         } 
    398       cp->offset += 512; 
    399       ptr += 512; 
    400       indx++; 
    401       lg -= 512; 
    402     } 
    403 // printf("Exit  : bitindex -> %d\n",indx); 
    404   cp->bitindex = indx; 
    405 
    406  
    407  
    408 /* 
    409  * 
    410  */ 
    411 void restore(char *device, unsigned int sect, char *fname) 
    412 
    413   int fo;                       /* output device */ 
    414   z_stream zptr; 
    415   int state, filenum, fmax = -1; 
    416   int ret, firstpass, bitmaplg; 
    417   FILE *fi; 
    418   PARAMS currentparams; 
    419   __u64 i, starto; 
    420  
    421   currentparams.offset = 512*(__u64)sect; 
    422   currentparams.bitindex = 0; 
    423   memset (zero, 0, 512); 
    424  
    425   // log 
    426   myprintf("restore: %s, offset %d sectors, %s\n", device, sect, fname); 
    427  
    428   // open the output device 
    429   fo = open(device, O_WRONLY | O_LARGEFILE ); 
    430   currentparams.fo = fo; 
    431   DEBUG(printf ("Seeking to : %lld\n", currentparams.offset)); 
    432   i = lseek64 (currentparams.fo, currentparams.offset, SEEK_SET); 
    433   if (i != currentparams.offset) { 
    434     ui_seek_error(__FILE__, __LINE__, errno, currentparams.fo, currentparams.offset); 
    435   } 
    436  
    437   // open the data directory 
    438   filenum = 0; 
    439   while (!fops.get(fname ,filenum)) 
    440     { 
    441       /*      name = ep->d_name; 
    442       l = strlen(name); 
    443       if (strncmp(name, fname, strlen(fname))) continue; 
    444       if (l < 4) continue; 
    445       if (!isdigit(name[l-1]) || !isdigit(name[l-2]) || !isdigit(name[l-3]) ) continue;    
    446       DEBUG (printf ("** File: %s **", fname)); 
    447       */ 
    448     start: 
    449       if (!nonewt) update_file(fname, filenum, fmax, device, done); 
    450  
    451       state = Z_SYNC_FLUSH; 
    452       firstpass = 1; 
    453       bitmaplg = 0; 
    454        
    455       zptr.zalloc = NULL; 
    456       zptr.zfree = NULL; 
    457  
    458       starto = lseek64(currentparams.fo, 0 , SEEK_CUR); /* save the current offset */ 
    459  
    460       fi = fops.open (fname, filenum); 
    461       if (fi == NULL) 
    462         { 
    463           /*printf ("Cannot open input file\n");*/ 
    464           system("/revobin/image_error \"Cannot open input file\""); 
    465           fatal(); 
    466         } 
    467  
    468       zptr.avail_in = fread (IN, 1, INSIZE, fi); 
    469              
    470       currentparams.offset = 0; 
    471       currentparams.bitindex = 0; 
    472        
    473       zptr.next_in = (unsigned char *) IN; 
    474       zptr.next_out = (unsigned char *) BUFFER; // was dbuf.data; 
    475       zptr.avail_out = 24064; 
    476        
    477       inflateInit (&zptr); 
    478  
    479       do 
    480         { 
    481 //  if (inflateSyncPoint(&zptr)) printf("#"); 
    482  
    483               ret = inflate (&zptr, state); 
    484               if (!nonewt) update_progress( done + (zptr.total_in/1024) ); 
    485  
    486 //  printf("-> %d : %d / %d\n",ret ,zptr.avail_in ,zptr.avail_out ); 
    487  
    488               if ((ret == Z_OK) && (zptr.avail_out == 0)) 
    489                 { 
    490                   if (firstpass) 
    491                     { 
    492                       DEBUG (printf ("Params : *%s\n", BUFFER)); 
    493                       if (strstr (BUFFER, "BLOCKS=")) 
    494                         { 
    495                           int i = 0; 
    496                           if (sscanf (strstr (BUFFER, "BLOCKS=") + 7, "%d", &i) == 1) { 
     542                if (firstpass) { 
     543                    DEBUG(printf("Params : *%s*\n", BUFFER)); 
     544                    if (strstr(BUFFER, "BLOCKS=")) { 
     545                        int i = 0; 
     546                        if (sscanf(strstr(BUFFER, "BLOCKS=") + 7, "%d", &i) 
     547                            == 1) { 
    497548                            fmax = i; 
    498                           } 
    499549                        } 
    500                       if (strstr (BUFFER, "ALLOCTABLELG=")) 
    501                         sscanf (strstr (BUFFER, "ALLOCTABLELG=") + 13, "%d", 
    502                                 &bitmaplg); 
    503                       memcpy (Bitmap, BUFFER + 2048, 24064 - 2048); 
    504                       currentparams.bitindex = 0; 
    505                       firstpass = 0; 
    506550                    } 
    507                   else 
    508                     { 
    509                       flushToDisk (BUFFER, Bitmap, &currentparams, 24064); 
    510                     } 
    511  
    512                   zptr.next_out = (unsigned char *) BUFFER; 
    513                   zptr.avail_out = 24064; 
    514                 } 
    515  
    516               if ((ret == Z_OK) && (zptr.avail_in == 0)) 
    517                 { 
    518                   zptr.avail_in = fread (IN, 1, INSIZE, fi); 
    519                   zptr.next_in = (unsigned char *) IN; 
     551                    if (strstr(BUFFER, "ALLOCTABLELG=")) 
     552                        sscanf(strstr(BUFFER, "ALLOCTABLELG=") + 13, "%d", 
     553                               &bitmaplg); 
     554                    memcpy(Bitmap, BUFFER + 2048, 24064 - 2048); 
     555                    zptr.next_out = (unsigned char *) BUFFER; 
     556                    zptr.avail_out = 24064; 
    520557                } 
    521558            } 
    522           while (ret == Z_OK); 
    523  
    524           if (ret == Z_STREAM_END) 
    525             { 
    526               { 
    527                 if (firstpass) 
    528                   { 
    529                     DEBUG (printf ("Params : *%s*\n", BUFFER)); 
    530                     if (strstr (BUFFER, "BLOCKS=")) 
    531                       { 
    532                         int i = 0; 
    533                         if (sscanf (strstr (BUFFER, "BLOCKS=") + 7, "%d", &i) == 1) { 
    534                           fmax = i; 
    535                         } 
    536                       } 
    537                     if (strstr (BUFFER, "ALLOCTABLELG=")) 
    538                       sscanf (strstr (BUFFER, "ALLOCTABLELG=") + 13, "%d", 
    539                               &bitmaplg); 
    540                     memcpy (Bitmap, BUFFER + 2048, 24064 - 2048); 
    541                     zptr.next_out = (unsigned char *) BUFFER; 
    542                     zptr.avail_out = 24064; 
    543                   } 
    544               } 
    545  
    546               //printf ("Flushing to EOF ... (%d bytes)\n", 
    547                 //      24064 - zptr.avail_out); 
    548               flushToDisk (BUFFER, Bitmap, &currentparams, 24064 - zptr.avail_out); 
    549               zptr.next_out = (unsigned char *) BUFFER; 
    550               zptr.avail_out = 24064; 
    551             } 
    552  
    553           ret = inflate (&zptr, Z_FINISH); 
    554           inflateEnd (&zptr); 
    555  
    556  
    557           if (ret < 0) 
    558             { 
    559               /*printf ("Returned : %d\t", ret); 
    560                 printf ("(AvailIn : %d / ", zptr.avail_in); 
    561                 printf ("AvailOut: %d)\n", zptr.avail_out); 
    562                 printf ("(TotalIn : %ld / ", zptr.total_in); 
    563                 printf ("TotalOut: %ld)\n", zptr.total_out);*/ 
    564               ui_zlib_error(ret); 
    565               fops.close (fi); 
    566               fops.get(fname ,filenum); /* reget file */ 
    567               /* return to the correct offset */ 
    568               lseek64(currentparams.fo, starto , SEEK_SET);      
    569  
    570               goto start; 
    571             } 
    572  
    573           /*printf ("Offset : %lld\n", currentparams.offset); 
    574           printf ("Bitmap index : %d\n", currentparams.bitindex);*/ 
    575  
    576           if (bitmaplg) 
    577             { 
    578               if (bitmaplg * 8 > currentparams.bitindex) 
    579                 { 
    580                   currentparams.offset += 
    581                     (__u64)512 * (bitmaplg * 8 - currentparams.bitindex); 
    582                   if (currentparams.fo) 
    583                     { 
    584                       /* no error check for the last fill */ 
    585                       /* bounds will be checked by the following write */ 
    586                       lseek64 (currentparams.fo,  
    587                             (__u64)512 * (bitmaplg * 8 - currentparams.bitindex), 
    588                             SEEK_CUR); 
    589                     } 
     559 
     560            //printf ("Flushing to EOF ... (%d bytes)\n", 
     561            //      24064 - zptr.avail_out); 
     562            flushToDisk(BUFFER, Bitmap, &currentparams, 
     563                        24064 - zptr.avail_out); 
     564            zptr.next_out = (unsigned char *) BUFFER; 
     565            zptr.avail_out = 24064; 
     566        } 
     567 
     568        ret = inflate(&zptr, Z_FINISH); 
     569        inflateEnd(&zptr); 
     570 
     571 
     572        if (ret < 0) { 
     573            /*printf ("Returned : %d\t", ret); 
     574               printf ("(AvailIn : %d / ", zptr.avail_in); 
     575               printf ("AvailOut: %d)\n", zptr.avail_out); 
     576               printf ("(TotalIn : %ld / ", zptr.total_in); 
     577               printf ("TotalOut: %ld)\n", zptr.total_out); */ 
     578            ui_zlib_error(ret); 
     579            fops.close(fi); 
     580            fops.get(fname, filenum);   /* reget file */ 
     581            /* return to the correct offset */ 
     582            lseek64(currentparams.fo, starto, SEEK_SET); 
     583 
     584            goto start; 
     585        } 
     586 
     587        /*printf ("Offset : %lld\n", currentparams.offset); 
     588           printf ("Bitmap index : %d\n", currentparams.bitindex); */ 
     589 
     590        if (bitmaplg) { 
     591            if (bitmaplg * 8 > currentparams.bitindex) { 
     592                currentparams.offset += 
     593                    (__u64) 512 *(bitmaplg * 8 - currentparams.bitindex); 
     594                if (currentparams.fo) { 
     595                    /* no error check for the last fill */ 
     596                    /* bounds will be checked by the following write */ 
     597                    lseek64(currentparams.fo, 
     598                            (__u64) 512 * (bitmaplg * 8 - 
     599                                           currentparams.bitindex), 
     600                            SEEK_CUR); 
    590601                } 
    591602            } 
    592  
    593           filenum ++; 
    594           fops.close (fi); 
    595           done += zptr.total_in/1024; 
    596           if ((fmax != -1) && (filenum >= fmax)) break; 
    597     } 
    598   close(fo); 
    599 
    600  
    601 /* 
    602  */ 
    603 unsigned char *find(const char *str, const char *fname) 
     603        } 
     604 
     605        filenum++; 
     606        fops.close(fi); 
     607        done += zptr.total_in / 1024; 
     608        if ((fmax != -1) && (filenum >= fmax)) 
     609            break; 
     610    } 
     611    close(fo); 
     612
     613 
     614/* 
     615 */ 
     616char *find(const char *str, const char *fname) 
    604617{ 
    605618    FILE *f; 
     
    608621    if (f == NULL) 
    609622        return NULL; 
    610     while (fgets((char *)buf, 256, f)) { 
     623    while (fgets((char *) buf, 256, f)) { 
    611624        if (strstr(buf, str)) { 
    612625            fclose(f); 
     
    623636void netinfo(void) 
    624637{ 
    625     unsigned char *ptr, *ptr2; 
     638    char *ptr, *ptr2; 
    626639 
    627640    if ((ptr = find("Next server: ", "/etc/netinfo.log"))) { 
     
    658671    f = fopen("/etc/lbxname", "r"); 
    659672    if (f != NULL) { 
    660         fscanf(f, "%31s", hostname); 
    661         fclose(f);     
     673       fscanf(f, "%31s", hostname); 
     674        fclose(f); 
    662675    } 
    663676 
    664677    f = fopen("/revoinfo/hostname", "r"); 
    665678    if (f == NULL) 
    666         return
     679        return
    667680    fscanf(f, "%31s", hostname); 
    668     fclose(f);     
    669 
    670  
    671 void commandline(void) 
    672 
    673     unsigned char *ptr, *ptr2; 
     681    fclose(f); 
     682
     683 
     684 
     685void setdefault(char *v) 
     686
     687    char buf[256]; 
     688 
     689    sprintf(buf, "revosetdefault %s", v != NULL ? v : "0"); 
     690    system(buf); 
     691
     692 
     693/* 
     694 * interprets the conf.txt file 
     695 */ 
     696void restoreimage(void) 
     697
     698    FILE *f; 
     699    char buf[255], buf2[255], lvm[255]; 
     700    int vgscan = 0; 
     701    char *conftxt; 
     702 
     703    if (cdrom) { 
     704        conftxt = "/tmp/conf.txt"; 
     705    } else { 
     706        tmprintf("%s/conf.txt", revosave); 
     707        conftxt = tmppath; 
     708    } 
     709    if ((f = fopen(conftxt, "r")) == NULL) 
     710        return; 
     711    while (!feof(f)) { 
     712        fgets(buf, 250, f); 
     713        if (sscanf(buf, "%s", buf2) == 1) { 
     714            /* buf=full line, buf2=1st keyword */ 
     715            DEBUG(printf("%s\n", buf2)); 
     716            if (!strcmp("ptabs", buf2)) { 
     717                // ptabs command 
     718                unsigned int d1; 
     719 
     720                if (sscanf(buf, " ptabs (hd%u) (nd)PATH/%s", &d1, buf2) == 
     721                    2) { 
     722                    DEBUG(printf("%d,%s\n", d1, buf2)); 
     723                    // restore the files to the device 
     724#ifdef TEST 
     725                    //restore_raw("/revoinfo/PTABS", buf2); 
     726#else 
     727                    restore_raw(hdmap[d1], buf2); 
     728#endif 
     729                } 
     730 
     731            } else if (!strcmp("partcopy", buf2)) { 
     732                // partcopy command 
     733                unsigned int d1, d2, sect; 
     734#ifdef TEST_PARTONLY 
     735                continue; 
     736#endif 
     737                if (sscanf(buf, " partcopy (hd%u,%u) %u PATH/%s", 
     738                           &d1, &d2, &sect, buf2) == 4) { 
     739                    DEBUG(printf("%d,%d,%d,%s\n", d1, d2, sect, buf2)); 
     740                    // convert the BIOS hd number to a Linux device          
     741                    // and restore the files to the device 
     742                    if (d1 >= 3968 && d2 == -1) { 
     743                        // lvm: no hdmap necessary 
     744                        strncpy(lvm, "/dev/", 5); 
     745                        if (sscanf 
     746                            (buf, " partcopy (hd%*u,%*u) %*u PATH/%*s %s", 
     747                             &lvm[5]) != 1) { 
     748                            myprintf("syntax error in conf.txt: %s\n", 
     749                                     buf); 
     750                            exit(1); 
     751                        } 
     752                        if (vgscan == 0) { 
     753                            system 
     754                                ("lvm vgscan >/dev/null 2>&1; lvm vgchange -ay >/dev/null 2>&1"); 
     755                            vgscan = 1; 
     756                        } 
     757                        DEBUG(printf("lvm : %s\n", lvm)); 
     758                        restore(lvm, sect, buf2); 
     759 
     760                    } else { 
     761#ifdef TEST 
     762                        // restore("/revoinfo/P1", sect, buf2); 
     763#else 
     764                        restore(hdmap[d1], sect, buf2); 
     765#endif 
     766                        // fix the NT Bootloader if needed 
     767                        if (sect == 63 && revontblfix == 1) { 
     768                            char command[255]; 
     769                            sprintf(command, "ntblfix %s %d", hdmap[d1], 
     770                                    d1 + 1); 
     771                            mysystem(command); 
     772                        } 
     773                    } 
     774                } 
     775            } else if (!strcmp("setdefault", buf2) && !nolrs) { 
     776                strtok(buf, " "); 
     777                setdefault(strtok(NULL, " ")); 
     778            } else if (!strcmp("chainloader", buf2) && !nolrs) { 
     779                setdefault("0"); 
     780            } 
     781        } 
     782    } 
     783    fclose(f); 
     784
     785 
     786/* 
     787 * Check if the image can fit 
     788 */ 
     789void checkhdspace(__u32 major, __u32 minor, __u32 sect) 
     790
     791    FILE *f; 
     792    char command[256]; 
     793    __u32 orig = 0; 
     794 
     795    if (revonospc) 
     796        return; 
     797 
     798    tmprintf("%s/size%02x%02x.txt", revosave, major, minor); 
     799    f = fopen(tmppath, "r"); 
     800    if (f == NULL) 
     801        return; 
     802    fscanf(f, "%u", &orig); 
     803    if (orig > sect) { 
     804        /* problem : the disk seems to be too small */ 
     805        if (!nolrs) 
     806            system("revosendlog 8"); 
     807        sprintf(command, 
     808                "/revobin/image_error \"Your hard disk seems to be to small to restore this image (%u vs %u KB).\n\nIf you want to restore anyway, you can disable the disk space checks in the client's options panel.\"", 
     809                sect, orig); 
     810        system(command); 
     811        while (1) 
     812            sleep(1); 
     813    } 
     814    fclose(f); 
     815
     816 
     817/* 
     818 * Build a BIOS number to device map  
     819 * (should use the 'hdmap' file if present)