2014-02-14 23:21:05 +00:00
|
|
|
// Copyright 2014 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/local_file.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2015-05-20 17:14:36 +00:00
|
|
|
#include <stdio.h>
|
2016-08-14 22:28:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
#include <windows.h>
|
2018-11-19 22:52:44 +00:00
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
2016-08-14 22:28:21 +00:00
|
|
|
#endif // defined(OS_WIN)
|
2018-11-19 22:52:44 +00:00
|
|
|
#include "packager/base/files/file_path.h"
|
2015-07-22 23:40:45 +00:00
|
|
|
#include "packager/base/files/file_util.h"
|
2014-10-01 22:10:21 +00:00
|
|
|
#include "packager/base/logging.h"
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2018-11-19 22:52:44 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
// Check if the directory |path| exists. Returns false if it does not exist or
|
|
|
|
// it is not a directory. On non-Windows, |mode| will be filled with the file
|
|
|
|
// permission bits on success.
|
|
|
|
bool DirectoryExists(const base::FilePath& path, int* mode) {
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
DWORD fileattr = GetFileAttributes(path.value().c_str());
|
|
|
|
if (fileattr != INVALID_FILE_ATTRIBUTES)
|
|
|
|
return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
|
|
|
#else
|
|
|
|
struct stat info;
|
|
|
|
if (stat(path.value().c_str(), &info) != 0)
|
|
|
|
return false;
|
|
|
|
if (S_ISDIR(info.st_mode)) {
|
|
|
|
const int FILE_PERMISSION_MASK = S_IRWXU | S_IRWXG | S_IRWXO;
|
|
|
|
if (mode)
|
|
|
|
*mode = info.st_mode & FILE_PERMISSION_MASK;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create all the inexistent directories in the path. Returns true on success or
|
|
|
|
// if the directory already exists.
|
|
|
|
bool CreateDirectory(const base::FilePath& full_path) {
|
|
|
|
std::vector<base::FilePath> subpaths;
|
|
|
|
|
|
|
|
// Collect a list of all parent directories.
|
|
|
|
base::FilePath last_path = full_path;
|
|
|
|
subpaths.push_back(full_path);
|
|
|
|
for (base::FilePath path = full_path.DirName();
|
|
|
|
path.value() != last_path.value(); path = path.DirName()) {
|
|
|
|
subpaths.push_back(path);
|
|
|
|
last_path = path;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For non-Windows only. File permission for the new directories.
|
|
|
|
// The file permission will be inherited from the last existing directory in
|
|
|
|
// the file path. If none of the directory exists in the path, it is set to
|
|
|
|
// 0755 by default.
|
|
|
|
int mode = 0755;
|
|
|
|
|
|
|
|
// Iterate through the parents and create the missing ones.
|
|
|
|
for (auto i = subpaths.rbegin(); i != subpaths.rend(); ++i) {
|
|
|
|
if (DirectoryExists(*i, &mode)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#if defined(OS_WIN)
|
|
|
|
if (::CreateDirectory(i->value().c_str(), nullptr)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (mkdir(i->value().c_str(), mode) == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Mkdir failed, but it might have failed with EEXIST, or some other error
|
|
|
|
// due to the the directory appearing out of thin air. This can occur if
|
|
|
|
// two processes are trying to create the same file system tree at the same
|
|
|
|
// time. Check to see if it exists and make sure it is a directory.
|
|
|
|
const auto saved_error_code = ::logging::GetLastSystemErrorCode();
|
|
|
|
if (!DirectoryExists(*i, nullptr)) {
|
|
|
|
LOG(ERROR) << "Failed to create directory " << i->value().c_str()
|
|
|
|
<< " ErrorCode " << saved_error_code;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
2013-10-11 21:44:55 +00:00
|
|
|
|
2016-08-14 22:28:21 +00:00
|
|
|
// Always open files in binary mode.
|
|
|
|
const char kAdditionalFileMode[] = "b";
|
|
|
|
|
2014-01-24 00:26:00 +00:00
|
|
|
LocalFile::LocalFile(const char* file_name, const char* mode)
|
2017-07-10 18:26:22 +00:00
|
|
|
: File(file_name), file_mode_(mode), internal_file_(NULL) {
|
2016-08-14 22:28:21 +00:00
|
|
|
if (file_mode_.find(kAdditionalFileMode) == std::string::npos)
|
|
|
|
file_mode_ += kAdditionalFileMode;
|
|
|
|
}
|
2013-10-11 21:44:55 +00:00
|
|
|
|
|
|
|
bool LocalFile::Close() {
|
|
|
|
bool result = true;
|
|
|
|
if (internal_file_) {
|
2014-02-26 23:55:01 +00:00
|
|
|
result = base::CloseFile(internal_file_);
|
2013-10-11 21:44:55 +00:00
|
|
|
internal_file_ = NULL;
|
|
|
|
}
|
|
|
|
delete this;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t LocalFile::Read(void* buffer, uint64_t length) {
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(buffer != NULL);
|
|
|
|
DCHECK(internal_file_ != NULL);
|
2017-09-07 23:05:20 +00:00
|
|
|
size_t bytes_read = fread(buffer, sizeof(char), length, internal_file_);
|
|
|
|
VLOG(2) << "Read " << length << " return " << bytes_read << " error "
|
|
|
|
<< ferror(internal_file_);
|
|
|
|
if (bytes_read == 0 && ferror(internal_file_) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bytes_read;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t LocalFile::Write(const void* buffer, uint64_t length) {
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(buffer != NULL);
|
|
|
|
DCHECK(internal_file_ != NULL);
|
2017-09-07 23:05:20 +00:00
|
|
|
size_t bytes_written = fwrite(buffer, sizeof(char), length, internal_file_);
|
|
|
|
VLOG(2) << "Write " << length << " return " << bytes_written << " error "
|
|
|
|
<< ferror(internal_file_);
|
|
|
|
if (bytes_written == 0 && ferror(internal_file_) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return bytes_written;
|
2013-10-11 21:44:55 +00:00
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t LocalFile::Size() {
|
2013-10-11 21:44:55 +00:00
|
|
|
DCHECK(internal_file_ != NULL);
|
|
|
|
|
|
|
|
// Flush any buffered data, so we get the true file size.
|
|
|
|
if (!Flush()) {
|
|
|
|
LOG(ERROR) << "Cannot flush file.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-09-30 21:52:21 +00:00
|
|
|
int64_t file_size;
|
2016-08-14 22:28:21 +00:00
|
|
|
if (!base::GetFileSize(base::FilePath::FromUTF8Unsafe(file_name()),
|
2017-07-10 18:26:22 +00:00
|
|
|
&file_size)) {
|
2013-10-11 21:44:55 +00:00
|
|
|
LOG(ERROR) << "Cannot get file size.";
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return file_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalFile::Flush() {
|
|
|
|
DCHECK(internal_file_ != NULL);
|
|
|
|
return ((fflush(internal_file_) == 0) && !ferror(internal_file_));
|
|
|
|
}
|
|
|
|
|
2015-05-20 17:14:36 +00:00
|
|
|
bool LocalFile::Seek(uint64_t position) {
|
2016-08-14 22:28:21 +00:00
|
|
|
#if defined(OS_WIN)
|
2017-07-10 18:26:22 +00:00
|
|
|
return _fseeki64(internal_file_, static_cast<__int64>(position), SEEK_SET) ==
|
|
|
|
0;
|
2016-08-14 22:28:21 +00:00
|
|
|
#else
|
2015-05-20 17:14:36 +00:00
|
|
|
return fseeko(internal_file_, position, SEEK_SET) >= 0;
|
2016-08-14 22:28:21 +00:00
|
|
|
#endif // !defined(OS_WIN)
|
2015-05-20 17:14:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalFile::Tell(uint64_t* position) {
|
2016-08-14 22:28:21 +00:00
|
|
|
#if defined(OS_WIN)
|
|
|
|
__int64 offset = _ftelli64(internal_file_);
|
|
|
|
#else
|
2015-05-20 17:14:36 +00:00
|
|
|
off_t offset = ftello(internal_file_);
|
2016-08-14 22:28:21 +00:00
|
|
|
#endif // !defined(OS_WIN)
|
2015-05-20 17:14:36 +00:00
|
|
|
if (offset < 0)
|
|
|
|
return false;
|
2016-08-14 22:28:21 +00:00
|
|
|
*position = static_cast<uint64_t>(offset);
|
2015-05-20 17:14:36 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-01-24 00:26:00 +00:00
|
|
|
LocalFile::~LocalFile() {}
|
|
|
|
|
|
|
|
bool LocalFile::Open() {
|
2017-10-10 21:54:36 +00:00
|
|
|
base::FilePath file_path(base::FilePath::FromUTF8Unsafe(file_name()));
|
|
|
|
|
|
|
|
// Create upper level directories for write mode.
|
|
|
|
if (file_mode_.find("w") != std::string::npos) {
|
2018-11-19 22:52:44 +00:00
|
|
|
// The function returns true if the directories already exist.
|
|
|
|
if (!shaka::CreateDirectory(file_path.DirName())) {
|
2017-10-10 21:54:36 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
internal_file_ = base::OpenFile(file_path, file_mode_.c_str());
|
2014-01-24 00:26:00 +00:00
|
|
|
return (internal_file_ != NULL);
|
|
|
|
}
|
|
|
|
|
2015-03-11 19:18:17 +00:00
|
|
|
bool LocalFile::Delete(const char* file_name) {
|
2016-08-14 22:28:21 +00:00
|
|
|
return base::DeleteFile(base::FilePath::FromUTF8Unsafe(file_name), false);
|
2015-03-11 19:18:17 +00:00
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|