2016-11-15 18:56:02 +01:00
|
|
|
// Copyright 2016 The Chromium Embedded Framework Authors. Portions copyright
|
|
|
|
// 2012 The Chromium Authors. All rights reserved. Use of this source code is
|
|
|
|
// governed by a BSD-style license that can be found in the LICENSE file.
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
#include "tests/shared/browser/file_util.h"
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <memory>
|
|
|
|
|
2021-06-17 22:08:01 +02:00
|
|
|
#include "include/base/cef_build.h"
|
|
|
|
#include "include/cef_task.h"
|
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
namespace client::file_util {
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
bool AllowFileIO() {
|
|
|
|
if (CefCurrentlyOn(TID_UI) || CefCurrentlyOn(TID_IO)) {
|
|
|
|
NOTREACHED() << "file IO is not allowed on the current thread";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
const char kPathSep = '\\';
|
|
|
|
#else
|
|
|
|
const char kPathSep = '/';
|
|
|
|
#endif
|
|
|
|
|
2017-05-17 11:29:28 +02:00
|
|
|
bool ReadFileToString(const std::string& path,
|
|
|
|
std::string* contents,
|
2016-11-15 18:56:02 +01:00
|
|
|
size_t max_size) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
2023-01-02 23:59:03 +01:00
|
|
|
if (contents) {
|
2016-11-15 18:56:02 +01:00
|
|
|
contents->clear();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
FILE* file = fopen(path.c_str(), "rb");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!file) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return false;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
const size_t kBufferSize = 1 << 16;
|
2021-06-17 22:08:01 +02:00
|
|
|
std::unique_ptr<char[]> buf(new char[kBufferSize]);
|
2016-11-15 18:56:02 +01:00
|
|
|
size_t len;
|
|
|
|
size_t size = 0;
|
|
|
|
bool read_status = true;
|
|
|
|
|
|
|
|
// Many files supplied in |path| have incorrect size (proc files etc).
|
|
|
|
// Hence, the file is read sequentially as opposed to a one-shot read.
|
|
|
|
while ((len = fread(buf.get(), 1, kBufferSize, file)) > 0) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (contents) {
|
2016-11-15 18:56:02 +01:00
|
|
|
contents->append(buf.get(), std::min(len, max_size - size));
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
if ((max_size - size) < len) {
|
|
|
|
read_status = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
size += len;
|
|
|
|
}
|
|
|
|
read_status = read_status && !ferror(file);
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return read_status;
|
|
|
|
}
|
|
|
|
|
|
|
|
int WriteFile(const std::string& path, const char* data, int size) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!AllowFileIO()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return -1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
FILE* file = fopen(path.c_str(), "wb");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (!file) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return -1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
int written = 0;
|
|
|
|
|
|
|
|
do {
|
|
|
|
size_t write = fwrite(data + written, 1, size - written, file);
|
2023-01-02 23:59:03 +01:00
|
|
|
if (write == 0) {
|
2016-11-15 18:56:02 +01:00
|
|
|
break;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-30 21:23:04 +01:00
|
|
|
written += static_cast<int>(write);
|
2016-11-15 18:56:02 +01:00
|
|
|
} while (written < size);
|
|
|
|
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return written;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string JoinPath(const std::string& path1, const std::string& path2) {
|
2023-01-02 23:59:03 +01:00
|
|
|
if (path1.empty() && path2.empty()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return std::string();
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (path1.empty()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return path2;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (path2.empty()) {
|
2016-11-15 18:56:02 +01:00
|
|
|
return path1;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
|
|
|
|
std::string result = path1;
|
2023-01-02 23:59:03 +01:00
|
|
|
if (result[result.size() - 1] != kPathSep) {
|
2016-11-15 18:56:02 +01:00
|
|
|
result += kPathSep;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
|
|
|
if (path2[0] == kPathSep) {
|
2016-11-15 18:56:02 +01:00
|
|
|
result += path2.substr(1);
|
2023-01-02 23:59:03 +01:00
|
|
|
} else {
|
2016-11-15 18:56:02 +01:00
|
|
|
result += path2;
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2016-11-15 18:56:02 +01:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-04 00:55:19 +02:00
|
|
|
std::string GetFileExtension(const std::string& path) {
|
|
|
|
size_t sep = path.find_last_of(".");
|
2023-01-02 23:59:03 +01:00
|
|
|
if (sep != std::string::npos) {
|
2017-08-04 00:55:19 +02:00
|
|
|
return path.substr(sep + 1);
|
2023-01-02 23:59:03 +01:00
|
|
|
}
|
2017-08-04 00:55:19 +02:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2024-01-20 03:22:56 +01:00
|
|
|
} // namespace client::file_util
|