Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarFile.cpp
blob: 75813b4c5dde90bebe89036cfeca59ece3e3f928 (plain)
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
#include "StarFile.hpp"
#include "StarFormat.hpp"

#include <fstream>

namespace Star {

void File::makeDirectoryRecursive(String const& fileName) {
  auto parent = dirName(fileName);
  if (!isDirectory(parent))
    makeDirectoryRecursive(parent);
  if (!isDirectory(fileName))
    makeDirectory(fileName);
}

void File::removeDirectoryRecursive(String const& fileName) {
  {
    String fileInDir;
    bool isDir;

    for (auto const& p : dirList(fileName)) {
      std::tie(fileInDir, isDir) = p;

      fileInDir = relativeTo(fileName, fileInDir);

      if (isDir)
        removeDirectoryRecursive(fileInDir);
      else
        remove(fileInDir);
    }
  }

  remove(fileName);
}

void File::copy(String const& source, String const& target) {
  auto sourceFile = File::open(source, IOMode::Read);
  auto targetFile = File::open(target, IOMode::ReadWrite);

  targetFile->resize(0);

  char buf[1024];
  while (!sourceFile->atEnd()) {
    size_t r = sourceFile->read(buf, 1024);
    targetFile->writeFull(buf, r);
  }
}

FilePtr File::open(const String& filename, IOMode mode) {
  auto file = make_shared<File>(filename);
  file->open(mode);
  return file;
}

ByteArray File::readFile(String const& filename) {
  FilePtr file = File::open(filename, IOMode::Read);
  ByteArray bytes;
  while (!file->atEnd()) {
    char buffer[1024];
    size_t r = file->read(buffer, 1024);
    bytes.append(buffer, r);
  }

  return bytes;
}

String File::readFileString(String const& filename) {
  FilePtr file = File::open(filename, IOMode::Read);
  std::string str;
  while (!file->atEnd()) {
    char buffer[1024];
    size_t r = file->read(buffer, 1024);
    for (size_t i = 0; i < r; ++i)
      str.push_back(buffer[i]);
  }

  return str;
}

StreamOffset File::fileSize(String const& filename) {
  return File::open(filename, IOMode::Read)->size();
}

void File::writeFile(char const* data, size_t len, String const& filename) {
  FilePtr file = File::open(filename, IOMode::Write | IOMode::Truncate);
  file->writeFull(data, len);
}

void File::writeFile(ByteArray const& data, String const& filename) {
  writeFile(data.ptr(), data.size(), filename);
}

void File::writeFile(String const& data, String const& filename) {
  writeFile(data.utf8Ptr(), data.utf8Size(), filename);
}

void File::overwriteFileWithRename(ByteArray const& data, String const& filename, String const& newSuffix) {
  overwriteFileWithRename(data.ptr(), data.size(), filename, newSuffix);
}

void File::overwriteFileWithRename(String const& data, String const& filename, String const& newSuffix) {
  overwriteFileWithRename(data.utf8Ptr(), data.utf8Size(), filename, newSuffix);
}

void File::backupFileInSequence(String const& initialFile, String const& targetFile, unsigned maximumBackups, String const& backupExtensionPrefix) {
  for (unsigned i = maximumBackups; i > 0; --i) {
    bool initial = i == 1;
    String const& sourceFile = initial ? initialFile : targetFile;
    String curExtension = initial ? "" : strf("{}{}", backupExtensionPrefix, i - 1);
    String nextExtension = strf("{}{}", backupExtensionPrefix, i);

    if (File::isFile(sourceFile + curExtension))
      File::copy(sourceFile + curExtension, targetFile + nextExtension);
  }
}

void File::backupFileInSequence(String const& targetFile, unsigned maximumBackups, String const& backupExtensionPrefix) {
  backupFileInSequence(targetFile, targetFile, maximumBackups, backupExtensionPrefix);
}

File::File()
  : IODevice(IOMode::Closed) {
  m_file = 0;
}

File::File(String filename)
  : IODevice(IOMode::Closed), m_filename(std::move(filename)), m_file(0) {}

File::~File() {
  close();
}

StreamOffset File::pos() {
  if (!m_file)
    throw IOException("pos called on closed File");

  return ftell(m_file);
}

void File::seek(StreamOffset offset, IOSeek seekMode) {
  if (!m_file)
    throw IOException("seek called on closed File");

  fseek(m_file, offset, seekMode);
}

StreamOffset File::size() {
  return fsize(m_file);
}

bool File::atEnd() {
  if (!m_file)
    throw IOException("eof called on closed File");

  return ftell(m_file) >= fsize(m_file);
}

size_t File::read(char* data, size_t len) {
  if (!m_file)
    throw IOException("read called on closed File");

  if (!isReadable())
    throw IOException("read called on non-readable File");

  return fread(m_file, data, len);
}

size_t File::write(const char* data, size_t len) {
  if (!m_file)
    throw IOException("write called on closed File");

  if (!isWritable())
    throw IOException("write called on non-writable File");

  return fwrite(m_file, data, len);
}

size_t File::readAbsolute(StreamOffset readPosition, char* data, size_t len) {
  return pread(m_file, data, len, readPosition);
}

size_t File::writeAbsolute(StreamOffset writePosition, char const* data, size_t len) {
  return pwrite(m_file, data, len, writePosition);
}

String File::fileName() const {
  return m_filename;
}

void File::setFilename(String filename) {
  if (isOpen())
    throw IOException("Cannot call setFilename while File is open");
  m_filename = std::move(filename);
}

void File::remove() {
  close();
  if (m_filename.empty())
    throw IOException("Cannot remove file, no filename set");
  remove(m_filename);
}

void File::resize(StreamOffset s) {
  bool tempOpen = false;
  if (!isOpen()) {
    tempOpen = true;
    open(mode());
  }

  File::resize(m_file, s);

  if (tempOpen)
    close();
}

void File::sync() {
  if (!m_file)
    throw IOException("sync called on closed File");

  fsync(m_file);
}

void File::open(IOMode m) {
  close();
  if (m_filename.empty())
    throw IOException("Cannot open file, no filename set");

  m_file = fopen(m_filename.utf8Ptr(), m);
  setMode(m);
}

void File::close() {
  if (m_file)
    fclose(m_file);
  m_file = 0;
  setMode(IOMode::Closed);
}

String File::deviceName() const {
  if (m_filename.empty())
    return "<unnamed temp file>";
  else
    return m_filename;
}

IODevicePtr File::clone() {
  auto cloned = make_shared<File>(m_filename);
  if (isOpen()) {
    // Open with same mode
    cloned->open(mode());
    // Seek to same position
    cloned->seek(pos());
  }
  return cloned;
}

}