Switch to unified view

a/src/utils/circache.cpp b/src/utils/circache.cpp
...
...
186
    bool khEnter(const string& udi, off_t ofs)
186
    bool khEnter(const string& udi, off_t ofs)
187
    {
187
    {
188
        UdiH h(udi);
188
        UdiH h(udi);
189
189
190
        LOGDEB2(("Circache::khEnter: h %s offs %lu udi [%s]\n", 
190
        LOGDEB2(("Circache::khEnter: h %s offs %lu udi [%s]\n", 
191
                h.asHexString().c_str(), (ULONG)ofs, udi.c_str()));
191
       h.asHexString().c_str(), (ULONG)ofs, udi.c_str()));
192
192
193
        pair<kh_type::iterator, kh_type::iterator> p = m_ofskh.equal_range(h);
193
        pair<kh_type::iterator, kh_type::iterator> p = m_ofskh.equal_range(h);
194
194
195
        if (p.first != m_ofskh.end() && p.first->first == h) {
195
        if (p.first != m_ofskh.end() && p.first->first == h) {
196
            for (kh_type::iterator it = p.first; it != p.second; it++) {
196
            for (kh_type::iterator it = p.first; it != p.second; it++) {
197
                LOGDEB2(("Circache::khEnter: col h %s, ofs %lu\n", 
197
                LOGDEB2(("Circache::khEnter: col h %s, ofs %lu\n", 
198
                        it->first.asHexString().c_str(),
198
           it->first.asHexString().c_str(),
199
                        (ULONG)it->second));
199
           (ULONG)it->second));
200
                if (it->second == ofs) {
200
                if (it->second == ofs) {
201
                    // (h,offs) already there. Happens
201
                    // (h,offs) already there. Happens
202
                    LOGDEB2(("Circache::khEnter: already there\n"));
202
                    LOGDEB2(("Circache::khEnter: already there\n"));
203
                    return true;
203
                    return true;
204
                }
204
                }
...
...
225
        ofss.clear();
225
        ofss.clear();
226
226
227
        UdiH h(udi);
227
        UdiH h(udi);
228
228
229
        LOGDEB2(("Circache::khFind: h %s udi [%s]\n", 
229
        LOGDEB2(("Circache::khFind: h %s udi [%s]\n", 
230
                h.asHexString().c_str(), udi.c_str()));
230
       h.asHexString().c_str(), udi.c_str()));
231
231
232
        pair<kh_type::iterator, kh_type::iterator> p = m_ofskh.equal_range(h);
232
        pair<kh_type::iterator, kh_type::iterator> p = m_ofskh.equal_range(h);
233
233
234
#if 0
234
#if 0
235
        if (p.first == m_ofskh.end()) LOGDEB(("KHFIND: FIRST END()\n"));
235
        if (p.first == m_ofskh.end()) LOGDEB(("KHFIND: FIRST END()\n"));
...
...
619
    LOGDEB(("CirCache::create: [%s] flags 0x%x\n", m_dir.c_str(), flags));
619
    LOGDEB(("CirCache::create: [%s] flags 0x%x\n", m_dir.c_str(), flags));
620
    if (m_d == 0) {
620
    if (m_d == 0) {
621
    LOGERR(("CirCache::create: null data\n"));
621
    LOGERR(("CirCache::create: null data\n"));
622
    return false;
622
    return false;
623
    }
623
    }
624
625
    {
624
    struct stat st;
626
  struct stat st;
625
    if (stat(m_dir.c_str(), &st) < 0) {
627
  if (stat(m_dir.c_str(), &st) < 0) {
628
      // Directory does not exist, create it
626
        if (mkdir(m_dir.c_str(), 0777) < 0) {
629
      if (mkdir(m_dir.c_str(), 0777) < 0) {
627
            m_d->m_reason << "CirCache::create: mkdir(" << m_dir << 
630
      m_d->m_reason << "CirCache::create: mkdir(" << m_dir << 
628
                ") failed" << " errno " << errno;
631
          ") failed" << " errno " << errno;
629
            return false;
632
      return false;
630
        }
633
      }
631
    } else {
634
  } else {
635
      // Directory exists but file might still not exist:
636
      // e.g. the user is using a non default directory and
637
      // created it for us.
638
      if (access(m_d->datafn(m_dir).c_str(), 0) >= 0) {
639
      // File exists, switch to "open" mode, except if we're told to 
640
      // truncate.
632
        if (!(flags & CC_CRTRUNCATE))
641
      if (!(flags & CC_CRTRUNCATE)) {
633
            return open(CC_OPWRITE);
642
          return open(CC_OPWRITE);
643
      }
644
      }
645
      // Else fall through to create file
646
  }
634
    }
647
    }
635
648
636
    if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), 
649
    if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), 
637
                          O_CREAT | O_RDWR | O_TRUNC, 
650
              O_CREAT | O_RDWR | O_TRUNC, 0666)) < 0) {
638
                          0666)) < 0) {
639
        m_d->m_reason << "CirCache::create: open/creat(" << 
651
        m_d->m_reason << "CirCache::create: open/creat(" << 
640
            m_d->datafn(m_dir) << ") failed " << "errno " << errno;
652
            m_d->datafn(m_dir) << ") failed " << "errno " << errno;
641
            return false;
653
  return false;
642
    }
