mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-10-09 11:25:00 +00:00
Bug 1426526 - Delete file_util from ex-Chromium IPC source. r=froydnj
Depends on D26746 Differential Revision: https://phabricator.services.mozilla.com/D26747 --HG-- extra : moz-landing-system : lando
This commit is contained in:
parent
ee88930515
commit
150e57c3cc
@ -39,7 +39,6 @@
|
||||
#include "mozilla/Unused.h"
|
||||
|
||||
#include "base/eintr_wrapper.h"
|
||||
#include "base/file_util.h"
|
||||
|
||||
#include <locale.h>
|
||||
|
||||
|
@ -11,7 +11,6 @@ UNIFIED_SOURCES += [
|
||||
'src/base/at_exit.cc',
|
||||
'src/base/command_line.cc',
|
||||
'src/base/file_path.cc',
|
||||
'src/base/file_util.cc',
|
||||
'src/base/histogram.cc',
|
||||
'src/base/logging.cc',
|
||||
'src/base/message_loop.cc',
|
||||
@ -35,7 +34,6 @@ UNIFIED_SOURCES += [
|
||||
if os_win:
|
||||
SOURCES += [
|
||||
'src/base/condition_variable_win.cc',
|
||||
'src/base/file_util_win.cc',
|
||||
'src/base/lock_impl_win.cc',
|
||||
'src/base/message_pump_win.cc',
|
||||
'src/base/object_watcher.cc',
|
||||
@ -57,7 +55,6 @@ elif not CONFIG['MOZ_SYSTEM_LIBEVENT']:
|
||||
if os_posix:
|
||||
UNIFIED_SOURCES += [
|
||||
'src/base/condition_variable_posix.cc',
|
||||
'src/base/file_util_posix.cc',
|
||||
'src/base/lock_impl_posix.cc',
|
||||
'src/base/message_pump_libevent.cc',
|
||||
'src/base/platform_thread_posix.cc',
|
||||
@ -74,7 +71,6 @@ if os_posix:
|
||||
if os_macosx:
|
||||
UNIFIED_SOURCES += [
|
||||
'src/base/chrome_application_mac.mm',
|
||||
'src/base/file_util_mac.mm',
|
||||
'src/base/mac_util.mm',
|
||||
'src/base/message_pump_mac.mm',
|
||||
'src/base/process_util_mac.mm',
|
||||
|
@ -1,234 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2006-2009 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.
|
||||
|
||||
#include "base/file_util.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
# include <io.h>
|
||||
#endif
|
||||
#include <stdio.h>
|
||||
#if defined(ANDROID) || defined(OS_POSIX)
|
||||
# include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include <fstream>
|
||||
|
||||
#include "base/file_path.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/string_util.h"
|
||||
|
||||
#include "base/string_piece.h"
|
||||
#include "base/sys_string_conversions.h"
|
||||
|
||||
namespace {
|
||||
|
||||
const FilePath::CharType kExtensionSeparator = FILE_PATH_LITERAL('.');
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace file_util {
|
||||
|
||||
bool EndsWithSeparator(const FilePath& path) {
|
||||
FilePath::StringType value = path.value();
|
||||
if (value.empty()) return false;
|
||||
|
||||
return FilePath::IsSeparator(value[value.size() - 1]);
|
||||
}
|
||||
|
||||
void TrimTrailingSeparator(std::wstring* dir) {
|
||||
while (dir->length() > 1 && EndsWithSeparator(dir))
|
||||
dir->resize(dir->length() - 1);
|
||||
}
|
||||
|
||||
FilePath::StringType GetFileExtensionFromPath(const FilePath& path) {
|
||||
FilePath::StringType file_name = path.BaseName().value();
|
||||
const FilePath::StringType::size_type last_dot =
|
||||
file_name.rfind(kExtensionSeparator);
|
||||
return FilePath::StringType(last_dot == FilePath::StringType::npos
|
||||
? FILE_PATH_LITERAL("")
|
||||
: file_name,
|
||||
last_dot + 1);
|
||||
}
|
||||
|
||||
void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix) {
|
||||
FilePath::StringType& value =
|
||||
const_cast<FilePath::StringType&>(path->value());
|
||||
|
||||
const FilePath::StringType::size_type last_dot =
|
||||
value.rfind(kExtensionSeparator);
|
||||
const FilePath::StringType::size_type last_separator =
|
||||
value.find_last_of(FilePath::StringType(FilePath::kSeparators));
|
||||
|
||||
if (last_dot == FilePath::StringType::npos ||
|
||||
(last_separator != std::wstring::npos && last_dot < last_separator)) {
|
||||
// The path looks something like "C:\pics.old\jojo" or "C:\pics\jojo".
|
||||
// We should just append the suffix to the entire path.
|
||||
value.append(suffix);
|
||||
return;
|
||||
}
|
||||
|
||||
value.insert(last_dot, suffix);
|
||||
}
|
||||
|
||||
void ReplaceExtension(FilePath* path, const FilePath::StringType& extension) {
|
||||
FilePath::StringType clean_extension;
|
||||
// If the new extension is "" or ".", then we will just remove the current
|
||||
// extension.
|
||||
if (!extension.empty() &&
|
||||
extension != FilePath::StringType(&kExtensionSeparator, 1)) {
|
||||
if (extension[0] != kExtensionSeparator)
|
||||
clean_extension.append(&kExtensionSeparator, 1);
|
||||
clean_extension.append(extension);
|
||||
}
|
||||
|
||||
FilePath::StringType& value =
|
||||
const_cast<FilePath::StringType&>(path->value());
|
||||
const FilePath::StringType::size_type last_dot =
|
||||
value.rfind(kExtensionSeparator);
|
||||
const FilePath::StringType::size_type last_separator =
|
||||
value.find_last_of(FilePath::StringType(FilePath::kSeparators));
|
||||
|
||||
// Erase the current extension, if any.
|
||||
if ((last_dot > last_separator ||
|
||||
last_separator == FilePath::StringType::npos) &&
|
||||
last_dot != FilePath::StringType::npos)
|
||||
value.erase(last_dot);
|
||||
|
||||
value.append(clean_extension);
|
||||
}
|
||||
|
||||
FILE* CreateAndOpenTemporaryFile(FilePath* path) {
|
||||
FilePath directory;
|
||||
if (!GetTempDir(&directory)) return NULL;
|
||||
|
||||
return CreateAndOpenTemporaryFileInDir(directory, path);
|
||||
}
|
||||
|
||||
bool GetFileSize(const FilePath& file_path, int64_t* file_size) {
|
||||
FileInfo info;
|
||||
if (!GetFileInfo(file_path, &info)) return false;
|
||||
*file_size = info.size;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CloseFile(FILE* file) {
|
||||
if (file == NULL) return true;
|
||||
return fclose(file) == 0;
|
||||
}
|
||||
|
||||
// Deprecated functions ----------------------------------------------------
|
||||
|
||||
bool AbsolutePath(std::wstring* path_str) {
|
||||
FilePath path(FilePath::FromWStringHack(*path_str));
|
||||
if (!AbsolutePath(&path)) return false;
|
||||
*path_str = path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
void AppendToPath(std::wstring* path, const std::wstring& new_ending) {
|
||||
if (!path) {
|
||||
NOTREACHED();
|
||||
return; // Don't crash in this function in release builds.
|
||||
}
|
||||
|
||||
if (!EndsWithSeparator(path)) path->push_back(FilePath::kSeparators[0]);
|
||||
path->append(new_ending);
|
||||
}
|
||||
bool CopyFile(const std::wstring& from_path, const std::wstring& to_path) {
|
||||
return CopyFile(FilePath::FromWStringHack(from_path),
|
||||
FilePath::FromWStringHack(to_path));
|
||||
}
|
||||
bool CreateDirectory(const std::wstring& full_path) {
|
||||
return CreateDirectory(FilePath::FromWStringHack(full_path));
|
||||
}
|
||||
bool CreateNewTempDirectory(const std::wstring& prefix,
|
||||
std::wstring* new_temp_path) {
|
||||
#if defined(OS_WIN)
|
||||
FilePath::StringType dir_prefix(prefix);
|
||||
#elif defined(OS_POSIX)
|
||||
FilePath::StringType dir_prefix = WideToUTF8(prefix);
|
||||
#endif
|
||||
FilePath temp_path;
|
||||
if (!CreateNewTempDirectory(dir_prefix, &temp_path)) return false;
|
||||
*new_temp_path = temp_path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
bool CreateTemporaryFileName(std::wstring* temp_file) {
|
||||
FilePath temp_file_path;
|
||||
if (!CreateTemporaryFileName(&temp_file_path)) return false;
|
||||
*temp_file = temp_file_path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
bool Delete(const std::wstring& path) {
|
||||
return Delete(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool DirectoryExists(const std::wstring& path) {
|
||||
return DirectoryExists(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool EndsWithSeparator(std::wstring* path) {
|
||||
return EndsWithSeparator(FilePath::FromWStringHack(*path));
|
||||
}
|
||||
bool EndsWithSeparator(const std::wstring& path) {
|
||||
return EndsWithSeparator(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool GetCurrentDirectory(std::wstring* path_str) {
|
||||
FilePath path;
|
||||
if (!GetCurrentDirectory(&path)) return false;
|
||||
*path_str = path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
std::wstring GetFileExtensionFromPath(const std::wstring& path) {
|
||||
FilePath::StringType extension =
|
||||
GetFileExtensionFromPath(FilePath::FromWStringHack(path));
|
||||
#if defined(OS_WIN)
|
||||
return extension;
|
||||
#elif defined(OS_POSIX)
|
||||
return UTF8ToWide(extension);
|
||||
#endif
|
||||
}
|
||||
bool GetFileInfo(const std::wstring& file_path, FileInfo* results) {
|
||||
return GetFileInfo(FilePath::FromWStringHack(file_path), results);
|
||||
}
|
||||
std::wstring GetFilenameFromPath(const std::wstring& path) {
|
||||
if (path.empty() || EndsWithSeparator(path)) return std::wstring();
|
||||
|
||||
return FilePath::FromWStringHack(path).BaseName().ToWStringHack();
|
||||
}
|
||||
bool GetFileSize(const std::wstring& file_path, int64_t* file_size) {
|
||||
return GetFileSize(FilePath::FromWStringHack(file_path), file_size);
|
||||
}
|
||||
bool GetTempDir(std::wstring* path_str) {
|
||||
FilePath path;
|
||||
if (!GetTempDir(&path)) return false;
|
||||
*path_str = path.ToWStringHack();
|
||||
return true;
|
||||
}
|
||||
FILE* OpenFile(const std::wstring& filename, const char* mode) {
|
||||
return OpenFile(FilePath::FromWStringHack(filename), mode);
|
||||
}
|
||||
bool PathExists(const std::wstring& path) {
|
||||
return PathExists(FilePath::FromWStringHack(path));
|
||||
}
|
||||
bool PathIsWritable(const std::wstring& path) {
|
||||
return PathIsWritable(FilePath::FromWStringHack(path));
|
||||
}
|
||||
int ReadFile(const std::wstring& filename, char* data, int size) {
|
||||
return ReadFile(FilePath::FromWStringHack(filename), data, size);
|
||||
}
|
||||
bool SetCurrentDirectory(const std::wstring& directory) {
|
||||
return SetCurrentDirectory(FilePath::FromWStringHack(directory));
|
||||
}
|
||||
void UpOneDirectory(std::wstring* dir) {
|
||||
FilePath path = FilePath::FromWStringHack(*dir);
|
||||
FilePath directory = path.DirName();
|
||||
// If there is no separator, we will get back kCurrentDirectory.
|
||||
// In this case don't change |dir|.
|
||||
if (directory.value() != FilePath::kCurrentDirectory)
|
||||
*dir = directory.ToWStringHack();
|
||||
}
|
||||
int WriteFile(const std::wstring& filename, const char* data, int size) {
|
||||
return WriteFile(FilePath::FromWStringHack(filename), data, size);
|
||||
}
|
||||
} // namespace file_util
|
@ -1,229 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2006-2008 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.
|
||||
|
||||
// This file contains utility functions for dealing with the local
|
||||
// filesystem.
|
||||
|
||||
#ifndef BASE_FILE_UTIL_H_
|
||||
#define BASE_FILE_UTIL_H_
|
||||
|
||||
#include "build/build_config.h"
|
||||
|
||||
#if defined(OS_WIN)
|
||||
# include <windows.h>
|
||||
#elif defined(ANDROID)
|
||||
# include <sys/stat.h>
|
||||
#elif defined(OS_POSIX)
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stack>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "base/file_path.h"
|
||||
|
||||
namespace file_util {
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Functions that operate purely on a path string w/o touching the filesystem:
|
||||
|
||||
// Returns true if the given path ends with a path separator character.
|
||||
bool EndsWithSeparator(const FilePath& path);
|
||||
// These two versions are both deprecated. TODO(estade): remove them.
|
||||
bool EndsWithSeparator(std::wstring* path);
|
||||
bool EndsWithSeparator(const std::wstring& path);
|
||||
|
||||
// Modifies a string by trimming all trailing separators from the end.
|
||||
// Deprecated. FilePath does this automatically, and if it's constructed from a
|
||||
// path with a trailing separator, StripTrailingSeparators() may be used.
|
||||
void TrimTrailingSeparator(std::wstring* dir);
|
||||
|
||||
// Strips the topmost directory from the end of 'dir'. Assumes 'dir' does not
|
||||
// refer to a file.
|
||||
// If 'dir' is a root directory, return without change.
|
||||
// Deprecated. Use FilePath::DirName instead.
|
||||
void UpOneDirectory(std::wstring* dir);
|
||||
|
||||
// Returns the filename portion of 'path', without any leading \'s or /'s.
|
||||
// Deprecated. Use FilePath::BaseName instead.
|
||||
std::wstring GetFilenameFromPath(const std::wstring& path);
|
||||
|
||||
// Deprecated compatibility function. Use FilePath::Extension.
|
||||
FilePath::StringType GetFileExtensionFromPath(const FilePath& path);
|
||||
// Deprecated temporary compatibility function.
|
||||
std::wstring GetFileExtensionFromPath(const std::wstring& path);
|
||||
|
||||
// Appends new_ending to path, adding a separator between the two if necessary.
|
||||
void AppendToPath(std::wstring* path, const std::wstring& new_ending);
|
||||
|
||||
// Convert provided relative path into an absolute path. Returns false on
|
||||
// error. On POSIX, this function fails if the path does not exist.
|
||||
bool AbsolutePath(FilePath* path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool AbsolutePath(std::wstring* path);
|
||||
|
||||
// Deprecated compatibility function. Use FilePath::InsertBeforeExtension.
|
||||
void InsertBeforeExtension(FilePath* path, const FilePath::StringType& suffix);
|
||||
|
||||
// Deprecated compatibility function. Use FilePath::ReplaceExtension.
|
||||
void ReplaceExtension(FilePath* file_name,
|
||||
const FilePath::StringType& extension);
|
||||
|
||||
#if defined(OS_WIN)
|
||||
// Deprecated temporary compatibility functions.
|
||||
void InsertBeforeExtension(std::wstring* path, const std::wstring& suffix);
|
||||
void ReplaceExtension(std::wstring* file_name, const std::wstring& extension);
|
||||
#endif
|
||||
|
||||
//-----------------------------------------------------------------------------
|
||||
// Functions that involve filesystem access or modification:
|
||||
|
||||
// Deletes the given path, whether it's a file or a directory.
|
||||
// If it's a directory, it's perfectly happy to delete all of the
|
||||
// directory's contents.
|
||||
// Returns true if successful, false otherwise.
|
||||
bool Delete(const FilePath& path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool Delete(const std::wstring& path);
|
||||
|
||||
// Copies a single file. Use CopyDirectory to copy directories.
|
||||
bool CopyFile(const FilePath& from_path, const FilePath& to_path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool CopyFile(const std::wstring& from_path, const std::wstring& to_path);
|
||||
|
||||
// Returns true if the given path exists on the local filesystem,
|
||||
// false otherwise.
|
||||
bool PathExists(const FilePath& path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool PathExists(const std::wstring& path);
|
||||
|
||||
// Returns true if the given path is writable by the user, false otherwise.
|
||||
bool PathIsWritable(const FilePath& path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool PathIsWritable(const std::wstring& path);
|
||||
|
||||
// Returns true if the given path exists and is a directory, false otherwise.
|
||||
bool DirectoryExists(const FilePath& path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool DirectoryExists(const std::wstring& path);
|
||||
|
||||
#if defined(OS_POSIX)
|
||||
// Read exactly |bytes| bytes from file descriptor |fd|, storing the result
|
||||
// in |buffer|. This function is protected against EINTR and partial reads.
|
||||
// Returns true iff |bytes| bytes have been successfuly read from |fd|.
|
||||
bool ReadFromFD(int fd, char* buffer, size_t bytes);
|
||||
#endif // defined(OS_POSIX)
|
||||
|
||||
// Get the temporary directory provided by the system.
|
||||
bool GetTempDir(FilePath* path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool GetTempDir(std::wstring* path);
|
||||
// Get a temporary directory for shared memory files.
|
||||
// Only useful on POSIX; redirects to GetTempDir() on Windows.
|
||||
bool GetShmemTempDir(FilePath* path);
|
||||
|
||||
// Creates a temporary file. The full path is placed in |path|, and the
|
||||
// function returns true if was successful in creating the file. The file will
|
||||
// be empty and all handles closed after this function returns.
|
||||
// TODO(erikkay): rename this function and track down all of the callers.
|
||||
// (Clarification of erik's comment: the intent is to rename the BlahFileName()
|
||||
// calls into BlahFile(), since they create temp files (not temp filenames).)
|
||||
bool CreateTemporaryFileName(FilePath* path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool CreateTemporaryFileName(std::wstring* temp_file);
|
||||
|
||||
// Create and open a temporary file. File is opened for read/write.
|
||||
// The full path is placed in |path|, and the function returns true if
|
||||
// was successful in creating and opening the file.
|
||||
FILE* CreateAndOpenTemporaryFile(FilePath* path);
|
||||
// Like above but for shmem files. Only useful for POSIX.
|
||||
FILE* CreateAndOpenTemporaryShmemFile(FilePath* path);
|
||||
|
||||
// Similar to CreateAndOpenTemporaryFile, but the file is created in |dir|.
|
||||
FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path);
|
||||
|
||||
// Same as CreateTemporaryFileName but the file is created in |dir|.
|
||||
bool CreateTemporaryFileNameInDir(const std::wstring& dir,
|
||||
std::wstring* temp_file);
|
||||
|
||||
// Create a new directory under TempPath. If prefix is provided, the new
|
||||
// directory name is in the format of prefixyyyy.
|
||||
// NOTE: prefix is ignored in the POSIX implementation.
|
||||
// TODO(erikkay): is this OK?
|
||||
// If success, return true and output the full path of the directory created.
|
||||
bool CreateNewTempDirectory(const FilePath::StringType& prefix,
|
||||
FilePath* new_temp_path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool CreateNewTempDirectory(const std::wstring& prefix,
|
||||
std::wstring* new_temp_path);
|
||||
|
||||
// Creates a directory, as well as creating any parent directories, if they
|
||||
// don't exist. Returns 'true' on successful creation, or if the directory
|
||||
// already exists.
|
||||
bool CreateDirectory(const FilePath& full_path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool CreateDirectory(const std::wstring& full_path);
|
||||
|
||||
// Returns the file size. Returns true on success.
|
||||
bool GetFileSize(const FilePath& file_path, int64_t* file_size);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool GetFileSize(const std::wstring& file_path, int64_t* file_size);
|
||||
|
||||
// Used to hold information about a given file path. See GetFileInfo below.
|
||||
struct FileInfo {
|
||||
// The size of the file in bytes. Undefined when is_directory is true.
|
||||
int64_t size;
|
||||
|
||||
// True if the file corresponds to a directory.
|
||||
bool is_directory;
|
||||
|
||||
// Add additional fields here as needed.
|
||||
};
|
||||
|
||||
// Returns information about the given file path.
|
||||
bool GetFileInfo(const FilePath& file_path, FileInfo* info);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool GetFileInfo(const std::wstring& file_path, FileInfo* info);
|
||||
|
||||
// Wrapper for fopen-like calls. Returns non-NULL FILE* on success.
|
||||
FILE* OpenFile(const FilePath& filename, const char* mode);
|
||||
// Deprecated temporary compatibility functions.
|
||||
FILE* OpenFile(const std::string& filename, const char* mode);
|
||||
FILE* OpenFile(const std::wstring& filename, const char* mode);
|
||||
|
||||
// Closes file opened by OpenFile. Returns true on success.
|
||||
bool CloseFile(FILE* file);
|
||||
|
||||
// Reads the given number of bytes from the file into the buffer. Returns
|
||||
// the number of read bytes, or -1 on error.
|
||||
int ReadFile(const FilePath& filename, char* data, int size);
|
||||
// Deprecated temporary compatibility function.
|
||||
int ReadFile(const std::wstring& filename, char* data, int size);
|
||||
|
||||
// Writes the given buffer into the file, overwriting any data that was
|
||||
// previously there. Returns the number of bytes written, or -1 on error.
|
||||
int WriteFile(const FilePath& filename, const char* data, int size);
|
||||
// Deprecated temporary compatibility function.
|
||||
int WriteFile(const std::wstring& filename, const char* data, int size);
|
||||
|
||||
// Gets the current working directory for the process.
|
||||
bool GetCurrentDirectory(FilePath* path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool GetCurrentDirectory(std::wstring* path);
|
||||
|
||||
// Sets the current working directory for the process.
|
||||
bool SetCurrentDirectory(const FilePath& path);
|
||||
// Deprecated temporary compatibility function.
|
||||
bool SetCurrentDirectory(const std::wstring& current_directory);
|
||||
|
||||
} // namespace file_util
|
||||
|
||||
#endif // BASE_FILE_UTIL_H_
|
@ -1,31 +0,0 @@
|
||||
// Copyright (c) 2006-2008 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.
|
||||
|
||||
#include "base/file_util.h"
|
||||
|
||||
#import <Cocoa/Cocoa.h>
|
||||
#include <copyfile.h>
|
||||
|
||||
#include "base/file_path.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/string_util.h"
|
||||
#include "base/scoped_nsautorelease_pool.h"
|
||||
|
||||
namespace file_util {
|
||||
|
||||
bool GetTempDir(FilePath* path) {
|
||||
base::ScopedNSAutoreleasePool autorelease_pool;
|
||||
NSString* tmp = NSTemporaryDirectory();
|
||||
if (tmp == nil) return false;
|
||||
*path = FilePath([tmp fileSystemRepresentation]);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetShmemTempDir(FilePath* path) { return GetTempDir(path); }
|
||||
|
||||
bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
|
||||
return (copyfile(from_path.value().c_str(), to_path.value().c_str(), NULL, COPYFILE_ALL) == 0);
|
||||
}
|
||||
|
||||
} // namespace
|
@ -1,316 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2006-2008 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.
|
||||
|
||||
#include "base/file_util.h"
|
||||
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <fnmatch.h>
|
||||
#include <libgen.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <sys/mman.h>
|
||||
#define _DARWIN_USE_64_BIT_INODE // Use 64-bit inode data structures
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "base/basictypes.h"
|
||||
#include "base/eintr_wrapper.h"
|
||||
#include "base/file_path.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/string_util.h"
|
||||
#include "base/time.h"
|
||||
|
||||
namespace file_util {
|
||||
|
||||
#if defined(GOOGLE_CHROME_BUILD)
|
||||
static const char* kTempFileName = "com.google.chrome.XXXXXX";
|
||||
#else
|
||||
static const char* kTempFileName = "org.chromium.XXXXXX";
|
||||
#endif
|
||||
|
||||
bool AbsolutePath(FilePath* path) {
|
||||
char full_path[PATH_MAX];
|
||||
if (realpath(path->value().c_str(), full_path) == NULL) return false;
|
||||
*path = FilePath(full_path);
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO(erikkay): The Windows version of this accepts paths like "foo/bar/*"
|
||||
// which works both with and without the recursive flag. I'm not sure we need
|
||||
// that functionality. If not, remove from file_util_win.cc, otherwise add it
|
||||
// here.
|
||||
bool Delete(const FilePath& path) {
|
||||
const char* path_str = path.value().c_str();
|
||||
struct stat file_info;
|
||||
int test = stat(path_str, &file_info);
|
||||
if (test != 0) {
|
||||
// The Windows version defines this condition as success.
|
||||
bool ret = (errno == ENOENT || errno == ENOTDIR);
|
||||
return ret;
|
||||
}
|
||||
if (!S_ISDIR(file_info.st_mode)) return (unlink(path_str) == 0);
|
||||
|
||||
return (rmdir(path_str) == 0);
|
||||
}
|
||||
|
||||
bool PathExists(const FilePath& path) {
|
||||
struct stat file_info;
|
||||
return (stat(path.value().c_str(), &file_info) == 0);
|
||||
}
|
||||
|
||||
bool PathIsWritable(const FilePath& path) {
|
||||
FilePath test_path(path);
|
||||
struct stat file_info;
|
||||
if (stat(test_path.value().c_str(), &file_info) != 0) {
|
||||
// If the path doesn't exist, test the parent dir.
|
||||
test_path = test_path.DirName();
|
||||
// If the parent dir doesn't exist, then return false (the path is not
|
||||
// directly writable).
|
||||
if (stat(test_path.value().c_str(), &file_info) != 0) return false;
|
||||
}
|
||||
if (S_IWOTH & file_info.st_mode) return true;
|
||||
if (getegid() == file_info.st_gid && (S_IWGRP & file_info.st_mode))
|
||||
return true;
|
||||
if (geteuid() == file_info.st_uid && (S_IWUSR & file_info.st_mode))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DirectoryExists(const FilePath& path) {
|
||||
struct stat file_info;
|
||||
if (stat(path.value().c_str(), &file_info) == 0)
|
||||
return S_ISDIR(file_info.st_mode);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ReadFromFD(int fd, char* buffer, size_t bytes) {
|
||||
size_t total_read = 0;
|
||||
while (total_read < bytes) {
|
||||
ssize_t bytes_read =
|
||||
HANDLE_EINTR(read(fd, buffer + total_read, bytes - total_read));
|
||||
if (bytes_read <= 0) break;
|
||||
total_read += bytes_read;
|
||||
}
|
||||
return total_read == bytes;
|
||||
}
|
||||
|
||||
// Creates and opens a temporary file in |directory|, returning the
|
||||
// file descriptor. |path| is set to the temporary file path.
|
||||
// Note TODO(erikkay) comment in header for BlahFileName() calls; the
|
||||
// intent is to rename these files BlahFile() (since they create
|
||||
// files, not filenames). This function does NOT unlink() the file.
|
||||
int CreateAndOpenFdForTemporaryFile(FilePath directory, FilePath* path) {
|
||||
*path = directory.Append(kTempFileName);
|
||||
const std::string& tmpdir_string = path->value();
|
||||
// this should be OK since mkstemp just replaces characters in place
|
||||
char* buffer = const_cast<char*>(tmpdir_string.c_str());
|
||||
|
||||
return mkstemp(buffer);
|
||||
}
|
||||
|
||||
bool CreateTemporaryFileName(FilePath* path) {
|
||||
FilePath directory;
|
||||
if (!GetTempDir(&directory)) return false;
|
||||
int fd = CreateAndOpenFdForTemporaryFile(directory, path);
|
||||
if (fd < 0) return false;
|
||||
close(fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) {
|
||||
FilePath directory;
|
||||
if (!GetShmemTempDir(&directory)) return NULL;
|
||||
|
||||
return CreateAndOpenTemporaryFileInDir(directory, path);
|
||||
}
|
||||
|
||||
FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
|
||||
int fd = CreateAndOpenFdForTemporaryFile(dir, path);
|
||||
if (fd < 0) return NULL;
|
||||
|
||||
return fdopen(fd, "a+");
|
||||
}
|
||||
|
||||
bool CreateTemporaryFileNameInDir(const std::wstring& dir,
|
||||
std::wstring* temp_file) {
|
||||
// Not implemented yet.
|
||||
NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
|
||||
bool CreateNewTempDirectory(const FilePath::StringType& prefix,
|
||||
FilePath* new_temp_path) {
|
||||
FilePath tmpdir;
|
||||
if (!GetTempDir(&tmpdir)) return false;
|
||||
tmpdir = tmpdir.Append(kTempFileName);
|
||||
std::string tmpdir_string = tmpdir.value();
|
||||
#ifdef ANDROID
|
||||
char* dtemp = NULL;
|
||||
#else
|
||||
// this should be OK since mkdtemp just replaces characters in place
|
||||
char* buffer = const_cast<char*>(tmpdir_string.c_str());
|
||||
char* dtemp = mkdtemp(buffer);
|
||||
#endif
|
||||
if (!dtemp) return false;
|
||||
*new_temp_path = FilePath(dtemp);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateDirectory(const FilePath& full_path) {
|
||||
std::vector<FilePath> subpaths;
|
||||
|
||||
// Collect a list of all parent directories.
|
||||
FilePath last_path = full_path;
|
||||
subpaths.push_back(full_path);
|
||||
for (FilePath path = full_path.DirName(); path.value() != last_path.value();
|
||||
path = path.DirName()) {
|
||||
subpaths.push_back(path);
|
||||
last_path = path;
|
||||
}
|
||||
|
||||
// Iterate through the parents and create the missing ones.
|
||||
for (std::vector<FilePath>::reverse_iterator i = subpaths.rbegin();
|
||||
i != subpaths.rend(); ++i) {
|
||||
if (!DirectoryExists(*i)) {
|
||||
if (mkdir(i->value().c_str(), 0777) != 0) return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetFileInfo(const FilePath& file_path, FileInfo* results) {
|
||||
struct stat file_info;
|
||||
if (stat(file_path.value().c_str(), &file_info) != 0) return false;
|
||||
results->is_directory = S_ISDIR(file_info.st_mode);
|
||||
results->size = file_info.st_size;
|
||||
return true;
|
||||
}
|
||||
|
||||
FILE* OpenFile(const std::string& filename, const char* mode) {
|
||||
return OpenFile(FilePath(filename), mode);
|
||||
}
|
||||
|
||||
FILE* OpenFile(const FilePath& filename, const char* mode) {
|
||||
return fopen(filename.value().c_str(), mode);
|
||||
}
|
||||
|
||||
int ReadFile(const FilePath& filename, char* data, int size) {
|
||||
int fd = open(filename.value().c_str(), O_RDONLY);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
int ret_value = HANDLE_EINTR(read(fd, data, size));
|
||||
IGNORE_EINTR(close(fd));
|
||||
return ret_value;
|
||||
}
|
||||
|
||||
int WriteFile(const FilePath& filename, const char* data, int size) {
|
||||
int fd = creat(filename.value().c_str(), 0666);
|
||||
if (fd < 0) return -1;
|
||||
|
||||
// Allow for partial writes
|
||||
ssize_t bytes_written_total = 0;
|
||||
do {
|
||||
ssize_t bytes_written_partial = HANDLE_EINTR(
|
||||
write(fd, data + bytes_written_total, size - bytes_written_total));
|
||||
if (bytes_written_partial < 0) {
|
||||
IGNORE_EINTR(close(fd));
|
||||
return -1;
|
||||
}
|
||||
bytes_written_total += bytes_written_partial;
|
||||
} while (bytes_written_total < size);
|
||||
|
||||
IGNORE_EINTR(close(fd));
|
||||
return bytes_written_total;
|
||||
}
|
||||
|
||||
// Gets the current working directory for the process.
|
||||
bool GetCurrentDirectory(FilePath* dir) {
|
||||
char system_buffer[PATH_MAX] = "";
|
||||
if (!getcwd(system_buffer, sizeof(system_buffer))) {
|
||||
NOTREACHED();
|
||||
return false;
|
||||
}
|
||||
*dir = FilePath(system_buffer);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Sets the current working directory for the process.
|
||||
bool SetCurrentDirectory(const FilePath& path) {
|
||||
int ret = chdir(path.value().c_str());
|
||||
return !ret;
|
||||
}
|
||||
|
||||
#if !defined(OS_MACOSX)
|
||||
bool GetTempDir(FilePath* path) {
|
||||
const char* tmp = getenv("TMPDIR");
|
||||
if (tmp)
|
||||
*path = FilePath(tmp);
|
||||
else
|
||||
*path = FilePath("/tmp");
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetShmemTempDir(FilePath* path) {
|
||||
# if defined(OS_LINUX) && !defined(ANDROID)
|
||||
*path = FilePath("/dev/shm");
|
||||
return true;
|
||||
# else
|
||||
return GetTempDir(path);
|
||||
# endif
|
||||
}
|
||||
|
||||
bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
|
||||
int infile = open(from_path.value().c_str(), O_RDONLY);
|
||||
if (infile < 0) return false;
|
||||
|
||||
int outfile = creat(to_path.value().c_str(), 0666);
|
||||
if (outfile < 0) {
|
||||
close(infile);
|
||||
return false;
|
||||
}
|
||||
|
||||
const size_t kBufferSize = 32768;
|
||||
std::vector<char> buffer(kBufferSize);
|
||||
bool result = true;
|
||||
|
||||
while (result) {
|
||||
ssize_t bytes_read = HANDLE_EINTR(read(infile, &buffer[0], buffer.size()));
|
||||
if (bytes_read < 0) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
if (bytes_read == 0) break;
|
||||
// Allow for partial writes
|
||||
ssize_t bytes_written_per_read = 0;
|
||||
do {
|
||||
ssize_t bytes_written_partial =
|
||||
HANDLE_EINTR(write(outfile, &buffer[bytes_written_per_read],
|
||||
bytes_read - bytes_written_per_read));
|
||||
if (bytes_written_partial < 0) {
|
||||
result = false;
|
||||
break;
|
||||
}
|
||||
bytes_written_per_read += bytes_written_partial;
|
||||
} while (bytes_written_per_read < bytes_read);
|
||||
}
|
||||
|
||||
if (IGNORE_EINTR(close(infile)) < 0) result = false;
|
||||
if (IGNORE_EINTR(close(outfile)) < 0) result = false;
|
||||
|
||||
return result;
|
||||
}
|
||||
#endif // !defined(OS_MACOSX)
|
||||
|
||||
} // namespace file_util
|
@ -1,330 +0,0 @@
|
||||
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
||||
// Copyright (c) 2006-2008 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.
|
||||
|
||||
#include "base/file_util.h"
|
||||
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
#include <shlobj.h>
|
||||
#include <time.h>
|
||||
#include <string>
|
||||
|
||||
#include "base/file_path.h"
|
||||
#include "base/logging.h"
|
||||
#include "base/scoped_handle.h"
|
||||
#include "base/string_util.h"
|
||||
#include "base/time.h"
|
||||
#include "base/win_util.h"
|
||||
|
||||
namespace file_util {
|
||||
|
||||
bool AbsolutePath(FilePath* path) {
|
||||
wchar_t file_path_buf[MAX_PATH];
|
||||
if (!_wfullpath(file_path_buf, path->value().c_str(), MAX_PATH)) return false;
|
||||
*path = FilePath(file_path_buf);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Delete(const FilePath& path) {
|
||||
if (path.value().length() >= MAX_PATH) return false;
|
||||
|
||||
// Use DeleteFile; it should be faster. DeleteFile
|
||||
// fails if passed a directory though, which is why we fall through on
|
||||
// failure to the SHFileOperation.
|
||||
if (DeleteFile(path.value().c_str()) != 0) return true;
|
||||
|
||||
// SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
|
||||
// so we have to use wcscpy because wcscpy_s writes non-NULLs
|
||||
// into the rest of the buffer.
|
||||
wchar_t double_terminated_path[MAX_PATH + 1] = {0};
|
||||
#pragma warning(suppress : 4996) // don't complain about wcscpy deprecation
|
||||
wcscpy(double_terminated_path, path.value().c_str());
|
||||
|
||||
SHFILEOPSTRUCT file_operation = {0};
|
||||
file_operation.wFunc = FO_DELETE;
|
||||
file_operation.pFrom = double_terminated_path;
|
||||
file_operation.fFlags = FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION;
|
||||
file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY;
|
||||
int err = SHFileOperation(&file_operation);
|
||||
// Some versions of Windows return ERROR_FILE_NOT_FOUND when
|
||||
// deleting an empty directory.
|
||||
return (err == 0 || err == ERROR_FILE_NOT_FOUND);
|
||||
}
|
||||
|
||||
bool CopyFile(const FilePath& from_path, const FilePath& to_path) {
|
||||
// NOTE: I suspect we could support longer paths, but that would involve
|
||||
// analyzing all our usage of files.
|
||||
if (from_path.value().length() >= MAX_PATH ||
|
||||
to_path.value().length() >= MAX_PATH) {
|
||||
return false;
|
||||
}
|
||||
return (::CopyFile(from_path.value().c_str(), to_path.value().c_str(),
|
||||
false) != 0);
|
||||
}
|
||||
|
||||
bool ShellCopy(const FilePath& from_path, const FilePath& to_path,
|
||||
bool recursive) {
|
||||
// NOTE: I suspect we could support longer paths, but that would involve
|
||||
// analyzing all our usage of files.
|
||||
if (from_path.value().length() >= MAX_PATH ||
|
||||
to_path.value().length() >= MAX_PATH) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// SHFILEOPSTRUCT wants the path to be terminated with two NULLs,
|
||||
// so we have to use wcscpy because wcscpy_s writes non-NULLs
|
||||
// into the rest of the buffer.
|
||||
wchar_t double_terminated_path_from[MAX_PATH + 1] = {0};
|
||||
wchar_t double_terminated_path_to[MAX_PATH + 1] = {0};
|
||||
#pragma warning(suppress : 4996) // don't complain about wcscpy deprecation
|
||||
wcscpy(double_terminated_path_from, from_path.value().c_str());
|
||||
#pragma warning(suppress : 4996) // don't complain about wcscpy deprecation
|
||||
wcscpy(double_terminated_path_to, to_path.value().c_str());
|
||||
|
||||
SHFILEOPSTRUCT file_operation = {0};
|
||||
file_operation.wFunc = FO_COPY;
|
||||
file_operation.pFrom = double_terminated_path_from;
|
||||
file_operation.pTo = double_terminated_path_to;
|
||||
file_operation.fFlags =
|
||||
FOF_NOERRORUI | FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
|
||||
if (!recursive) file_operation.fFlags |= FOF_NORECURSION | FOF_FILESONLY;
|
||||
|
||||
return (SHFileOperation(&file_operation) == 0);
|
||||
}
|
||||
|
||||
bool PathExists(const FilePath& path) {
|
||||
return (GetFileAttributes(path.value().c_str()) != INVALID_FILE_ATTRIBUTES);
|
||||
}
|
||||
|
||||
bool PathIsWritable(const FilePath& path) {
|
||||
HANDLE dir =
|
||||
CreateFile(path.value().c_str(), FILE_ADD_FILE,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, NULL,
|
||||
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
|
||||
|
||||
if (dir == INVALID_HANDLE_VALUE) return false;
|
||||
|
||||
CloseHandle(dir);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool DirectoryExists(const FilePath& path) {
|
||||
DWORD fileattr = GetFileAttributes(path.value().c_str());
|
||||
if (fileattr != INVALID_FILE_ATTRIBUTES)
|
||||
return (fileattr & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool GetTempDir(FilePath* path) {
|
||||
wchar_t temp_path[MAX_PATH + 1];
|
||||
DWORD path_len = ::GetTempPath(MAX_PATH, temp_path);
|
||||
if (path_len >= MAX_PATH || path_len <= 0) return false;
|
||||
// TODO(evanm): the old behavior of this function was to always strip the
|
||||
// trailing slash. We duplicate this here, but it shouldn't be necessary
|
||||
// when everyone is using the appropriate FilePath APIs.
|
||||
std::wstring path_str(temp_path);
|
||||
TrimTrailingSeparator(&path_str);
|
||||
*path = FilePath(path_str);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool GetShmemTempDir(FilePath* path) { return GetTempDir(path); }
|
||||
|
||||
bool CreateTemporaryFileName(FilePath* path) {
|
||||
std::wstring temp_path, temp_file;
|
||||
|
||||
if (!GetTempDir(&temp_path)) return false;
|
||||
|
||||
if (CreateTemporaryFileNameInDir(temp_path, &temp_file)) {
|
||||
*path = FilePath(temp_file);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
FILE* CreateAndOpenTemporaryShmemFile(FilePath* path) {
|
||||
return CreateAndOpenTemporaryFile(path);
|
||||
}
|
||||
|
||||
// On POSIX we have semantics to create and open a temporary file
|
||||
// atomically.
|
||||
// TODO(jrg): is there equivalent call to use on Windows instead of
|
||||
// going 2-step?
|
||||
FILE* CreateAndOpenTemporaryFileInDir(const FilePath& dir, FilePath* path) {
|
||||
std::wstring wstring_path;
|
||||
if (!CreateTemporaryFileNameInDir(dir.value(), &wstring_path)) {
|
||||
return NULL;
|
||||
}
|
||||
*path = FilePath(wstring_path);
|
||||
// Open file in binary mode, to avoid problems with fwrite. On Windows
|
||||
// it replaces \n's with \r\n's, which may surprise you.
|
||||
// Reference: http://msdn.microsoft.com/en-us/library/h9t88zwz(VS.71).aspx
|
||||
return OpenFile(*path, "wb+");
|
||||
}
|
||||
|
||||
bool CreateTemporaryFileNameInDir(const std::wstring& dir,
|
||||
std::wstring* temp_file) {
|
||||
wchar_t temp_name[MAX_PATH + 1];
|
||||
|
||||
if (!GetTempFileName(dir.c_str(), L"", 0, temp_name)) return false; // fail!
|
||||
|
||||
DWORD path_len = GetLongPathName(temp_name, temp_name, MAX_PATH);
|
||||
if (path_len > MAX_PATH + 1 || path_len == 0) return false; // fail!
|
||||
|
||||
temp_file->assign(temp_name, path_len);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateNewTempDirectory(const FilePath::StringType& prefix,
|
||||
FilePath* new_temp_path) {
|
||||
FilePath system_temp_dir;
|
||||
if (!GetTempDir(&system_temp_dir)) return false;
|
||||
|
||||
FilePath path_to_create;
|
||||
srand(static_cast<uint32_t>(time(NULL)));
|
||||
|
||||
int count = 0;
|
||||
while (count < 50) {
|
||||
// Try create a new temporary directory with random generated name. If
|
||||
// the one exists, keep trying another path name until we reach some limit.
|
||||
path_to_create = system_temp_dir;
|
||||
std::wstring new_dir_name;
|
||||
new_dir_name.assign(prefix);
|
||||
new_dir_name.append(IntToWString(rand() % kint16max));
|
||||
path_to_create = path_to_create.Append(new_dir_name);
|
||||
|
||||
if (::CreateDirectory(path_to_create.value().c_str(), NULL)) break;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count == 50) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*new_temp_path = path_to_create;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool CreateDirectory(const FilePath& full_path) {
|
||||
if (DirectoryExists(full_path)) return true;
|
||||
int err = SHCreateDirectoryEx(NULL, full_path.value().c_str(), NULL);
|
||||
return err == ERROR_SUCCESS;
|
||||
}
|
||||
|
||||
bool GetFileInfo(const FilePath& file_path, FileInfo* results) {
|
||||
WIN32_FILE_ATTRIBUTE_DATA attr;
|
||||
if (!GetFileAttributesEx(file_path.ToWStringHack().c_str(),
|
||||
GetFileExInfoStandard, &attr)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ULARGE_INTEGER size;
|
||||
size.HighPart = attr.nFileSizeHigh;
|
||||
size.LowPart = attr.nFileSizeLow;
|
||||
results->size = size.QuadPart;
|
||||
|
||||
results->is_directory =
|
||||
(attr.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
FILE* OpenFile(const FilePath& filename, const char* mode) {
|
||||
std::wstring w_mode = ASCIIToWide(std::string(mode));
|
||||
FILE* file;
|
||||
if (_wfopen_s(&file, filename.value().c_str(), w_mode.c_str()) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
FILE* OpenFile(const std::string& filename, const char* mode) {
|
||||
FILE* file;
|
||||
if (fopen_s(&file, filename.c_str(), mode) != 0) {
|
||||
return NULL;
|
||||
}
|
||||
return file;
|
||||
}
|
||||
|
||||
int ReadFile(const FilePath& filename, char* data, int size) {
|
||||
ScopedHandle file(CreateFile(filename.value().c_str(), GENERIC_READ,
|
||||
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
|
||||
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL));
|
||||
if (file == INVALID_HANDLE_VALUE) return -1;
|
||||
|
||||
int ret_value;
|
||||
DWORD read;
|
||||
if (::ReadFile(file, data, size, &read, NULL) && read == size) {
|
||||
ret_value = static_cast<int>(read);
|
||||
} else {
|
||||
ret_value = -1;
|
||||
}
|
||||
|
||||
return ret_value;
|
||||
}
|
||||
|
||||
int WriteFile(const FilePath& filename, const char* data, int size) {
|
||||
ScopedHandle file(CreateFile(filename.value().c_str(), GENERIC_WRITE, 0, NULL,
|
||||
CREATE_ALWAYS, 0, NULL));
|
||||
if (file == INVALID_HANDLE_VALUE) {
|
||||
CHROMIUM_LOG(WARNING) << "CreateFile failed for path " << filename.value()
|
||||
<< " error code=" << GetLastError()
|
||||
<< " error text=" << win_util::FormatLastWin32Error();
|
||||
return -1;
|
||||
}
|
||||
|
||||
DWORD written;
|
||||
BOOL result = ::WriteFile(file, data, size, &written, NULL);
|
||||
if (result && written == size) return static_cast<int>(written);
|
||||
|
||||
if (!result) {
|
||||
// WriteFile failed.
|
||||
CHROMIUM_LOG(WARNING) << "writing file " << filename.value()
|
||||
<< " failed, error code=" << GetLastError()
|
||||
<< " description="
|
||||
<< win_util::FormatLastWin32Error();
|
||||
} else {
|
||||
// Didn't write all the bytes.
|
||||
CHROMIUM_LOG(WARNING) << "wrote" << written << " bytes to "
|
||||
<< filename.value() << " expected " << size;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// Gets the current working directory for the process.
|
||||
bool GetCurrentDirectory(FilePath* dir) {
|
||||
wchar_t system_buffer[MAX_PATH];
|
||||
system_buffer[0] = 0;
|
||||
DWORD len = ::GetCurrentDirectory(MAX_PATH, system_buffer);
|
||||
if (len == 0 || len > MAX_PATH) return false;
|
||||
// TODO(evanm): the old behavior of this function was to always strip the
|
||||
// trailing slash. We duplicate this here, but it shouldn't be necessary
|
||||
// when everyone is using the appropriate FilePath APIs.
|
||||
std::wstring dir_str(system_buffer);
|
||||
file_util::TrimTrailingSeparator(&dir_str);
|
||||
*dir = FilePath(dir_str);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Sets the current working directory for the process.
|
||||
bool SetCurrentDirectory(const FilePath& directory) {
|
||||
BOOL ret = ::SetCurrentDirectory(directory.value().c_str());
|
||||
return ret != 0;
|
||||
}
|
||||
|
||||
// Deprecated functions ----------------------------------------------------
|
||||
|
||||
void InsertBeforeExtension(std::wstring* path_str, const std::wstring& suffix) {
|
||||
FilePath path(*path_str);
|
||||
InsertBeforeExtension(&path, suffix);
|
||||
path_str->assign(path.value());
|
||||
}
|
||||
void ReplaceExtension(std::wstring* file_name, const std::wstring& extension) {
|
||||
FilePath path(*file_name);
|
||||
ReplaceExtension(&path, extension);
|
||||
file_name->assign(path.value());
|
||||
}
|
||||
} // namespace file_util
|
Loading…
Reference in New Issue
Block a user