Beispiel #1
0
TEST(TestMemFile, EmptyFileInCache) {
  StaticContentCache::TheFileCache = FileCachePtr(new FileCache());
  StaticContentCache::TheFileCache->write("/no/content/entry", true);

  MemFile mf;

  // The file itself...
  ASSERT_FALSE(mf.open("/no/content/entry", "r"));

  // ... and one of the automatically-created "directory" entries.
  ASSERT_FALSE(mf.open("/no/content", "r"));
}
Beispiel #2
0
TEST(TestMemFile, RealFileInCache) {
  string temp_fn;
  ASSERT_TRUE(makeTempFile("123abc", &temp_fn));

  StaticContentCache::TheFileCache = FileCachePtr(new FileCache());
  StaticContentCache::TheFileCache->write("/content", temp_fn.c_str());

  MemFile mf;
  ASSERT_TRUE(mf.open("/content", "r"));

  ASSERT_EQ(mf.getc(), '1');
  ASSERT_EQ(mf.getc(), '2');
  ASSERT_EQ(mf.getc(), '3');
  ASSERT_EQ(mf.getc(), 'a');
  ASSERT_EQ(mf.getc(), 'b');
  ASSERT_EQ(mf.getc(), 'c');

  ASSERT_EQ(mf.getc(), EOF);

  ASSERT_TRUE(mf.close());

  ASSERT_EQ(unlink(temp_fn.c_str()), 0);
}
void StaticContentCache::load() {
  Timer timer(Timer::WallTime, "loading static content");

  if (!RuntimeOption::FileCache.empty()) {
    TheFileCache = FileCachePtr(new FileCache());
    TheFileCache->load(RuntimeOption::FileCache.c_str());
    Logger::Info("loaded file cache from %s",
                 RuntimeOption::FileCache.c_str());
    return;
  }

  int rootSize = RuntimeOption::SourceRoot.size();
  if (rootSize == 0) return;

  // get a list of all files, one for each extension
  Logger::Info("searching all files under source root...");
  int count = 0;
  map<string, vector<string> > ext2files;
  {
    const char *argv[] = {"", (char*)RuntimeOption::SourceRoot.c_str(),
                          "-type", "f", NULL};
    string files;
    vector<string> out;
    Process::Exec("find", argv, NULL, files);
    Util::split('\n', files.c_str(), out, true);
    for (unsigned int i = 0; i < out.size(); i++) {
      const string &name = out[i];
      size_t pos = name.rfind('.');
      if (pos != string::npos) {
        ext2files[name.substr(pos+1)].push_back(name);
        ++count;
      }
    }
  }

  Logger::Info("analyzing %d files under source root...", count);
  for (map<string, string>::const_iterator iter =
         RuntimeOption::StaticFileExtensions.begin();
       iter != RuntimeOption::StaticFileExtensions.end(); ++iter) {
    if (ext2files.find(iter->first) == ext2files.end()) {
      continue;
    }
    const vector<string> &out = ext2files[iter->first];

    int total = 0;
    for (unsigned int i = 0; i < out.size(); i++) {
      ResourceFilePtr f(new ResourceFile());

      StringBufferPtr sb(new StringBuffer(out[i].c_str()));
      if (sb->valid() && sb->size() > 0) {
        string url = out[i].substr(rootSize + 1);
        f->file = sb;
        m_files[url] = f;

        // prepare gzipped content, skipping image and swf files
        if (iter->second.find("image/") != 0 && iter->first != "swf") {
          int len = sb->size();
          char *data = gzencode(sb->data(), len, 9, CODING_GZIP);
          if (data) {
            if (len < sb->size()) {
              f->compressed = StringBufferPtr(new StringBuffer(data, len));
            } else {
              free(data);
            }
          }
        }

        total += sb->size();
      }
    }
    Logger::Info("..loaded %d bytes of %s files", total, iter->first.c_str());
    m_totalSize += total;
  }
  Logger::Info("loaded %d bytes of static content in total", m_totalSize);
}
Beispiel #4
0
TEST(TestMemFile, NotInCache) {
  StaticContentCache::TheFileCache = FileCachePtr(new FileCache());

  MemFile mf;
  ASSERT_FALSE(mf.open("/not/even/there", "r"));
}
Beispiel #5
0
TEST(TestMemFile, BadReadFromCache) {
  StaticContentCache::TheFileCache = FileCachePtr(new FileCache());

  MemFile mf;
  ASSERT_FALSE(mf.open("/some/random/file", "r"));
}