654
    }
643
655
644
    m_d->m_maxsize = m_maxsize;
656
    m_d->m_maxsize = m_maxsize;
645
    m_d->m_oheadoffs = CIRCACHE_FIRSTBLOCK_SIZE;
657
    m_d->m_oheadoffs = CIRCACHE_FIRSTBLOCK_SIZE;
646
    m_d->m_uniquentries = ((flags & CC_CRUNIQUE) != 0);
658
    m_d->m_uniquentries = ((flags & CC_CRUNIQUE) != 0);
...
...
665
677
666
    if (m_d->m_fd >= 0)
678
    if (m_d->m_fd >= 0)
667
        ::close(m_d->m_fd);
679
        ::close(m_d->m_fd);
668
680
669
    if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), 
681
    if ((m_d->m_fd = ::open(m_d->datafn(m_dir).c_str(), 
670
                          mode == CC_OPREAD ? O_RDONLY : O_RDWR)) < 0) {
682
              mode == CC_OPREAD ? O_RDONLY : O_RDWR)) < 0) {
671
        m_d->m_reason << "CirCache::open: open(" << m_d->datafn(m_dir) << 
683
        m_d->m_reason << "CirCache::open: open(" << m_d->datafn(m_dir) << 
672
            ") failed " << "errno " << errno;
684
            ") failed " << "errno " << errno;
673
        return false;
685
        return false;
674
    }
686
    }
675
    return m_d->readfirstblock();
687
    return m_d->readfirstblock();
...
...
874
public:
886
public:
875
    UINT sizewanted;
887
    UINT sizewanted;
876
    UINT sizeseen;
888
    UINT sizeseen;
877
    vector<pair<string, off_t> > squashed_udis;
889
    vector<pair<string, off_t> > squashed_udis;
878
    CCScanHookSpacer(int sz)
890
    CCScanHookSpacer(int sz)
879
        : sizewanted(sz), sizeseen(0) {assert(sz > 0);}
891
    : sizewanted(sz), sizeseen(0) {assert(sz > 0);}
880
892
881
    virtual status takeone(off_t offs, const string& udi, 
893
    virtual status takeone(off_t offs, const string& udi, 
882
                           const EntryHeaderData& d)
894
                           const EntryHeaderData& d)
883
    {
895
    {
884
        LOGDEB2(("Circache:ScanSpacer:off %u dcsz %u dtsz %u pdsz %u udi[%s]\n",
896
        LOGDEB2(("Circache:ScanSpacer:off %u dcsz %u dtsz %u pdsz %u udi[%s]\n",
885
                (UINT)offs, d.dicsize, d.datasize, d.padsize, udi.c_str()));
897
       (UINT)offs, d.dicsize, d.datasize, d.padsize, udi.c_str()));
886
        sizeseen += CIRCACHE_HEADER_SIZE + d.dicsize + d.datasize + d.padsize;
898
        sizeseen += CIRCACHE_HEADER_SIZE + d.dicsize + d.datasize + d.padsize;
887
        squashed_udis.push_back(make_pair(udi, offs));
899
        squashed_udis.push_back(make_pair(udi, offs));
888
        if (sizeseen >= sizewanted)
900
        if (sizeseen >= sizewanted)
889
            return Stop;
901
            return Stop;
890
        return Continue;
902
        return Continue;
...
...
1029
    }
1041
    }
1030
1042
1031
    char head[CIRCACHE_HEADER_SIZE];
1043
    char head[CIRCACHE_HEADER_SIZE];
