Macros

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
#include <cassert>
#include <cctype>
#include <cerrno>
#include <climits>
#include <cstdlib>
#include <cstring>

#include "Alignment/Geners/interface/BinaryArchiveBase.hh"
#include "Alignment/Geners/interface/CatalogIO.hh"
#include "Alignment/Geners/interface/binaryIO.hh"

#ifdef GENERS_BINARY_ARCHIVE_FORMAT_ID
#undef GENERS_BINARY_ARCHIVE_FORMAT_ID
#endif
#define GENERS_BINARY_ARCHIVE_FORMAT_ID (0x1f2e3d4c)

static bool parse_unsigned(std::ostringstream &err, const char *c, unsigned *result) {
  char *endptr;
  errno = 0;
  const unsigned long value = strtoul(c, &endptr, 0);
  if (errno || *endptr != '\0') {
    err << "expected an unsigned integer, got \"" << c << '"';
    if (errno)
      err << ", " << strerror(errno);
    return false;
  }
  if (value > UINT_MAX) {
    err << "unsigned value \"" << c << "\" is out of range";
    return false;
  }
  *result = value;
  return true;
}

static bool parse_int(std::ostringstream &err, const char *c, int *result) {
  char *endptr;
  errno = 0;
  const long value = strtol(c, &endptr, 0);
  if (errno || *endptr != '\0') {
    err << "expected an integer, got \"" << c << '"';
    if (errno)
      err << ", " << strerror(errno);
    return false;
  }
  if (value < INT_MIN || value > INT_MAX) {
    err << "integer value \"" << c << "\" is out of range";
    return false;
  }
  *result = value;
  return true;
}

namespace gs {
  BinaryArchiveBase::BinaryArchiveBase(const char *name, const char *mode)
      : AbsArchive(name),
        mode_(parseMode(mode)),
        errorStream_(nullptr),
        cStream_(nullptr),
        catalog_(nullptr),
        storedEntryId_(nullptr),
        storedLocationId_(nullptr),
        catalogIsSet_(false),
        addCatalogToData_(false) {
    CStringStream::CompressionMode m = CStringStream::NOT_COMPRESSED;
    int compressionLevel = -1;
    unsigned minSizeToCompress = 1024U;
    unsigned bufSize = 1048576U;  // 1024*1024

    std::ostringstream err;
    modeIsValid_ =
        parseArchiveOptions(err, mode, &m, &compressionLevel, &minSizeToCompress, &bufSize, &addCatalogToData_);
    if (modeIsValid_)
      cStream_ = new CStringStream(m, compressionLevel, minSizeToCompress, bufSize);
    else {
      errorStream() << "In BinaryArchiveBase constructor: "
                    << "invalid archive opening mode \"" << mode << '"';
      const std::string &errInfo = err.str();
      if (!errInfo.empty())
        errorStream() << ": " << errInfo;
    }
  }

  void BinaryArchiveBase::releaseClassIds() {
    delete storedEntryId_;
    storedEntryId_ = nullptr;
    delete storedLocationId_;
    storedLocationId_ = nullptr;
  }

  BinaryArchiveBase::~BinaryArchiveBase() {
    releaseClassIds();
    delete errorStream_;
    delete catalog_;
    delete cStream_;
  }

  void BinaryArchiveBase::writeHeader(std::ostream &os) {
    const unsigned format = GENERS_BINARY_ARCHIVE_FORMAT_ID;
    write_pod(os, format);

    // Write some other info
    const unsigned multiplex = addCatalogToData_ ? 1 : 0;
    const unsigned sizeoflong = sizeof(long);
    const unsigned infoword = (sizeoflong << 1) | multiplex;
    write_pod(os, infoword);

    if (multiplex) {
      // Write class ids for CatalogEntry and ItemLocation
      releaseClassIds();
      storedEntryId_ = new ClassId(ClassId::makeId<CatalogEntry>());
      storedEntryId_->write(os);
      storedLocationId_ = new ClassId(ClassId::makeId<ItemLocation>());
      storedLocationId_->write(os);
    }
  }

