2023-12-01 17:32:19 +00:00
|
|
|
// Copyright 2014 Google LLC. All rights reserved.
|
2014-02-14 23:21:05 +00:00
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <packager/file.h>
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2015-10-28 17:23:08 +00:00
|
|
|
#include <algorithm>
|
2023-12-01 17:32:19 +00:00
|
|
|
#include <cinttypes>
|
|
|
|
#include <filesystem>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
#include <absl/flags/flag.h>
|
|
|
|
#include <absl/log/check.h>
|
|
|
|
#include <absl/log/log.h>
|
|
|
|
#include <absl/strings/numbers.h>
|
|
|
|
#include <absl/strings/str_format.h>
|
|
|
|
|
|
|
|
#include <packager/file/callback_file.h>
|
|
|
|
#include <packager/file/file_util.h>
|
|
|
|
#include <packager/file/http_file.h>
|
|
|
|
#include <packager/file/local_file.h>
|
|
|
|
#include <packager/file/memory_file.h>
|
|
|
|
#include <packager/file/threaded_io_file.h>
|
|
|
|
#include <packager/file/udp_file.h>
|
|
|
|
#include <packager/macros/compiler.h>
|
|
|
|
#include <packager/macros/logging.h>
|
|
|
|
|
|
|
|
ABSL_FLAG(uint64_t,
|
|
|
|
io_cache_size,
|
|
|
|
32ULL << 20,
|
|
|
|
"Size of the threaded I/O cache, in bytes. Specify 0 to disable "
|
|
|
|
"threaded I/O.");
|
|
|
|
ABSL_FLAG(uint64_t,
|
|
|
|
io_block_size,
|
|
|
|
1ULL << 16,
|
|
|
|
"Size of the block size used for threaded I/O, in bytes.");
|
2016-08-14 22:28:21 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2017-07-17 18:15:07 +00:00
|
|
|
const char* kCallbackFilePrefix = "callback://";
|
2013-10-11 21:44:55 +00:00
|
|
|
const char* kLocalFilePrefix = "file://";
|
2015-12-22 00:20:34 +00:00
|
|
|
const char* kMemoryFilePrefix = "memory://";
|
2017-07-17 18:15:07 +00:00
|
|
|
const char* kUdpFilePrefix = "udp://";
|
2021-02-02 18:51:50 +00:00
|
|
|
const char* kHttpFilePrefix = "http://";
|
|
|
|
const char* kHttpsFilePrefix = "https://";
|
|
|
|
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
typedef File* (*FileFactoryFunction)(const char* file_name, const char* mode);
|
2015-03-11 19:18:17 +00:00
|
|
|
typedef bool (*FileDeleteFunction)(const char* file_name);
|
2017-06-15 20:00:28 +00:00
|
|
|
typedef bool (*FileAtomicWriteFunction)(const char* file_name,
|
|
|
|
const std::string& contents);
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2017-06-15 20:00:28 +00:00
|
|
|
struct FileTypeInfo {
|
2013-10-11 21:44:55 +00:00
|
|
|
const char* type;
|
|
|
|
const FileFactoryFunction factory_function;
|
2015-03-11 19:18:17 +00:00
|
|
|
const FileDeleteFunction delete_function;
|
2017-06-15 20:00:28 +00:00
|
|
|
const FileAtomicWriteFunction atomic_write_function;
|
2013-10-11 21:44:55 +00:00
|
|
|
};
|
|
|
|
|
2017-07-17 18:15:07 +00:00
|
|
|
File* CreateCallbackFile(const char* file_name, const char* mode) {
|
|
|
|
return new CallbackFile(file_name, mode);
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
File* CreateLocalFile(const char* file_name, const char* mode) {
|
2014-01-15 22:29:56 +00:00
|
|
|
return new LocalFile(file_name, mode);
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
bool DeleteLocalFile(const char* file_name) {
|
|
|
|
return LocalFile::Delete(file_name);
|
|
|
|
}
|
|
|
|
|
2017-06-15 20:00:28 +00:00
|
|
|
bool WriteLocalFileAtomically(const char* file_name,
|
|
|
|
const std::string& contents) {
|
2023-12-01 17:32:19 +00:00
|
|
|
const auto file_path = std::filesystem::u8path(file_name);
|
|
|
|
const auto dir_path = file_path.parent_path();
|
|
|
|
|
2017-08-01 22:34:20 +00:00
|
|
|
std::string temp_file_name;
|
2023-12-01 17:32:19 +00:00
|
|
|
if (!TempFilePath(dir_path.string(), &temp_file_name))
|
2017-08-01 22:34:20 +00:00
|
|
|
return false;
|
|
|
|
if (!File::WriteStringToFile(temp_file_name.c_str(), contents))
|
|
|
|
return false;
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
auto temp_file_path = std::filesystem::u8path(temp_file_name);
|
|
|
|
std::filesystem::rename(temp_file_path, file_name, ec);
|
|
|
|
if (ec) {
|
2017-08-01 22:34:20 +00:00
|
|
|
LOG(ERROR) << "Failed to replace file '" << file_name << "' with '"
|
2023-12-01 17:32:19 +00:00
|
|
|
<< temp_file_name << "', error: " << ec;
|
2017-08-01 22:34:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
2017-06-15 20:00:28 +00:00
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
File* CreateUdpFile(const char* file_name, const char* mode) {
|
2016-08-17 21:42:23 +00:00
|
|
|
if (strcmp(mode, "r")) {
|
2014-04-30 23:22:15 +00:00
|
|
|
NOTIMPLEMENTED() << "UdpFile only supports read (receive) mode.";
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return new UdpFile(file_name);
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:51:50 +00:00
|
|
|
File* CreateHttpsFile(const char* file_name, const char* mode) {
|
2023-12-01 17:32:19 +00:00
|
|
|
HttpMethod method = HttpMethod::kGet;
|
|
|
|
if (strcmp(mode, "r") != 0) {
|
|
|
|
method = HttpMethod::kPut;
|
|
|
|
}
|
|
|
|
return new HttpFile(method, std::string("https://") + file_name);
|
2021-02-02 18:51:50 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 03:13:04 +00:00
|
|
|
bool DeleteHttpsFile(const char* file_name) {
|
|
|
|
return HttpFile::Delete(std::string("https://") + file_name);
|
|
|
|
}
|
|
|
|
|
2021-02-02 18:51:50 +00:00
|
|
|
File* CreateHttpFile(const char* file_name, const char* mode) {
|
2023-12-01 17:32:19 +00:00
|
|
|
HttpMethod method = HttpMethod::kGet;
|
|
|
|
if (strcmp(mode, "r") != 0) {
|
|
|
|
method = HttpMethod::kPut;
|
|
|
|
}
|
|
|
|
return new HttpFile(method, std::string("http://") + file_name);
|
2021-02-02 18:51:50 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 03:13:04 +00:00
|
|
|
bool DeleteHttpFile(const char* file_name) {
|
|
|
|
return HttpFile::Delete(std::string("http://") + file_name);
|
|
|
|
}
|
|
|
|
|
2015-12-22 00:20:34 +00:00
|
|
|
File* CreateMemoryFile(const char* file_name, const char* mode) {
|
2015-12-22 00:33:55 +00:00
|
|
|
return new MemoryFile(file_name, mode);
|
2015-12-22 00:20:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool DeleteMemoryFile(const char* file_name) {
|
|
|
|
MemoryFile::Delete(file_name);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-06-15 20:00:28 +00:00
|
|
|
static const FileTypeInfo kFileTypeInfo[] = {
|
2017-07-10 18:26:22 +00:00
|
|
|
{
|
|
|
|
kLocalFilePrefix,
|
|
|
|
&CreateLocalFile,
|
|
|
|
&DeleteLocalFile,
|
|
|
|
&WriteLocalFileAtomically,
|
|
|
|
},
|
2017-07-17 18:15:07 +00:00
|
|
|
{kUdpFilePrefix, &CreateUdpFile, nullptr, nullptr},
|
|
|
|
{kMemoryFilePrefix, &CreateMemoryFile, &DeleteMemoryFile, nullptr},
|
|
|
|
{kCallbackFilePrefix, &CreateCallbackFile, nullptr, nullptr},
|
2024-10-25 03:13:04 +00:00
|
|
|
{kHttpFilePrefix, &CreateHttpFile, &DeleteHttpFile, nullptr},
|
|
|
|
{kHttpsFilePrefix, &CreateHttpsFile, &DeleteHttpsFile, nullptr},
|
2013-10-11 21:44:55 +00:00
|
|
|
};
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view GetFileTypePrefix(std::string_view file_name) {
|
2017-07-17 18:15:07 +00:00
|
|
|
size_t pos = file_name.find("://");
|
|
|
|
return (pos == std::string::npos) ? "" : file_name.substr(0, pos + 3);
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
const FileTypeInfo* GetFileTypeInfo(std::string_view file_name,
|
|
|
|
std::string_view* real_file_name) {
|
|
|
|
std::string_view file_type_prefix = GetFileTypePrefix(file_name);
|
2017-06-15 20:00:28 +00:00
|
|
|
for (const FileTypeInfo& file_type : kFileTypeInfo) {
|
2017-07-17 18:15:07 +00:00
|
|
|
if (file_type_prefix == file_type.type) {
|
|
|
|
*real_file_name = file_name.substr(file_type_prefix.size());
|
2017-06-15 20:00:28 +00:00
|
|
|
return &file_type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Otherwise we default to the first file type, which is LocalFile.
|
|
|
|
*real_file_name = file_name;
|
|
|
|
return &kFileTypeInfo[0];
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
} // namespace
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
File* File::Create(const char* file_name, const char* mode) {
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> internal_file(
|
2015-05-20 17:14:36 +00:00
|
|
|
CreateInternalFile(file_name, mode));
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view file_type_prefix = GetFileTypePrefix(file_name);
|
2017-07-17 18:15:07 +00:00
|
|
|
if (file_type_prefix == kMemoryFilePrefix ||
|
|
|
|
file_type_prefix == kCallbackFilePrefix) {
|
|
|
|
// Disable caching for memory and callback files.
|
2015-12-22 00:20:34 +00:00
|
|
|
return internal_file.release();
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
if (absl::GetFlag(FLAGS_io_cache_size)) {
|
2015-03-19 18:28:04 +00:00
|
|
|
// Enable threaded I/O for "r", "w", and "a" modes only.
|
|
|
|
if (!strcmp(mode, "r")) {
|
2016-08-17 17:41:40 +00:00
|
|
|
return new ThreadedIoFile(std::move(internal_file),
|
2023-12-01 17:32:19 +00:00
|
|
|
ThreadedIoFile::kInputMode,
|
|
|
|
absl::GetFlag(FLAGS_io_cache_size),
|
|
|
|
absl::GetFlag(FLAGS_io_block_size));
|
2015-03-19 18:28:04 +00:00
|
|
|
} else if (!strcmp(mode, "w") || !strcmp(mode, "a")) {
|
2016-08-17 17:41:40 +00:00
|
|
|
return new ThreadedIoFile(std::move(internal_file),
|
2015-03-19 18:28:04 +00:00
|
|
|
ThreadedIoFile::kOutputMode,
|
2023-12-01 17:32:19 +00:00
|
|
|
absl::GetFlag(FLAGS_io_cache_size),
|
|
|
|
absl::GetFlag(FLAGS_io_block_size));
|
2015-03-19 18:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Threaded I/O is disabled.
|
|
|
|
DLOG(WARNING) << "Threaded I/O is disabled. Performance may be decreased.";
|
|
|
|
return internal_file.release();
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2015-05-20 17:14:36 +00:00
|
|
|
File* File::CreateInternalFile(const char* file_name, const char* mode) {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view real_file_name;
|
2017-06-15 20:00:28 +00:00
|
|
|
const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
|
|
|
|
DCHECK(file_type);
|
2021-08-25 15:38:05 +00:00
|
|
|
// Calls constructor for the derived File class.
|
2017-06-15 20:00:28 +00:00
|
|
|
return file_type->factory_function(real_file_name.data(), mode);
|
2015-05-20 17:14:36 +00:00
|
|
|
}
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
File* File::Open(const char* file_name, const char* mode) {
|
|
|
|
File* file = File::Create(file_name, mode);
|
|
|
|
if (!file)
|
2013-10-11 21:44:55 +00:00
|
|
|
return NULL;
|
|
|
|
if (!file->Open()) {
|
|
|
|
delete file;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:14:36 +00:00
|
|
|
File* File::OpenWithNoBuffering(const char* file_name, const char* mode) {
|
|
|
|
File* file = File::CreateInternalFile(file_name, mode);
|
|
|
|
if (!file)
|
|
|
|
return NULL;
|
|
|
|
if (!file->Open()) {
|
|
|
|
delete file;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return file;
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
bool File::Delete(const char* file_name) {
|
2021-06-22 18:57:51 +00:00
|
|
|
static bool logged = false;
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view real_file_name;
|
2017-06-15 20:00:28 +00:00
|
|
|
const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
|
|
|
|
DCHECK(file_type);
|
2021-06-22 18:57:51 +00:00
|
|
|
if (file_type->delete_function) {
|
|
|
|
return file_type->delete_function(real_file_name.data());
|
|
|
|
} else {
|
|
|
|
if (!logged) {
|
|
|
|
logged = true;
|
|
|
|
LOG(WARNING) << "File::Delete: file type for "
|
|
|
|
<< file_name
|
|
|
|
<< " ('" << file_type->type << "') "
|
|
|
|
<< "has no 'delete' function.";
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2015-03-11 19:18:17 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t File::GetFileSize(const char* file_name) {
|
2014-01-15 22:29:56 +00:00
|
|
|
File* file = File::Open(file_name, "r");
|
|
|
|
if (!file)
|
2013-10-11 21:44:55 +00:00
|
|
|
return -1;
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t res = file->Size();
|
2014-01-15 22:29:56 +00:00
|
|
|
file->Close();
|
2013-10-11 21:44:55 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-01-15 22:29:56 +00:00
|
|
|
bool File::ReadFileToString(const char* file_name, std::string* contents) {
|
|
|
|
DCHECK(contents);
|
|
|
|
|
|
|
|
File* file = File::Open(file_name, "r");
|
|
|
|
if (!file)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const size_t kBufferSize = 0x40000; // 256KB.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<char[]> buf(new char[kBufferSize]);
|
2014-01-15 22:29:56 +00:00
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t len;
|
2014-01-15 22:29:56 +00:00
|
|
|
while ((len = file->Read(buf.get(), kBufferSize)) > 0)
|
|
|
|
contents->append(buf.get(), len);
|
|
|
|
|
|
|
|
file->Close();
|
|
|
|
return len == 0;
|
|
|
|
}
|
|
|
|
|
2017-08-01 22:34:20 +00:00
|
|
|
bool File::WriteStringToFile(const char* file_name,
|
|
|
|
const std::string& contents) {
|
2021-02-02 18:51:50 +00:00
|
|
|
VLOG(2) << "File::WriteStringToFile: " << file_name;
|
2017-07-10 18:26:22 +00:00
|
|
|
std::unique_ptr<File, FileCloser> file(File::Open(file_name, "w"));
|
2017-06-15 20:00:28 +00:00
|
|
|
if (!file) {
|
|
|
|
LOG(ERROR) << "Failed to open file " << file_name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
int64_t bytes_written = file->Write(contents.data(), contents.size());
|
|
|
|
if (bytes_written < 0) {
|
|
|
|
LOG(ERROR) << "Failed to write to file '" << file_name << "' ("
|
|
|
|
<< bytes_written << ").";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (static_cast<size_t>(bytes_written) != contents.size()) {
|
|
|
|
LOG(ERROR) << "Failed to write the whole file to " << file_name
|
|
|
|
<< ". Wrote " << bytes_written << " but expecting "
|
|
|
|
<< contents.size() << " bytes.";
|
|
|
|
return false;
|
|
|
|
}
|
2017-09-07 23:05:20 +00:00
|
|
|
if (!file.release()->Close()) {
|
|
|
|
LOG(ERROR)
|
|
|
|
<< "Failed to close file '" << file_name
|
|
|
|
<< "', possibly file permission issue or running out of disk space.";
|
2017-08-01 22:34:20 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-06-15 20:00:28 +00:00
|
|
|
return true;
|
|
|
|
}
|
2017-08-01 22:34:20 +00:00
|
|
|
|
|
|
|
bool File::WriteFileAtomically(const char* file_name,
|
|
|
|
const std::string& contents) {
|
2021-02-02 18:51:50 +00:00
|
|
|
VLOG(2) << "File::WriteFileAtomically: " << file_name;
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view real_file_name;
|
2017-08-01 22:34:20 +00:00
|
|
|
const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
|
|
|
|
DCHECK(file_type);
|
|
|
|
if (file_type->atomic_write_function)
|
|
|
|
return file_type->atomic_write_function(real_file_name.data(), contents);
|
|
|
|
|
|
|
|
// Provide a default implementation which may not be atomic unfortunately.
|
|
|
|
|
|
|
|
// Skip the warning message for memory files, which is meant for testing
|
|
|
|
// anyway..
|
2021-05-25 18:59:53 +00:00
|
|
|
// Also check for http files, as they can't do atomic writes.
|
|
|
|
if (strncmp(file_name, kMemoryFilePrefix, strlen(kMemoryFilePrefix)) != 0
|
|
|
|
&& strncmp(file_name, kHttpFilePrefix, strlen(kHttpFilePrefix)) != 0
|
|
|
|
&& strncmp(file_name, kHttpsFilePrefix, strlen(kHttpsFilePrefix)) != 0) {
|
2017-08-01 22:34:20 +00:00
|
|
|
LOG(WARNING) << "Writing to " << file_name
|
|
|
|
<< " is not guaranteed to be atomic.";
|
|
|
|
}
|
|
|
|
return WriteStringToFile(file_name, contents);
|
|
|
|
}
|
2017-06-15 20:00:28 +00:00
|
|
|
|
2015-11-05 01:49:35 +00:00
|
|
|
bool File::Copy(const char* from_file_name, const char* to_file_name) {
|
|
|
|
std::string content;
|
2021-02-02 21:09:18 +00:00
|
|
|
VLOG(2) << "File::Copy from " << from_file_name << " to " << to_file_name;
|
2015-11-05 01:49:35 +00:00
|
|
|
if (!ReadFileToString(from_file_name, &content)) {
|
|
|
|
LOG(ERROR) << "Failed to open file " << from_file_name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> output_file(File::Open(to_file_name, "w"));
|
2015-11-05 01:49:35 +00:00
|
|
|
if (!output_file) {
|
|
|
|
LOG(ERROR) << "Failed to write to " << to_file_name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t bytes_left = content.size();
|
|
|
|
uint64_t total_bytes_written = 0;
|
|
|
|
const char* content_cstr = content.c_str();
|
|
|
|
while (bytes_left > total_bytes_written) {
|
|
|
|
const int64_t bytes_written =
|
|
|
|
output_file->Write(content_cstr + total_bytes_written, bytes_left);
|
|
|
|
if (bytes_written < 0) {
|
|
|
|
LOG(ERROR) << "Failure while writing to " << to_file_name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_bytes_written += bytes_written;
|
|
|
|
}
|
2017-09-07 23:05:20 +00:00
|
|
|
if (!output_file.release()->Close()) {
|
|
|
|
LOG(ERROR)
|
|
|
|
<< "Failed to close file '" << to_file_name
|
|
|
|
<< "', possibly file permission issue or running out of disk space.";
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-05 01:49:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
int64_t File::Copy(File* source, File* destination) {
|
|
|
|
return Copy(source, destination, kWholeFile);
|
2015-10-28 17:23:08 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
int64_t File::Copy(File* source, File* destination, int64_t max_copy) {
|
2015-10-28 17:23:08 +00:00
|
|
|
DCHECK(source);
|
|
|
|
DCHECK(destination);
|
|
|
|
if (max_copy < 0)
|
|
|
|
max_copy = std::numeric_limits<int64_t>::max();
|
|
|
|
|
2023-12-01 17:32:19 +00:00
|
|
|
VLOG(2) << "File::Copy from " << source->file_name() << " to "
|
2021-02-02 21:09:18 +00:00
|
|
|
<< destination->file_name();
|
2021-02-02 18:51:50 +00:00
|
|
|
|
2015-10-28 17:23:08 +00:00
|
|
|
const int64_t kBufferSize = 0x40000; // 256KB.
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<uint8_t[]> buffer(new uint8_t[kBufferSize]);
|
2015-10-28 17:23:08 +00:00
|
|
|
int64_t bytes_copied = 0;
|
|
|
|
while (bytes_copied < max_copy) {
|
|
|
|
const int64_t size = std::min(kBufferSize, max_copy - bytes_copied);
|
|
|
|
const int64_t bytes_read = source->Read(buffer.get(), size);
|
|
|
|
if (bytes_read < 0)
|
|
|
|
return bytes_read;
|
|
|
|
if (bytes_read == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
int64_t total_bytes_written = 0;
|
|
|
|
while (total_bytes_written < bytes_read) {
|
|
|
|
const int64_t bytes_written = destination->Write(
|
|
|
|
buffer.get() + total_bytes_written, bytes_read - total_bytes_written);
|
|
|
|
if (bytes_written < 0)
|
|
|
|
return bytes_written;
|
|
|
|
|
|
|
|
total_bytes_written += bytes_written;
|
|
|
|
}
|
|
|
|
|
|
|
|
DCHECK_EQ(total_bytes_written, bytes_read);
|
|
|
|
bytes_copied += bytes_read;
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes_copied;
|
|
|
|
}
|
|
|
|
|
2019-10-18 22:15:31 +00:00
|
|
|
bool File::IsLocalRegularFile(const char* file_name) {
|
2023-12-01 17:32:19 +00:00
|
|
|
std::string_view real_file_name;
|
2019-10-18 22:15:31 +00:00
|
|
|
const FileTypeInfo* file_type = GetFileTypeInfo(file_name, &real_file_name);
|
|
|
|
DCHECK(file_type);
|
2023-12-01 17:32:19 +00:00
|
|
|
|
2019-10-18 22:15:31 +00:00
|
|
|
if (file_type->type != kLocalFilePrefix)
|
|
|
|
return false;
|
2023-12-01 17:32:19 +00:00
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
auto real_file_path = std::filesystem::u8path(real_file_name);
|
|
|
|
return std::filesystem::is_regular_file(real_file_path, ec);
|
2019-10-18 22:15:31 +00:00
|
|
|
}
|
|
|
|
|
2017-07-17 18:15:07 +00:00
|
|
|
std::string File::MakeCallbackFileName(
|
|
|
|
const BufferCallbackParams& callback_params,
|
|
|
|
const std::string& name) {
|
|
|
|
if (name.empty())
|
|
|
|
return "";
|
2023-12-01 17:32:19 +00:00
|
|
|
return absl::StrFormat("%s%" PRIdPTR "/%s", kCallbackFilePrefix,
|
|
|
|
reinterpret_cast<intptr_t>(&callback_params),
|
|
|
|
name.c_str());
|
2017-07-17 18:15:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool File::ParseCallbackFileName(const std::string& callback_file_name,
|
|
|
|
const BufferCallbackParams** callback_params,
|
|
|
|
std::string* name) {
|
|
|
|
size_t pos = callback_file_name.find("/");
|
|
|
|
int64_t callback_address = 0;
|
|
|
|
if (pos == std::string::npos ||
|
2023-12-01 17:32:19 +00:00
|
|
|
!absl::SimpleAtoi(callback_file_name.substr(0, pos), &callback_address)) {
|
2017-07-17 18:15:07 +00:00
|
|
|
LOG(ERROR) << "Expecting CallbackFile with name like "
|
|
|
|
"'<callback address>/<entity name>', but seeing "
|
|
|
|
<< callback_file_name;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
*callback_params = reinterpret_cast<BufferCallbackParams*>(callback_address);
|
|
|
|
*name = callback_file_name.substr(pos + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|