1032
    memset(head, 0, CIRCACHE_HEADER_SIZE);
1044
    memset(head, 0, CIRCACHE_HEADER_SIZE);
1033
    snprintf(head, CIRCACHE_HEADER_SIZE, 
1045
    snprintf(head, CIRCACHE_HEADER_SIZE, 
1034
        headerformat, dic.size(), datalen, npadsize, flags);
1046
         headerformat, dic.size(), datalen, npadsize, flags);
1035
    struct iovec vecs[3];
1047
    struct iovec vecs[3];
1036
    vecs[0].iov_base = head;
1048
    vecs[0].iov_base = head;
1037
    vecs[0].iov_len = CIRCACHE_HEADER_SIZE;
1049
    vecs[0].iov_len = CIRCACHE_HEADER_SIZE;
1038
    vecs[1].iov_base = (void *)dic.c_str();
1050
    vecs[1].iov_base = (void *)dic.c_str();
1039
    vecs[1].iov_len = dic.size();
1051
    vecs[1].iov_len = dic.size();
...
...
1199
        LOGDEB2(("InflateToDynBuf: avail_in %d total_in %d avail_out %d "
1211
        LOGDEB2(("InflateToDynBuf: avail_in %d total_in %d avail_out %d "
1200
                 "total_out %d\n", d_stream.avail_in, d_stream.total_in,
1212
                 "total_out %d\n", d_stream.avail_in, d_stream.total_in,
1201
                 d_stream.avail_out, d_stream.total_out));
1213
                 d_stream.avail_out, d_stream.total_out));