  bool BinaryArchiveBase::readHeader(std::istream &is) {
    const unsigned expectedFormat = GENERS_BINARY_ARCHIVE_FORMAT_ID;
    is.seekg(0, std::ios_base::beg);
    unsigned format = 0;
    read_pod(is, &format);
    if (format != expectedFormat)
      return false;

    unsigned infoword = 0xffffffff;
    read_pod(is, &infoword);
    const unsigned multiplex = infoword & 0x1U;
    const unsigned sizeoflong = infoword >> 1;

    // The following check will make sure that we are not reading
    // an archive created on a 32-bit machine with a 64-bit system
    // (and otherwise)
    if (sizeoflong != sizeof(long))
      return false;

    addCatalogToData_ = multiplex;
    if (addCatalogToData_) {
      releaseClassIds();
      storedEntryId_ = new ClassId(is, 1);
      storedLocationId_ = new ClassId(is, 1);

      // Can't open this archive for update if the above class ids
      // are obsolete -- otherwise we will loose the capability to
      // restore the catalog
      if (mode_ & std::ios_base::out) {
        const ClassId &entryId = ClassId::makeId<CatalogEntry>();
        const ClassId &locId = ClassId::makeId<ItemLocation>();
        if (entryId != *storedEntryId_ || locId != *storedLocationId_)
          throw IOInvalidData(
              "In gs::BinaryArchiveBase::readHeader: this "
              "archive can no longer be open for update as it was "
              "created using an older version of I/O software");
      }
    }
    return !is.fail();
  }

  void BinaryArchiveBase::openDataFile(std::fstream &stream, const char *filename) {
    assert(filename);
    if (stream.is_open())
      stream.close();
    stream.clear();
    stream.open(filename, mode_);
    if (!stream.is_open())
      throw IOOpeningFailure("gs::BinaryArchiveBase::openDataFile", filename);

    // Do we need to write the header out or to read it in?
    bool writeHead = false;
    if (mode_ & std::ios_base::out) {
      if (mode_ & std::ios_base::trunc)
        writeHead = true;
      else if (isEmptyFile(stream))
        writeHead = true;
    }

    if (writeHead) {
      writeHeader(stream);
      if (stream.fail()) {
        stream.close();
        std::string e =
            "In gs::BinaryArchiveBase::openDataFile: "
            "failed to write archive header to file \"";
        e += filename;
        e += "\"";
        throw IOWriteFailure(e);
      }
    } else {
      if (!readHeader(stream)) {
        const bool failed = stream.fail();
        stream.close();
        std::string e = "In gs::BinaryArchiveBase::openDataFile: ";
        if (failed) {
          e += "could not read archive header from file \"";
          e += filename;
          e += "\"";
          throw IOReadFailure(e);
        } else {
          e += "no valid archive header in file \"";
          e += filename;
          e += "\"";
          throw IOInvalidData(e);
        }
      }
    }
  }

  void BinaryArchiveBase::setCatalog(AbsCatalog *c) {
    if (c) {
      assert(!catalogIsSet_);
      catalogIsSet_ = true;
    }
    delete catalog_;
    catalog_ = c;
  }

  void BinaryArchiveBase::itemSearch(const SearchSpecifier &namePattern,
                                     const SearchSpecifier &categoryPattern,
                                     std::vector<unsigned long long> *idsFound) const {
    if (catalog_)
      catalog_->search(namePattern, categoryPattern, idsFound);
    else {
      assert(idsFound);
      idsFound->clear();
    }
  }

  bool BinaryArchiveBase::parseArchiveOptions(std::ostringstream &err,
                                              const char *modeIn,
                                              CStringStream::CompressionMode *m,
                                              int *compressionLevel,
                                              unsigned *minSizeToCompress,
                                              unsigned *bufSize,
                                              bool *multiplexCatalog) {
    if (!modeIn)
      return true;
    std::string cmode(modeIn ? modeIn : "");
    if (cmode.empty())
      return true;
    char *mode = const_cast<char *>(cmode.c_str());

    unsigned cnt = 0;
    for (char *opt = strtok(mode, ":"); opt; opt = strtok(nullptr, ":"), ++cnt) {
      // Skip the first word -- this is the file opening mode
      if (!cnt)
        continue;
      char *eq = strchr(opt, '=');
      if (eq) {
        // Get rid of spaces around option name
        char *optname = opt;
        while (isspace(*optname) && optname < eq)
          ++optname;
        if (optname == eq) {
          err << "invalid binary archive option \"\"";
          return false;
        }
        char *optend = eq - 1;
        while (isspace(*optend))
          --optend;
        ++optend;
        *optend = '\0';

        // Get rid of spaces around option value
        char *optval = eq + 1;
        while (*optval && isspace(*optval))
          ++optval;
        if (!*optval) {
          err << "invalid binary archive option value \"\"";
          return false;
        }
        char *valend = opt + strlen(opt) - 1;
        while (isspace(*valend))
          --valend;
        ++valend;
        *valend = '\0';
        if (strlen(optval) == 0) {
          err << "invalid binary archive option value \"\"";
          return false;
        }

        // Go over possible options
        if (!strcasecmp(optname, "z")) {
          // Compression type
          if (!CStringStream::getCompressionModeByName(optval, m)) {
            err << "invalid compression type \"" << optval << '"';
            return false;
          }
        } else if (!strcasecmp(optname, "cl")) {
          // Compression level
          if (!parse_int(err, optval, compressionLevel))
            return false;
          if (*compressionLevel < -1 || *compressionLevel > 9) {
            err << "compression level is out of range";
            return false;
          }
        } else if (!strcasecmp(optname, "cb")) {
          // Compression buffer size
          if (!parse_unsigned(err, optval, bufSize))
            return false;
        } else if (!strcasecmp(optname, "cm")) {
          // Compression minimum size
          if (!parse_unsigned(err, optval, minSizeToCompress))
            return false;
        } else if (!strcasecmp(optname, "cat")) {
          // Internal or external catalog
          if (optval[0] == 'i' || optval[0] == 'I')
            *multiplexCatalog = true;
          else if (optval[0] == 's' || optval[0] == 'S')
            *multiplexCatalog = false;
          else {
            err << "invalid catalog mode \"" << optval << '"';
            return false;
          }
        } else {
          // Unknown option
          err << "unrecognized binary archive option \"" << optname << '"';
          return false;
        }
      } else {
        err << "invalid binary archive option \"" << opt << '"';
        return false;
      }
    }
    return true;
  }

