Ignore:
Timestamp:
Mar 9, 2024, 3:10:04 AM (4 months ago)
Author:
Bruno Cornec
Message:

Fix all remaining compiler errors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3.3/mondo/src/common/libmondo-stream.c

    r3878 r3879  
    8080
    8181
     82
     83
     84/**
     85 * Log (to screen) an erroneous marker, along with what it should have been.
     86 * @param should_be What we were expecting.
     87 * @param it_is What we got.
     88 */
     89void wrong_marker(int should_be, int it_is)
     90{
     91    char *tmp1 = NULL;
     92    char *tmp2 = NULL;
     93    tmp1 =  marker_to_string(should_be);
     94    tmp2 =  marker_to_string(it_is);
     95    log_to_screen("Wrong marker! (Should be %s, is actually %s)", tmp1, tmp2);
     96    mr_free(tmp1);
     97    mr_free(tmp2);
     98}
     99
     100
     101
     102
     103/**
     104 * Read a header block from the currently opened stream (CD or tape).
     105 * This block indicates the length of the following file (if it's file-related)
     106 * the filename (if it's file-related), and the block type.
     107 * @param plen Where to put the length of the file. Valid only for file-related header blocks.
     108 * @param filename Where to put the name of the file. Valid only for file-related header blocks.
     109 * @param pcontrol_char Where to put the type of block (e.g. start-file, end-file, start-tape, ...)
     110 * @return 0 for success, nonzero for failure.
     111 * @note If you read a marker (@p pcontrol_char) you're not expecting, you can call wrong_marker().
     112 */
     113int
     114read_header_block_from_stream(long long *plen, char *filename,
     115                              int *pcontrol_char)
     116{
     117
     118    /*@ buffers ***************************************************** */
     119    char *tempblock;
     120    char *tmp = NULL;
     121
     122    /*@ int ********************************************************* */
     123    int i, retval;
     124
     125    /*@ end vars *************************************************** */
     126
     127    tempblock = (char *) malloc((size_t) TAPE_BLOCK_SIZE);
     128
     129    for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
     130        tempblock[i] = 0;
     131    }
     132    while (!(*pcontrol_char = tempblock[7000])) {
     133        g_tape_posK += fread(tempblock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream) / 1024;
     134    }
     135    memcpy((char *) plen, tempblock + 7001, sizeof(long long));
     136    if (strcmp(tempblock + 6000 + *pcontrol_char, STR_HEADER)) {
     137        log_it("Bad header block at %ld K", (long) g_tape_posK);
     138    }
     139    strcpy(filename, tempblock + 1000);
     140    if (*pcontrol_char == BLK_ABORTED_BACKUP) {
     141        log_to_screen("I can't verify an aborted backup.");
     142        retval = 1;
     143    } else {
     144        retval = 0;
     145    }
     146    for (i = 1000; i < 1020; i++) {
     147        if (tempblock[i] < 32 || tempblock[i] > 126) {
     148            tempblock[i] = ' ';
     149        }
     150    }
     151    tempblock[i] = '\0';
     152    tmp = marker_to_string(*pcontrol_char);
     153    log_msg(6, "%s (fname=%s, size=%ld K)", tmp, tempblock + 1000, (long) (*plen) >> 10);
     154    mr_free(tmp);
     155    paranoid_free(tempblock);
     156    return (retval);
     157}
    82158
    83159
     
    206282}
    207283
    208 
    209 
    210 
    211 /**
    212  * Log (to screen) an erroneous marker, along with what it should have been.
    213  * @param should_be What we were expecting.
    214  * @param it_is What we got.
    215  */
    216 void wrong_marker(int should_be, int it_is)
    217 {
    218     char *tmp1 = NULL;
    219     char *tmp2 = NULL;
    220     tmp1 =  marker_to_string(should_be);
    221     tmp2 =  marker_to_string(it_is);
    222     log_to_screen("Wrong marker! (Should be %s, is actually %s)", tmp1, tmp2);
    223     mr_free(tmp1);
    224     mr_free(tmp2);
    225 }
    226 
    227 
    228 
    229 /**
    230  * Read a header block from the currently opened stream (CD or tape).
    231  * This block indicates the length of the following file (if it's file-related)
    232  * the filename (if it's file-related), and the block type.
    233  * @param plen Where to put the length of the file. Valid only for file-related header blocks.
    234  * @param filename Where to put the name of the file. Valid only for file-related header blocks.
    235  * @param pcontrol_char Where to put the type of block (e.g. start-file, end-file, start-tape, ...)
    236  * @return 0 for success, nonzero for failure.
    237  * @note If you read a marker (@p pcontrol_char) you're not expecting, you can call wrong_marker().
    238  */
    239 int
    240 read_header_block_from_stream(long long *plen, char *filename,
    241                               int *pcontrol_char)
    242 {
    243 
    244     /*@ buffers ***************************************************** */
    245     char *tempblock;
    246     char *tmp = NULL;
    247 
    248     /*@ int ********************************************************* */
    249     int i, retval;
    250 
    251     /*@ end vars *************************************************** */
    252 
    253     tempblock = (char *) malloc((size_t) TAPE_BLOCK_SIZE);
    254 
    255     for (i = 0; i < (int) TAPE_BLOCK_SIZE; i++) {
    256         tempblock[i] = 0;
    257     }
    258     while (!(*pcontrol_char = tempblock[7000])) {
    259         g_tape_posK += fread(tempblock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream) / 1024;
    260     }
    261     memcpy((char *) plen, tempblock + 7001, sizeof(long long));
    262     if (strcmp(tempblock + 6000 + *pcontrol_char, STR_HEADER)) {
    263         log_it("Bad header block at %ld K", (long) g_tape_posK);
    264     }
    265     strcpy(filename, tempblock + 1000);
    266     if (*pcontrol_char == BLK_ABORTED_BACKUP) {
    267         log_to_screen("I can't verify an aborted backup.");
    268         retval = 1;
    269     } else {
    270         retval = 0;
    271     }
    272     for (i = 1000; i < 1020; i++) {
    273         if (tempblock[i] < 32 || tempblock[i] > 126) {
    274             tempblock[i] = ' ';
    275         }
    276     }
    277     tempblock[i] = '\0';
    278     tmp = marker_to_string(*pcontrol_char);
    279     log_msg(6, "%s (fname=%s, size=%ld K)", tmp, tempblock + 1000, (long) (*plen) >> 10);
    280     mr_free(tmp);
    281     paranoid_free(tempblock);
    282     return (retval);
    283 }
    284284
    285285
     
    561561    return (dev);
    562562}
     563
     564
     565/**
     566 * Seek through the stream until we find a header block where the NAME field matches
     567 * @p the_file_I_was_reading. This is useful if you've just started reading from
     568 * a new tape and want to find the file you were reading when the tape ended.
     569 * @param the_file_I_was_reading File name to look for.
     570 * @return 0 for success, nonzero for failure.
     571 */
     572int skip_incoming_files_until_we_find_this_one(char
     573                                               *the_file_I_was_reading)
     574{
     575    char *pA;
     576    char *pB;
     577    int res;
     578    int ctrl_chr;
     579    char *temp_fname;
     580    char *datablock;
     581    long long temp_size, size;
     582    long bytes_to_write;
     583
     584    datablock = malloc(TAPE_BLOCK_SIZE);
     585    malloc_string(temp_fname);
     586    pB = strrchr(the_file_I_was_reading, '/');
     587    if (pB) {
     588        pB++;
     589    } else {
     590        pB = the_file_I_was_reading;
     591    }
     592    log_msg(1, "skip_incoming_..(%s)", pB);
     593    log_msg(2, "Looking for initial START_AN_AFIO_OR_SLICE");
     594    ctrl_chr = -1;
     595    while (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
     596        res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
     597        if (res) {
     598            // FIXME
     599        }
     600        if (ctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
     601            break;
     602        }
     603        log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
     604        wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
     605        log_msg(3, "Still trying to re-sync w/ tape");
     606    }
     607    while (ctrl_chr != BLK_START_FILE) {
     608        res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
     609        if (res) {
     610            //FIXME
     611        }
     612        if (ctrl_chr == BLK_START_FILE) {
     613            break;
     614        }
     615        log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
     616        wrong_marker(BLK_START_FILE, ctrl_chr);
     617        log_msg(3, "Still trying to re-sync w/ tape");
     618    }
     619    pA = strrchr(temp_fname, '/');
     620    if (pA) {
     621        pA++;
     622    } else {
     623        pA = temp_fname;
     624    }
     625    pB = strrchr(the_file_I_was_reading, '/');
     626    if (pB) {
     627        pB++;
     628    } else {
     629        pB = the_file_I_was_reading;
     630    }
     631    while (strcmp(pA, pB)) {
     632        log_msg(6, "Skipping %s (it's not %s)", temp_fname, the_file_I_was_reading);
     633        for (size = temp_size; size > 0; size -= bytes_to_write) {
     634            bytes_to_write =
     635                (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
     636            if (fread(datablock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream)) {
     637                // FIXME - needs error-checking and -catching
     638            }
     639        }
     640        res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
     641        if (ctrl_chr != BLK_STOP_FILE) {
     642            wrong_marker(BLK_STOP_FILE, ctrl_chr);
     643        }
     644        res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
     645        if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
     646            wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
     647        }
     648        res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
     649        if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
     650            wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
     651        }
     652        res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
     653        if (ctrl_chr != BLK_START_FILE) {
     654            wrong_marker(BLK_START_FILE, ctrl_chr);
     655        }
     656        pA = strrchr(temp_fname, '/');
     657        if (pA) {
     658            pA++;
     659        } else {
     660            pA = temp_fname;
     661        }
     662        pB = strrchr(the_file_I_was_reading, '/');
     663        if (pB) {
     664            pB++;
     665        } else {
     666            pB = the_file_I_was_reading;
     667        }
     668    }
     669    log_msg(2, "Reading %s (it matches %s)", temp_fname, the_file_I_was_reading);
     670    paranoid_free(temp_fname);
     671    paranoid_free(datablock);
     672    return (0);
     673}
     674
     675
     676/**
     677 * Start to read from the next tape. Assumes the user has already inserted it.
     678 * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
     679 * @return 0 for success, nonzero for failure.
     680 */
     681int start_to_read_from_next_tape()
     682{
     683    /*@ int ********************************************************* */
     684    int res = 0;
     685    char *sz_msg;
     686    int ctrlchr;
     687    long long temp_size;
     688    malloc_string(sz_msg);
     689    /*@ end vars *************************************************** */
     690
     691    if (bkpinfo->media_device == NULL) {
     692        log_it("Unable to open in from NULL device");
     693        return (1);
     694    }
     695
     696    paranoid_pclose(g_tape_stream);
     697    sync();
     698    sync();
     699    sync();
     700    log_it("Next tape requested.");
     701    insist_on_this_tape_number(g_current_media_number + 1); // will increment it, too
     702    log_it("Opening IN the next tape");
     703    if (!(g_tape_stream = open_device_via_buffer(bkpinfo->media_device, 'r', bkpinfo->internal_tape_block_size))) {
     704        log_OS_error(g_tape_fifo);
     705        log_to_screen("Cannot openin stream device");
     706        return (1);
     707    }
     708    g_tape_posK = 0;
     709    g_sigpipe = FALSE;
     710    res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
     711    if (ctrlchr != BLK_START_OF_TAPE) {
     712        wrong_marker(BLK_START_OF_TAPE, ctrlchr);
     713    }
     714    res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
     715    if (ctrlchr != BLK_START_OF_BACKUP) {
     716        wrong_marker(BLK_START_OF_BACKUP, ctrlchr);
     717    } else {
     718        log_msg(3, "Next tape opened OK. Whoopee!");
     719    }
     720    paranoid_free(sz_msg);
     721    return (res);
     722}
     723
    563724
    564725
     
    14041565
    14051566/**
    1406  * Seek through the stream until we find a header block where the NAME field matches
    1407  * @p the_file_I_was_reading. This is useful if you've just started reading from
    1408  * a new tape and want to find the file you were reading when the tape ended.
    1409  * @param the_file_I_was_reading File name to look for.
    1410  * @return 0 for success, nonzero for failure.
    1411  */
    1412 int skip_incoming_files_until_we_find_this_one(char
    1413                                                *the_file_I_was_reading)
    1414 {
    1415     char *pA;
    1416     char *pB;
    1417     int res;
    1418     int ctrl_chr;
    1419     char *temp_fname;
    1420     char *datablock;
    1421     long long temp_size, size;
    1422     long bytes_to_write;
    1423 
    1424     datablock = malloc(TAPE_BLOCK_SIZE);
    1425     malloc_string(temp_fname);
    1426     pB = strrchr(the_file_I_was_reading, '/');
    1427     if (pB) {
    1428         pB++;
    1429     } else {
    1430         pB = the_file_I_was_reading;
    1431     }
    1432     log_msg(1, "skip_incoming_..(%s)", pB);
    1433     log_msg(2, "Looking for initial START_AN_AFIO_OR_SLICE");
    1434     ctrl_chr = -1;
    1435     while (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
    1436         res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
    1437         if (res) {
    1438             // FIXME
    1439         }
    1440         if (ctrl_chr == BLK_START_AN_AFIO_OR_SLICE) {
    1441             break;
    1442         }
    1443         log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
    1444         wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
    1445         log_msg(3, "Still trying to re-sync w/ tape");
    1446     }
    1447     while (ctrl_chr != BLK_START_FILE) {
    1448         res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
    1449         if (res) {
    1450             //FIXME
    1451         }
    1452         if (ctrl_chr == BLK_START_FILE) {
    1453             break;
    1454         }
    1455         log_msg(1, "%lld %s %c", temp_size, temp_fname, ctrl_chr);
    1456         wrong_marker(BLK_START_FILE, ctrl_chr);
    1457         log_msg(3, "Still trying to re-sync w/ tape");
    1458     }
    1459     pA = strrchr(temp_fname, '/');
    1460     if (pA) {
    1461         pA++;
    1462     } else {
    1463         pA = temp_fname;
    1464     }
    1465     pB = strrchr(the_file_I_was_reading, '/');
    1466     if (pB) {
    1467         pB++;
    1468     } else {
    1469         pB = the_file_I_was_reading;
    1470     }
    1471     while (strcmp(pA, pB)) {
    1472         log_msg(6, "Skipping %s (it's not %s)", temp_fname, the_file_I_was_reading);
    1473         for (size = temp_size; size > 0; size -= bytes_to_write) {
    1474             bytes_to_write =
    1475                 (size < TAPE_BLOCK_SIZE) ? (long) size : TAPE_BLOCK_SIZE;
    1476             if (fread(datablock, 1, (size_t) TAPE_BLOCK_SIZE, g_tape_stream)) {
    1477                 // FIXME - needs error-checking and -catching
    1478             }
    1479         }
    1480         res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
    1481         if (ctrl_chr != BLK_STOP_FILE) {
    1482             wrong_marker(BLK_STOP_FILE, ctrl_chr);
    1483         }
    1484         res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
    1485         if (ctrl_chr != BLK_STOP_AN_AFIO_OR_SLICE) {
    1486             wrong_marker(BLK_STOP_AN_AFIO_OR_SLICE, ctrl_chr);
    1487         }
    1488         res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
    1489         if (ctrl_chr != BLK_START_AN_AFIO_OR_SLICE) {
    1490             wrong_marker(BLK_START_AN_AFIO_OR_SLICE, ctrl_chr);
    1491         }
    1492         res = read_header_block_from_stream(&temp_size, temp_fname, &ctrl_chr);
    1493         if (ctrl_chr != BLK_START_FILE) {
    1494             wrong_marker(BLK_START_FILE, ctrl_chr);
    1495         }
    1496         pA = strrchr(temp_fname, '/');
    1497         if (pA) {
    1498             pA++;
    1499         } else {
    1500             pA = temp_fname;
    1501         }
    1502         pB = strrchr(the_file_I_was_reading, '/');
    1503         if (pB) {
    1504             pB++;
    1505         } else {
    1506             pB = the_file_I_was_reading;
    1507         }
    1508     }
    1509     log_msg(2, "Reading %s (it matches %s)", temp_fname, the_file_I_was_reading);
    1510     paranoid_free(temp_fname);
    1511     paranoid_free(datablock);
    1512     return (0);
    1513 }
    1514 
    1515 
    1516 /**
    1517  * Start to read from the next tape. Assumes the user has already inserted it.
    1518  * @param bkpinfo The backup information structure. @c bkpinfo->media_device is the only field used.
    1519  * @return 0 for success, nonzero for failure.
    1520  */
    1521 int start_to_read_from_next_tape()
    1522 {
    1523     /*@ int ********************************************************* */
    1524     int res = 0;
    1525     char *sz_msg;
    1526     int ctrlchr;
    1527     long long temp_size;
    1528     malloc_string(sz_msg);
    1529     /*@ end vars *************************************************** */
    1530 
    1531     if (bkpinfo->media_device == NULL) {
    1532         log_it("Unable to open in from NULL device");
    1533         return (1);
    1534     }
    1535 
    1536     paranoid_pclose(g_tape_stream);
    1537     sync();
    1538     sync();
    1539     sync();
    1540     log_it("Next tape requested.");
    1541     insist_on_this_tape_number(g_current_media_number + 1); // will increment it, too
    1542     log_it("Opening IN the next tape");
    1543     if (!(g_tape_stream = open_device_via_buffer(bkpinfo->media_device, 'r', bkpinfo->internal_tape_block_size))) {
    1544         log_OS_error(g_tape_fifo);
    1545         log_to_screen("Cannot openin stream device");
    1546         return (1);
    1547     }
    1548     g_tape_posK = 0;
    1549     g_sigpipe = FALSE;
    1550     res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
    1551     if (ctrlchr != BLK_START_OF_TAPE) {
    1552         wrong_marker(BLK_START_OF_TAPE, ctrlchr);
    1553     }
    1554     res += read_header_block_from_stream(&temp_size, sz_msg, &ctrlchr); /* just in case */
    1555     if (ctrlchr != BLK_START_OF_BACKUP) {
    1556         wrong_marker(BLK_START_OF_BACKUP, ctrlchr);
    1557     } else {
    1558         log_msg(3, "Next tape opened OK. Whoopee!");
    1559     }
    1560     paranoid_free(sz_msg);
    1561     return (res);
    1562 }
    1563 
    1564 
    1565 
    1566 /**
    15671567 * Copy a file from the currently opened stream (CD or tape) to the stream indicated
    15681568 * by @p fout.
Note: See TracChangeset for help on using the changeset viewer.