7 #include <packager/file.h>
14 #include <absl/flags/flag.h>
15 #include <absl/log/check.h>
16 #include <absl/log/log.h>
17 #include <absl/strings/numbers.h>
18 #include <absl/strings/str_format.h>
20 #include <packager/file/callback_file.h>
21 #include <packager/file/file_util.h>
22 #include <packager/file/http_file.h>
23 #include <packager/file/local_file.h>
24 #include <packager/file/memory_file.h>
25 #include <packager/file/threaded_io_file.h>
26 #include <packager/file/udp_file.h>
27 #include <packager/macros/compiler.h>
28 #include <packager/macros/logging.h>
33 "Size of the threaded I/O cache, in bytes. Specify 0 to disable "
38 "Size of the block size used for threaded I/O, in bytes.");
42 const char* kCallbackFilePrefix =
"callback://";
43 const char* kLocalFilePrefix =
"file://";
44 const char* kMemoryFilePrefix =
"memory://";
45 const char* kUdpFilePrefix =
"udp://";
46 const char* kHttpFilePrefix =
"http://";
47 const char* kHttpsFilePrefix =
"https://";
52 typedef File* (*FileFactoryFunction)(
const char* file_name,
const char* mode);
53 typedef bool (*FileDeleteFunction)(
const char* file_name);
54 typedef bool (*FileAtomicWriteFunction)(
const char* file_name,
55 const std::string& contents);
59 const FileFactoryFunction factory_function;
60 const FileDeleteFunction delete_function;
61 const FileAtomicWriteFunction atomic_write_function;
64 File* CreateCallbackFile(
const char* file_name,
const char* mode) {
65 return new CallbackFile(file_name, mode);
68 File* CreateLocalFile(
const char* file_name,
const char* mode) {
69 return new LocalFile(file_name, mode);
72 bool DeleteLocalFile(
const char* file_name) {
76 bool WriteLocalFileAtomically(
const char* file_name,
77 const std::string& contents) {
78 const auto file_path = std::filesystem::u8path(file_name);
79 const auto dir_path = file_path.parent_path();
81 std::string temp_file_name;
84 if (!File::WriteStringToFile(temp_file_name.c_str(), contents))
88 auto temp_file_path = std::filesystem::u8path(temp_file_name);
89 std::filesystem::rename(temp_file_path, file_name, ec);
91 LOG(ERROR) <<
"Failed to replace file '" << file_name <<
"' with '"
92 << temp_file_name <<
"', error: " << ec;
98 File* CreateUdpFile(
const char* file_name,
const char* mode) {
99 if (strcmp(mode,
"r")) {
100 NOTIMPLEMENTED() <<
"UdpFile only supports read (receive) mode.";
103 return new UdpFile(file_name);
106 File* CreateHttpsFile(
const char* file_name,
const char* mode) {
107 HttpMethod method = HttpMethod::kGet;
108 if (strcmp(mode,
"r") != 0) {
109 method = HttpMethod::kPut;
111 return new HttpFile(method, std::string(
"https://") + file_name);
114 bool DeleteHttpsFile(
const char* file_name) {
115 return HttpFile::Delete(std::string(
"https://") + file_name);
118 File* CreateHttpFile(
const char* file_name,
const char* mode) {
119 HttpMethod method = HttpMethod::kGet;
120 if (strcmp(mode,
"r") != 0) {
121 method = HttpMethod::kPut;
123 return new HttpFile(method, std::string(
"http://") + file_name);
126 bool DeleteHttpFile(
const char* file_name) {
127 return HttpFile::Delete(std::string(
"http://") + file_name);
130 File* CreateMemoryFile(
const char* file_name,
const char* mode) {
131 return new MemoryFile(file_name, mode);
134 bool DeleteMemoryFile(
const char* file_name) {
139 static const FileTypeInfo kFileTypeInfo[] = {
144 &WriteLocalFileAtomically,
146 {kUdpFilePrefix, &CreateUdpFile,
nullptr,
nullptr},
147 {kMemoryFilePrefix, &CreateMemoryFile, &DeleteMemoryFile,
nullptr},
148 {kCallbackFilePrefix, &CreateCallbackFile,
nullptr,
nullptr},
149 {kHttpFilePrefix, &CreateHttpFile, &DeleteHttpFile,
nullptr},
150 {kHttpsFilePrefix, &CreateHttpsFile, &DeleteHttpsFile,
nullptr},
153 std::string_view GetFileTypePrefix(std::string_view file_name) {
154 size_t pos = file_name.find(
"://");
155 return (pos == std::string::npos) ?
"" : file_name.substr(0, pos + 3);
158 const FileTypeInfo* GetFileTypeInfo(std::string_view file_name,
159 std::string_view* real_file_name) {
160 std::string_view file_type_prefix = GetFileTypePrefix(file_name);
161 for (
const FileTypeInfo& file_type : kFileTypeInfo) {
162 if (file_type_prefix == file_type.type) {
163 *real_file_name = file_name.substr(file_type_prefix.size());
168 *real_file_name = file_name;
169 return &kFileTypeInfo[0];
174 File* File::Create(
const char* file_name,
const char* mode) {
175 std::unique_ptr<File, FileCloser> internal_file(
176 CreateInternalFile(file_name, mode));
178 std::string_view file_type_prefix = GetFileTypePrefix(file_name);
179 if (file_type_prefix == kMemoryFilePrefix ||
180 file_type_prefix == kCallbackFilePrefix) {
182 return internal_file.release();
185 if (absl::GetFlag(FLAGS_io_cache_size)) {
187 if (!strcmp(mode,
"r")) {
188 return new ThreadedIoFile(std::move(internal_file),
189 ThreadedIoFile::kInputMode,
190 absl::GetFlag(FLAGS_io_cache_size),
191 absl::GetFlag(FLAGS_io_block_size));
192 }
else if (!strcmp(mode,
"w") || !strcmp(mode,
"a")) {
193 return new ThreadedIoFile(std::move(internal_file),
194 ThreadedIoFile::kOutputMode,
195 absl::GetFlag(FLAGS_io_cache_size),
196 absl::GetFlag(FLAGS_io_block_size));
201 DLOG(WARNING) <<
"Threaded I/O is disabled. Performance may be decreased.";
202 return internal_file.release();
205 File* File::CreateInternalFile(
const char* file_name,
const char* mode) {
206 std::string_view real_file_name;
207 const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
210 return file_type->factory_function(real_file_name.data(), mode);
213 File* File::Open(
const char* file_name,
const char* mode) {
214 File* file = File::Create(file_name, mode);
224 File* File::OpenWithNoBuffering(
const char* file_name,
const char* mode) {
225 File* file = File::CreateInternalFile(file_name, mode);
235 bool File::Delete(
const char* file_name) {
236 static bool logged =
false;
237 std::string_view real_file_name;
238 const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
240 if (file_type->delete_function) {
241 return file_type->delete_function(real_file_name.data());
245 LOG(WARNING) <<
"File::Delete: file type for "
247 <<
" ('" << file_type->type <<
"') "
248 <<
"has no 'delete' function.";
254 int64_t File::GetFileSize(
const char* file_name) {
255 File* file = File::Open(file_name,
"r");
258 int64_t res = file->Size();
263 bool File::ReadFileToString(
const char* file_name, std::string* contents) {
266 File* file = File::Open(file_name,
"r");
270 const size_t kBufferSize = 0x40000;
271 std::unique_ptr<char[]> buf(
new char[kBufferSize]);
274 while ((len = file->Read(buf.get(), kBufferSize)) > 0)
275 contents->append(buf.get(), len);
281 bool File::WriteStringToFile(
const char* file_name,
282 const std::string& contents) {
283 VLOG(2) <<
"File::WriteStringToFile: " << file_name;
284 std::unique_ptr<File, FileCloser> file(File::Open(file_name,
"w"));
286 LOG(ERROR) <<
"Failed to open file " << file_name;
289 int64_t bytes_written = file->Write(contents.data(), contents.size());
290 if (bytes_written < 0) {
291 LOG(ERROR) <<
"Failed to write to file '" << file_name <<
"' ("
292 << bytes_written <<
").";
295 if (
static_cast<size_t>(bytes_written) != contents.size()) {
296 LOG(ERROR) <<
"Failed to write the whole file to " << file_name
297 <<
". Wrote " << bytes_written <<
" but expecting "
298 << contents.size() <<
" bytes.";
301 if (!file.release()->Close()) {
303 <<
"Failed to close file '" << file_name
304 <<
"', possibly file permission issue or running out of disk space.";
310 bool File::WriteFileAtomically(
const char* file_name,
311 const std::string& contents) {
312 VLOG(2) <<
"File::WriteFileAtomically: " << file_name;
313 std::string_view real_file_name;
314 const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
316 if (file_type->atomic_write_function)
317 return file_type->atomic_write_function(real_file_name.data(), contents);
324 if (strncmp(file_name, kMemoryFilePrefix, strlen(kMemoryFilePrefix)) != 0
325 && strncmp(file_name, kHttpFilePrefix, strlen(kHttpFilePrefix)) != 0
326 && strncmp(file_name, kHttpsFilePrefix, strlen(kHttpsFilePrefix)) != 0) {
327 LOG(WARNING) <<
"Writing to " << file_name
328 <<
" is not guaranteed to be atomic.";
330 return WriteStringToFile(file_name, contents);
333 bool File::Copy(
const char* from_file_name,
const char* to_file_name) {
335 VLOG(2) <<
"File::Copy from " << from_file_name <<
" to " << to_file_name;
336 if (!ReadFileToString(from_file_name, &content)) {
337 LOG(ERROR) <<
"Failed to open file " << from_file_name;
341 std::unique_ptr<File, FileCloser> output_file(File::Open(to_file_name,
"w"));
343 LOG(ERROR) <<
"Failed to write to " << to_file_name;
347 uint64_t bytes_left = content.size();
348 uint64_t total_bytes_written = 0;
349 const char* content_cstr = content.c_str();
350 while (bytes_left > total_bytes_written) {
351 const int64_t bytes_written =
352 output_file->Write(content_cstr + total_bytes_written, bytes_left);
353 if (bytes_written < 0) {
354 LOG(ERROR) <<
"Failure while writing to " << to_file_name;
358 total_bytes_written += bytes_written;
360 if (!output_file.release()->Close()) {
362 <<
"Failed to close file '" << to_file_name
363 <<
"', possibly file permission issue or running out of disk space.";
369 int64_t File::Copy(File* source, File* destination) {
370 return Copy(source, destination, kWholeFile);
373 int64_t File::Copy(File* source, File* destination, int64_t max_copy) {
377 max_copy = std::numeric_limits<int64_t>::max();
379 VLOG(2) <<
"File::Copy from " << source->file_name() <<
" to "
380 << destination->file_name();
382 const int64_t kBufferSize = 0x40000;
383 std::unique_ptr<uint8_t[]> buffer(
new uint8_t[kBufferSize]);
384 int64_t bytes_copied = 0;
385 while (bytes_copied < max_copy) {
386 const int64_t size = std::min(kBufferSize, max_copy - bytes_copied);
387 const int64_t bytes_read = source->Read(buffer.get(), size);
393 int64_t total_bytes_written = 0;
394 while (total_bytes_written < bytes_read) {
395 const int64_t bytes_written = destination->Write(
396 buffer.get() + total_bytes_written, bytes_read - total_bytes_written);
397 if (bytes_written < 0)
398 return bytes_written;
400 total_bytes_written += bytes_written;
403 DCHECK_EQ(total_bytes_written, bytes_read);
404 bytes_copied += bytes_read;
410 bool File::IsLocalRegularFile(
const char* file_name) {
411 std::string_view real_file_name;
412 const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
415 if (file_type->type != kLocalFilePrefix)
419 auto real_file_path = std::filesystem::u8path(real_file_name);
420 return std::filesystem::is_regular_file(real_file_path, ec);
423 std::string File::MakeCallbackFileName(
424 const BufferCallbackParams& callback_params,
425 const std::string& name) {
428 return absl::StrFormat(
"%s%" PRIdPTR
"/%s", kCallbackFilePrefix,
429 reinterpret_cast<intptr_t
>(&callback_params),
433 bool File::ParseCallbackFileName(
const std::string& callback_file_name,
434 const BufferCallbackParams** callback_params,
436 size_t pos = callback_file_name.find(
"/");
437 int64_t callback_address = 0;
438 if (pos == std::string::npos ||
439 !absl::SimpleAtoi(callback_file_name.substr(0, pos), &callback_address)) {
440 LOG(ERROR) <<
"Expecting CallbackFile with name like "
441 "'<callback address>/<entity name>', but seeing "
442 << callback_file_name;
445 *callback_params =
reinterpret_cast<BufferCallbackParams*
>(callback_address);
446 *name = callback_file_name.substr(pos + 1);
static bool Delete(const char *file_name)
static void Delete(const std::string &file_name)
All the methods that are virtual are virtual for mocking.
bool TempFilePath(const std::string &temp_dir, std::string *temp_file_path)