File: tools.cpp

package info (click to toggle)
zimlib 8.1.1-0.2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,592 kB
  • sloc: cpp: 14,011; ansic: 548; python: 241; makefile: 8; sh: 5
file content (201 lines) | stat: -rw-r--r-- 5,290 bytes parent folder | download
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
/*
 * Copyright (C) 2020 Veloman Yunkan
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * is provided AS IS, WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, and
 * NON-INFRINGEMENT.  See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
 *
 */

#include "tools.h"

#ifdef _WIN32
#include <locale>
#include <codecvt>
#include <windows.h>
#include <fileapi.h>
#include <io.h>
#else
#include <dirent.h>
#endif

#include "../src/fs.h"

#include <fcntl.h>
#include <sys/stat.h>
#include "gtest/gtest.h"

namespace zim
{

namespace unittests
{

TempFile::TempFile(const char* name)
 : fd_(-1)
{
#ifdef _WIN32
  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> utfConv;
  wchar_t cbase[MAX_PATH];
  const std::wstring wname = utfConv.from_bytes(name);
  GetTempPathW(MAX_PATH-(wname.size()+2), cbase);
  //This create a empty file, we just have to open it later
  GetTempFileNameW(cbase, wname.c_str(), 0, wpath_);
  path_ = utfConv.to_bytes(wpath_);
#else
  const char* const TMPDIR = std::getenv("TMPDIR");
  const std::string tmpdir(TMPDIR ? TMPDIR : "/tmp");
  path_ = tmpdir + "/" + name + "_XXXXXX";
  auto tmp_fd = mkstemp(&path_[0]);
  ::close(tmp_fd);
#endif
}

TempFile::~TempFile()
{
  close();
#ifdef _WIN32
  DeleteFileW(wpath_);
#else
  unlink(path_.c_str());
#endif
}

int TempFile::fd()
{
  if (fd_ == -1) {
#ifdef _WIN32
    fd_ = _wopen(wpath_, _O_RDWR | _O_BINARY);
#else
    fd_ = open(path_.c_str(), O_RDWR);
#endif
  }
  return fd_;
}

void TempFile::close()
{
  if (fd_ != -1) {
	::close(fd_);
	fd_ = -1;
  }
}

std::unique_ptr<TempFile>
makeTempFile(const char* name, const std::string& content)
{
  std::unique_ptr<TempFile> p(new TempFile(name));
  write(p->fd(), &content[0], content.size());
  p->close();
  return p;
}

void setDataDir(std::string& dataDir)
{
  // FAIL must be used in a void function. So we need to use a out parameter.
  const char* cDataDir = std::getenv("ZIM_TEST_DATA_DIR");
  if (cDataDir == NULL) {
    dataDir = "INVALID_DATA_DIR";
    FAIL() << "ZIM_TEST_DATA_DIR is not defined. You must define it to the directory containing test zim files.";
  }
  dataDir = cDataDir;
}

TestFile::TestFile(const std::string& dataDir, const std::string& category, const std::string& filename) :
  filename(filename),
  category(category),
  path(zim::DEFAULTFS::join(zim::DEFAULTFS::join(dataDir, category), filename))
{
}

const std::vector<TestFile> getDataFilePath(const std::string& filename, const std::string& category)
{
  std::vector<TestFile> filePaths;
  std::string dataDirPath;
  setDataDir(dataDirPath);

  if (!category.empty()) {
      // We have asked for a particular category.
      filePaths.emplace_back(dataDirPath, category, filename);
  } else {
#ifdef _WIN32
    // We don't have dirent.h in windows.
    // If we move to test data out of the repository, we will need a way to discover the data.
    // Use a static list of categories for now.
    for (auto& category: {"withns", "nons"}) {
      filePaths.emplace_back(dataDirPath, category, filename);
    }
#else
    auto dataDir = opendir(dataDirPath.c_str());

    if (!dataDir) {
      filePaths.emplace_back(dataDirPath, "NO_DATA_DIR", filename);
      return filePaths;
    }
    struct dirent* current = NULL;
    while((current = readdir(dataDir))) {
      if (current->d_name[0] == '.' || current->d_name[0] == '_') {
        continue;
      }
      filePaths.emplace_back(dataDirPath, current->d_name, filename);
    }
    closedir(dataDir);
#endif
  }

  return filePaths;
}

zim::Archive TempZimArchive::createZimFromTitles(std::vector<std::string> titles) {
  zim::writer::Creator creator;
  creator.configIndexing(true, "en");
  creator.startZimCreation(this->path());

  // add dummy items with given titles
  for (auto title : titles) {
    std::string path = "dummyPath" + title;
    auto item = std::make_shared<TestItem>(path, "text/html", title);
    creator.addItem(item);
  }

  creator.addMetadata("Title", "This is a title");

  creator.finishZimCreation();
  return zim::Archive(this->path());
}

zim::Archive TempZimArchive::createZimFromContent(std::vector<std::vector<std::string>> contents) {
  zim::writer::Creator creator;
  creator.configIndexing(true, "en");
  creator.startZimCreation(this->path());

  // add dummy items with given titles
  for (auto content : contents) {
    std::string path = "dummyPath" + content[0];
    auto item = std::make_shared<TestItem>(path, "text/html", content[0], content[1]);
    creator.addItem(item);
  }

  creator.addMetadata("Title", "This is a title");

  creator.finishZimCreation();
  return zim::Archive(this->path());
}

const std::string TempZimArchive::getPath() {
  return this->path();
}

} // namespace unittests

} // namespace zim