1202
        if (d_stream.avail_out == 0) {
1214
        if (d_stream.avail_out == 0) {
1203
            if ((outp = (char*)allocmem(outp, inlen, &alloc, 
1215
            if ((outp = (char*)allocmem(outp, inlen, &alloc, 
1204
                                                 imul, mxinc)) == 0) {
1216
                  imul, mxinc)) == 0) {
1205
                LOGERR(("Inflate: out of memory, current alloc %d\n", 
1217
                LOGERR(("Inflate: out of memory, current alloc %d\n", 
1206
                        alloc*inlen));
1218
                        alloc*inlen));
1207
                inflateEnd(&d_stream);
1219
                inflateEnd(&d_stream);
1208
                return false;
1220
                return false;
1209
            } else {
1221
            } else {
...
...
1253
using namespace std;
1265
using namespace std;
1254
1266
1255
static char *thisprog;
1267
static char *thisprog;
1256
1268
1257
static char usage [] =
1269
static char usage [] =
1258
" -c [-u] <dirname> : create\n"
1270
    " -c [-u] <dirname> : create\n"
1259
" -p <dirname> <apath> [apath ...] : put files\n"
1271
    " -p <dirname> <apath> [apath ...] : put files\n"
1260
" -d <dirname> : dump\n"
1272
    " -d <dirname> : dump\n"
1261
" -g [-i instance] [-D] <dirname> <udi>: get\n"
1273
    " -g [-i instance] [-D] <dirname> <udi>: get\n"
1262
"   -D: also dump data\n"
1274
    "   -D: also dump data\n"
1263
" -e <dirname> <udi> : erase\n"
1275
    " -e <dirname> <udi> : erase\n"
1264
;
1276
    ;
1265
static void
1277
static void
1266
Usage(FILE *fp = stderr)
1278
Usage(FILE *fp = stderr)
1267
{
1279
{
1268
    fprintf(fp, "%s: usage:\n%s", thisprog, usage);
1280
    fprintf(fp, "%s: usage:\n%s", thisprog, usage);
1269
    exit(1);
1281
    exit(1);
...
...
1280
#define OPT_u     0x100
1292
#define OPT_u     0x100
1281
#define OPT_e     0x200
1293
#define OPT_e     0x200
1282
1294
1283
int main(int argc, char **argv)
1295
int main(int argc, char **argv)
1284
{
1296
{
1285
  int instance = -1;
1297
    int instance = -1;
1286
1298
1287
  thisprog = argv[0];
1299
    thisprog = argv[0];
1288
  argc--; argv++;
1300
    argc--; argv++;
1289
1301
1290
  while (argc > 0 && **argv == '-') {
1302
    while (argc > 0 && **argv == '-') {
1291
    (*argv)++;
1303
  (*argv)++;
1292
    if (!(**argv))
1304
  if (!(**argv))
1293
      /* Cas du "adb - core" */
1305
      /* Cas du "adb - core" */
1294
      Usage();
1306
      Usage();
1295
    while (**argv)
1307
  while (**argv)
1296
      switch (*(*argv)++) {
1308
      switch (*(*argv)++) {
1297
      case 'c':  op_flags |= OPT_c; break;
1309
      case 'c':  op_flags |= OPT_c; break;
1298
      case 'e':  op_flags |= OPT_e; break;
1310
      case 'e':  op_flags |= OPT_e; break;
1299
      case 'p':  op_flags |= OPT_p; break;
1311
      case 'p':  op_flags |= OPT_p; break;
1300
      case 'g':  op_flags |= OPT_g; break;
1312
      case 'g':  op_flags |= OPT_g; break;
1301
      case 'd':  op_flags |= OPT_d; break;
1313
      case 'd':  op_flags |= OPT_d; break;
1302
      case 'D':  op_flags |= OPT_D; break;
1314
      case 'D':  op_flags |= OPT_D; break;
1303
      case 'u':  op_flags |= OPT_u; break;
1315
      case 'u':  op_flags |= OPT_u; break;
1304
      case 'i':  op_flags |= OPT_i; if (argc < 2)  Usage();
1316
      case 'i':  op_flags |= OPT_i; if (argc < 2)  Usage();
1305
    if ((sscanf(*(++argv), "%d", &instance)) != 1) 
1317
     if ((sscanf(*(++argv), "%d", &instance)) != 1) 
1306
      Usage(); 
1318
          Usage(); 
1307
    argc--; 
1319
     argc--; 
1308
    goto b1;
1320
     goto b1;
1309
      default: Usage();  break;
1321
      default: Usage();  break;
1310
      }
1322
      }
1311
  b1: argc--; argv++;
1323
    b1: argc--; argv++;
1312
  }
1324
    }
1313
1325
1314
  DebugLog::getdbl()->setloglevel(DEBDEB1);
1326
    DebugLog::getdbl()->setloglevel(DEBDEB1);
1315
  DebugLog::setfilename("stderr");
1327
    DebugLog::setfilename("stderr");
1316
1328
1317
  if (argc < 1)
1329
    if (argc < 1)
1318
    Usage();
1330
  Usage();
1319
  string dir = *argv++;argc--;
1331
    string dir = *argv++;argc--;
1320
1332
1321
  CirCache cc(dir);
1333
    CirCache cc(dir);
1322
1334
1323
  if (op_flags & OPT_c) {
1335
    if (op_flags & OPT_c) {
1324
      int flags = 0;
1336
  int flags = 0;
1325
      if (op_flags & OPT_u)
1337
  if (op_flags & OPT_u)
1326
          flags |= CirCache::CC_CRUNIQUE;
1338
      flags |= CirCache::CC_CRUNIQUE;
1327
      if (!cc.create(100*1024, flags)) {
1339
  if (!cc.create(100*1024, flags)) {
1328
          cerr << "Create failed:" << cc.getReason() << endl;
1340
      cerr << "Create failed:" << cc.getReason() << endl;
1329
          exit(1);
1341
      exit(1);
1330
      }
1342
  }
1331
  } else if (op_flags & OPT_p) {
1343
    } else if (op_flags & OPT_p) {
1332
      if (argc < 1)
1344
  if (argc < 1)
1333
          Usage();
1345
      Usage();
1334
      if (!cc.open(CirCache::CC_OPWRITE)) {
1346
  if (!cc.open(CirCache::CC_OPWRITE)) {
1335
          cerr << "Open failed: " << cc.getReason() << endl;
1347
      cerr << "Open failed: " << cc.getReason() << endl;
1336
          exit(1);
1348
      exit(1);
1337
      }
1349
  }
1338
      while (argc) {
1350
  while (argc) {
1339
          string fn = *argv++;argc--;
1351
      string fn = *argv++;argc--;
1340
          char dic[1000];
1352
      char dic[1000];
1341
          string data, reason;
1353
      string data, reason;
1342
          if (!file_to_string(fn, data, &reason)) {
1354
      if (!file_to_string(fn, data, &reason)) {
1343
              cerr << "File_to_string: " << reason << endl;
1355
      cerr << "File_to_string: " << reason << endl;
1344
              exit(1);
1356
      exit(1);
1345
          }
1357
      }
1346
          string udi;
1358
      string udi;
1347
          make_udi(fn, "", udi);
1359
      make_udi(fn, "", udi);
1348
          sprintf(dic, "#whatever...\nmimetype = text/plain\nudi=%s\n", 
1360
      sprintf(dic, "#whatever...\nmimetype = text/plain\nudi=%s\n", 
1349
                  udi.c_str());
1361
          udi.c_str());
1350
          string sdic;
1362
      string sdic;
1351
          sdic.assign(dic, strlen(dic));
1363
      sdic.assign(dic, strlen(dic));
1352
          ConfSimple conf(sdic);
1364
      ConfSimple conf(sdic);
1353
   
1365
   
1354
          if (!cc.put(udi, &conf, data, 0)) {
1366
      if (!cc.put(udi, &conf, data, 0)) {
1355
              cerr << "Put failed: " << cc.getReason() << endl;
1367
      cerr << "Put failed: " << cc.getReason() << endl;
1356
              cerr << "conf: ["; conf.write(cerr); cerr << "]" << endl;
1368
      cerr << "conf: ["; conf.write(cerr); cerr << "]" << endl;
1357
              exit(1);
1369
      exit(1);
1358
          }
1359
      }
1370
      }
1371
  }
1360
      cc.open(CirCache::CC_OPREAD);
1372
  cc.open(CirCache::CC_OPREAD);
1361
  } else if (op_flags & OPT_g) {
1373
    } else if (op_flags & OPT_g) {
1362
      if (!cc.open(CirCache::CC_OPREAD)) {
1374
  if (!cc.open(CirCache::CC_OPREAD)) {
1363
          cerr << "Open failed: " << cc.getReason() << endl;
1375
      cerr << "Open failed: " << cc.getReason() << endl;
1364
          exit(1);
1376
      exit(1);
1365
      }
1377
  }
1366
      while (argc) {
1378
  while (argc) {
1367
          string udi = *argv++;argc--;
1379
      string udi = *argv++;argc--;
1368
          string dic, data;
1380
      string dic, data;
1369
          if (!cc.get(udi, dic, data, instance)) {
1381
      if (!cc.get(udi, dic, data, instance)) {
1370
              cerr << "Get failed: " << cc.getReason() << endl;
1382
      cerr << "Get failed: " << cc.getReason() << endl;
1371
              exit(1);
1383
      exit(1);
1372
          }
1384
      }
1373
          cout << "Dict: [" << dic << "]" << endl;
1385
      cout << "Dict: [" << dic << "]" << endl;
1374
          if (op_flags & OPT_D)
1386
      if (op_flags & OPT_D)
1375
              cout << "Data: [" << data << "]" << endl;
1387
      cout << "Data: [" << data << "]" << endl;
1376
      }
1388
  }
1377
  } else if (op_flags & OPT_e) {
1389
    } else if (op_flags & OPT_e) {
1378
      if (!cc.open(CirCache::CC_OPWRITE)) {
1390
  if (!cc.open(CirCache::CC_OPWRITE)) {
1379
          cerr << "Open failed: " << cc.getReason() << endl;
1391
      cerr << "Open failed: " << cc.getReason() << endl;
1380
          exit(1);
1392
      exit(1);
1381
      }
1393
  }
1382
      while (argc) {
1394
  while (argc) {
1383
          string udi = *argv++;argc--;
1395
      string udi = *argv++;argc--;
1384
          string dic, data;
1396
      string dic, data;
1385
          if (!cc.erase(udi)) {
1397
      if (!cc.erase(udi)) {
1386
              cerr << "Erase failed: " << cc.getReason() << endl;
1398
      cerr << "Erase failed: " << cc.getReason() << endl;
1387
              exit(1);
1399
      exit(1);
1388
          }
1389
      }
1400
      }
1401
  }
1390
  } else if (op_flags & OPT_d) {
1402
    } else if (op_flags & OPT_d) {
1391
      if (!cc.open(CirCache::CC_OPREAD)) {
1403
  if (!cc.open(CirCache::CC_OPREAD)) {
1392
          cerr << "Open failed: " << cc.getReason() << endl;
1404
      cerr << "Open failed: " << cc.getReason() << endl;
1393
          exit(1);
1405
      exit(1);
1394
      }
1406
  }
1395
      cc.dump();
1407
  cc.dump();
1396
  } else
1408
    } else
1397
      Usage();
1409
  Usage();
1398
1410
1399
  exit(0);
1411
    exit(0);
1400
}
1412
}
1401
1413
1402
#endif
1414
#endif