  std::ios_base::openmode BinaryArchiveBase::parseMode(const char *mode) {
    std::ios_base::openmode m = std::ios_base::binary;
    if (mode) {
      const unsigned len = strlen(mode);
      for (unsigned i = 0; i < len; ++i) {
        // Note that all characters other than 'r', 'w',
        // 'a', and '+' are basically ignored inside this cycle
        if (mode[i] == 'r')
          m |= std::ios_base::in;
        else if (mode[i] == 'w')
          m |= (std::ios_base::out | std::ios_base::trunc);
        else if (mode[i] == 'a')
          m |= (std::ios_base::out | std::ios_base::app);
        else if (mode[i] == '+')
          m |= (std::ios_base::in | std::ios_base::out);
        else if (mode[i] == ':')
          break;
      }
    }

    // Make sure that we are at least reading
    if (!(m & (std::ios_base::in | std::ios_base::out)))
      m |= std::ios_base::in;
    return m;
  }

  void BinaryArchiveBase::search(AbsReference &reference) {
    if (catalog_) {
      std::vector<unsigned long long> idlist;
      catalog_->search(reference.namePattern(), reference.categoryPattern(), &idlist);
      const unsigned long nfound = idlist.size();
      for (unsigned long i = 0; i < nfound; ++i) {
        std::shared_ptr<const CatalogEntry> pentry = catalog_->retrieveEntry(idlist[i]);
        if (reference.isIOCompatible(*pentry))
          addItemToReference(reference, idlist[i]);
      }
    }
  }

  bool BinaryArchiveBase::isEmptyFile(std::fstream &s) {
    s.seekg(0, std::ios_base::end);
    return s.tellg() == std::streampos(0);
  }

  std::istream &BinaryArchiveBase::inputStream(const unsigned long long id, long long *sz) {
    unsigned long long length = 0;
    unsigned compressionCode = 0;
    std::istream &is = plainInputStream(id, &compressionCode, &length);
    if (cStream_->compressionMode() == CStringStream::NOT_COMPRESSED) {
      if (sz)
        *sz = -1LL;
      return is;
    } else {
      cStream_->readCompressed(is, compressionCode, length);
      if (sz) {
        std::streamoff off = cStream_->tellp();
        *sz = off;
      }
      return *cStream_;
    }
  }

  std::ostream &BinaryArchiveBase::outputStream() { return plainOutputStream(); }

  std::ostream &BinaryArchiveBase::compressedStream(std::ostream &os) {
    if (cStream_->compressionMode() == CStringStream::NOT_COMPRESSED)
      return os;
    else {
      cStream_->reset();
      cStream_->setSink(os);
      return *cStream_;
    }
  }

  unsigned BinaryArchiveBase::flushCompressedRecord(std::ostream &) {
    CStringStream::CompressionMode m = cStream_->compressionMode();
    if (m != CStringStream::NOT_COMPRESSED) {
      cStream_->flush();
      m = cStream_->writeCompressed();
    }
    return static_cast<unsigned>(m);
  }
}  // namespace gs