Switch to unified view

a/src/utils/circache.cpp b/src/utils/circache.cpp
...
...
633
    LOGDEB2(("CCScanHookRecord::takeone: offs %lld padsize %lld\n", 
633
    LOGDEB2(("CCScanHookRecord::takeone: offs %lld padsize %lld\n", 
634
         headoffs, padsize));
634
         headoffs, padsize));
635
        return Continue;
635
        return Continue;
636
    }
636
    }
637
};
637
};
638
639
string CirCache::getpath()
640
{
641
    return m_d->datafn(m_dir);
642
}
638
643
639
bool CirCache::create(off_t maxsize, int flags)
644
bool CirCache::create(off_t maxsize, int flags)
640
{
645
{
641
    LOGDEB(("CirCache::create: [%s] maxsz %lld flags 0x%x\n", 
646
    LOGDEB(("CirCache::create: [%s] maxsz %lld flags 0x%x\n", 
642
        m_dir.c_str(), maxsize, flags));
647
        m_dir.c_str(), maxsize, flags));
...
...
1294
#include <stdio.h>
1299
#include <stdio.h>
1295
#include <stdlib.h>
1300
#include <stdlib.h>
1296
#include <unistd.h>
1301
#include <unistd.h>
1297
#include <errno.h>
1302
#include <errno.h>
1298
#include <string.h>
1303
#include <string.h>
1304
#include <sys/stat.h>
1305
#include <sys/types.h>
1299
1306
1300
#include <string>
1307
#include <string>
1301
#include <iostream>
1308
#include <iostream>
1302
1309
1303
#include "circache.h"
1310
#include "circache.h"
...
...
1305
#include "conftree.h"
1312
#include "conftree.h"
1306
#include "readfile.h"
1313
#include "readfile.h"
1307
#include "debuglog.h"
1314
#include "debuglog.h"
1308
1315
1309
using namespace std;
1316
using namespace std;
1317
1318
1319
bool resizecc(const string& dir, int newmbs)
1320
{
1321
    RefCntr<CirCache> occ(new CirCache(dir));
1322
    string ofn = occ->getpath();
1323
1324
    string backupfn = ofn + ".orig";
1325
    if (access(backupfn.c_str(), 0) >= 0) {
1326
        cerr << "Backup file " << backupfn << 
1327
            " exists, please move it out of the way" << endl;
1328
        return false;
1329
    }
1330
1331
    if (!occ->open(CirCache::CC_OPWRITE)) {
1332
        cerr << "Open failed in " << dir << " : " << occ->getReason() << endl;
1333
        return false;
1334
    }
1335
    string tmpdir = path_cat(dir, "tmp");
1336
    if (access(tmpdir.c_str(), 0) < 0) {
1337
        if (mkdir(tmpdir.c_str(), 0700) < 0) {
1338
            cerr << "Cant create temporary directory " << tmpdir << " ";
1339
            perror("mkdir");
1340
            return false;
1341
        }
1342
    }
1343
1344
    RefCntr<CirCache> ncc(new CirCache(tmpdir));
1345
    string nfn = ncc->getpath();
1346
    if (!ncc->create(off_t(newmbs) * 1000 * 1024, 
1347
                     CirCache::CC_CRUNIQUE | CirCache::CC_CRTRUNCATE)) {
1348
        cerr << "Cant create new file in " << tmpdir << " : " << 
1349
            ncc->getReason() << endl;
1350
  return false;
1351
    }
1352
1353
    bool eof = false;
1354
    occ->rewind(eof);
1355
    int nentries = 0;
1356
    while (!eof) {
1357
        string udi, sdic, data;
1358
        if (!occ->getCurrent(udi, sdic, data)) {
1359
            cerr << "getCurrent failed: " << occ->getReason() << endl;
1360
            return false;
1361
        }
1362
        // Shouldn't getcurrent deal with this ?
1363
        if (sdic.size() == 0) {
1364
            //cerr << "Skip empty entry" << endl;
1365
            occ->next(eof);
1366
            continue;
1367
        }
1368
        ConfSimple dic(sdic);
1369
        if (!dic.ok()) {
1370
            cerr << "Could not parse entry attributes dic" << endl;
1371
            return false;
1372
        }
1373
        //cerr << "UDI: " << udi << endl;
1374
        if (!ncc->put(udi, &dic, data)) {
1375
            cerr << "put failed: " << ncc->getReason() << " sdic [" << sdic <<
1376
                "]" << endl;
1377
            return false;
1378
        }
1379
        nentries++;
1380
        occ->next(eof);
1381
    }
1382
1383
    // Done with our objects here, there is no close() method, so delete them
1384
    occ.release();
1385
    ncc.release();
1386
1387
    if (rename(ofn.c_str(), backupfn.c_str()) < 0) {
1388
        cerr << "Could not create backup " << backupfn << " : ";
1389
        perror("rename");
1390
        return false;
1391
    }
1392
    cout << "Created backup file " << backupfn << endl;
1393
    if (rename(nfn.c_str(), ofn.c_str()) < 0) {
1394
        cerr << "Could not rename new file from " << nfn << " to " << ofn << " : ";
1395
        perror("rename");
1396
        return false;
1397
    }
1398
    cout << "Resize done, copied " << nentries << " entries " << endl;
1399
    return true;
1400
}
1401
1310
1402
1311
static char *thisprog;
1403
static char *thisprog;
1312
1404
1313
static char usage [] =
1405
static char usage [] =
1314
    " -c [-u] <dirname> : create\n"
1406
    " -c [-u] <dirname> : create\n"
1315
    " -p <dirname> <apath> [apath ...] : put files\n"
1407
    " -p <dirname> <apath> [apath ...] : put files\n"
1316
    " -d <dirname> : dump\n"
1408
    " -d <dirname> : dump\n"
1317
    " -g [-i instance] [-D] <dirname> <udi>: get\n"
1409
    " -g [-i instance] [-D] <dirname> <udi>: get\n"
1318
    "   -D: also dump data\n"
1410
    "   -D: also dump data\n"
1319
    " -e <dirname> <udi> : erase\n"
1411
    " -e <dirname> <udi> : erase\n"
1412
    " -s <dirname> <newmbs> : resize\n"
1320
    ;
1413
    ;
1321
static void
1414
static void
1322
Usage(FILE *fp = stderr)
1415
Usage(FILE *fp = stderr)
1323
{
1416
{
1324
    fprintf(fp, "%s: usage:\n%s", thisprog, usage);
1417
    fprintf(fp, "%s: usage:\n%s", thisprog, usage);
...
...
1333
#define OPT_d     0x20
1426
#define OPT_d     0x20
1334
#define OPT_i     0x40
1427
#define OPT_i     0x40
1335
#define OPT_D     0x80
1428
#define OPT_D     0x80
1336
#define OPT_u     0x100
1429
#define OPT_u     0x100
1337
#define OPT_e     0x200
1430
#define OPT_e     0x200
1431
#define OPT_s     0x400
1338
1432
1339
int main(int argc, char **argv)
1433
int main(int argc, char **argv)
1340
{
1434
{
1341
    int instance = -1;
1435
    int instance = -1;
1342
1436
...
...
1349
        /* Cas du "adb - core" */
1443
        /* Cas du "adb - core" */
1350
        Usage();
1444
        Usage();
1351
    while (**argv)
1445
    while (**argv)
1352
        switch (*(*argv)++) {
1446
        switch (*(*argv)++) {
1353
        case 'c':   op_flags |= OPT_c; break;
1447
        case 'c':   op_flags |= OPT_c; break;
1448
      case 'D':   op_flags |= OPT_D; break;
1449
      case 'd':   op_flags |= OPT_d; break;
1354
        case 'e':   op_flags |= OPT_e; break;
1450
        case 'e':   op_flags |= OPT_e; break;
1355
      case 'p':   op_flags |= OPT_p; break;
1356
        case 'g':   op_flags |= OPT_g; break;
1451
        case 'g':   op_flags |= OPT_g; break;
1357
      case 'd':   op_flags |= OPT_d; break;
1358
      case 'D':   op_flags |= OPT_D; break;
1359
      case 'u':   op_flags |= OPT_u; break;
1360
        case 'i':   op_flags |= OPT_i; if (argc < 2)  Usage();
1452
        case 'i':   op_flags |= OPT_i; if (argc < 2)  Usage();
1361
        if ((sscanf(*(++argv), "%d", &instance)) != 1) 
1453
        if ((sscanf(*(++argv), "%d", &instance)) != 1) 
1362
            Usage(); 
1454
            Usage(); 
1363
        argc--; 
1455
        argc--; 
1364
        goto b1;
1456
        goto b1;
1457
      case 'p':   op_flags |= OPT_p; break;
1458
      case 's':   op_flags |= OPT_s; break;
1459
      case 'u':   op_flags |= OPT_u; break;
1365
        default: Usage();   break;
1460
        default: Usage();   break;
1366
        }
1461
        }
1367
    b1: argc--; argv++;
1462
    b1: argc--; argv++;
1368
    }
1463
    }
1369
1464
1370
    DebugLog::getdbl()->setloglevel(DEBDEB1);
1465
    DebugLog::getdbl()->setloglevel(DEBERR);
1371
    DebugLog::setfilename("stderr");
1466
    DebugLog::setfilename("stderr");
1372
1467
1373
    if (argc < 1)
1468
    if (argc < 1)
1374
    Usage();
1469
    Usage();
1375
    string dir = *argv++;argc--;
1470
    string dir = *argv++;argc--;
...
...
1382
        flags |= CirCache::CC_CRUNIQUE;
1477
        flags |= CirCache::CC_CRUNIQUE;
1383
    if (!cc.create(100*1024, flags)) {
1478
    if (!cc.create(100*1024, flags)) {
1384
        cerr << "Create failed:" << cc.getReason() << endl;
1479
        cerr << "Create failed:" << cc.getReason() << endl;
1385
        exit(1);
1480
        exit(1);
1386
    }
1481
    }
1482
    } else if (op_flags & OPT_s) {
1483
        if (argc != 1) {
1484
            Usage();
1485
        }
1486
        int newmbs = atoi(*argv++);argc--;
1487
        if (!resizecc(dir, newmbs)) {
1488
            exit(1);
1489
        }
1387
    } else if (op_flags & OPT_p) {
1490
    } else if (op_flags & OPT_p) {
1388
    if (argc < 1)
1491
    if (argc < 1)
1389
        Usage();
1492
        Usage();
1390
    if (!cc.open(CirCache::CC_OPWRITE)) {
1493
    if (!cc.open(CirCache::CC_OPWRITE)) {
1391
        cerr << "Open failed: " << cc.getReason() << endl;
1494
        cerr << "Open failed: " << cc.getReason() << endl;