summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/file_util.cc22
-rw-r--r--base/file_util.h9
-rw-r--r--base/file_util_posix.cc7
-rw-r--r--base/file_util_unittest.cc16
-rw-r--r--base/file_util_win.cc9
-rw-r--r--base/platform_file_posix.cc3
-rw-r--r--base/platform_file_win.cc4
7 files changed, 24 insertions, 46 deletions
diff --git a/base/file_util.cc b/base/file_util.cc
index 747232b..e618903 100644
--- a/base/file_util.cc
+++ b/base/file_util.cc
@@ -200,28 +200,6 @@ bool IsDotDot(const FilePath& path) {
return FILE_PATH_LITERAL("..") == path.BaseName().value();
}
-bool TouchFile(const FilePath& path,
- const base::Time& last_accessed,
- const base::Time& last_modified) {
- base::PlatformFile file =
- base::CreatePlatformFile(path,
- base::PLATFORM_FILE_OPEN |
- base::PLATFORM_FILE_WRITE_ATTRIBUTES,
- NULL, NULL);
- if (file != base::kInvalidPlatformFileValue) {
- bool result = base::TouchPlatformFile(file, last_accessed, last_modified);
- base::ClosePlatformFile(file);
- return result;
- }
-
- return false;
-}
-
-bool SetLastModifiedTime(const FilePath& path,
- const base::Time& last_modified) {
- return TouchFile(path, last_modified, last_modified);
-}
-
bool CloseFile(FILE* file) {
if (file == NULL)
return true;
diff --git a/base/file_util.h b/base/file_util.h
index 7825d5b..415a192 100644
--- a/base/file_util.h
+++ b/base/file_util.h
@@ -31,6 +31,7 @@
#include "base/platform_file.h"
#include "base/scoped_ptr.h"
#include "base/string16.h"
+#include "base/time.h"
#if defined(OS_POSIX)
#include "base/eintr_wrapper.h"
@@ -317,14 +318,8 @@ bool NormalizeToNativeFilePath(const FilePath& path, FilePath* nt_path);
// Returns information about the given file path.
bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* info);
-// Sets the time of the last access and the time of the last modification.
-bool TouchFile(const FilePath& path,
- const base::Time& last_accessed,
- const base::Time& last_modified);
-
// Set the time of the last modification. Useful for unit tests.
-bool SetLastModifiedTime(const FilePath& path,
- const base::Time& last_modified);
+bool SetLastModifiedTime(const FilePath& file_path, base::Time last_modified);
#if defined(OS_POSIX)
// Store inode number of |path| in |inode|. Return true on success.
diff --git a/base/file_util_posix.cc b/base/file_util_posix.cc
index 3709afd..99e15fe 100644
--- a/base/file_util_posix.cc
+++ b/base/file_util_posix.cc
@@ -486,6 +486,13 @@ bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) {
return true;
}
+bool SetLastModifiedTime(const FilePath& file_path, base::Time last_modified) {
+ struct timeval times[2];
+ times[0] = last_modified.ToTimeVal();
+ times[1] = last_modified.ToTimeVal();
+ return (utimes(file_path.value().c_str(), times) == 0);
+}
+
bool GetInode(const FilePath& path, ino_t* inode) {
struct stat buffer;
int result = stat(path.value().c_str(), &buffer);
diff --git a/base/file_util_unittest.cc b/base/file_util_unittest.cc
index ca36b6a..068e5e50 100644
--- a/base/file_util_unittest.cc
+++ b/base/file_util_unittest.cc
@@ -1753,7 +1753,7 @@ TEST_F(FileUtilTest, Contains) {
#endif
}
-TEST_F(FileUtilTest, TouchFile) {
+TEST_F(FileUtilTest, LastModified) {
FilePath data_dir =
temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest"));
@@ -1767,25 +1767,15 @@ TEST_F(FileUtilTest, TouchFile) {
std::string data("hello");
ASSERT_TRUE(file_util::WriteFile(foobar, data.c_str(), data.length()));
- base::Time access_time;
- // This timestamp is divisible by one day (in local timezone),
- // to make it work on FAT too.
- ASSERT_TRUE(base::Time::FromString(L"Wed, 16 Nov 1994, 00:00:00",
- &access_time));
-
base::Time modification_time;
// Note that this timestamp is divisible by two (seconds) - FAT stores
// modification times with 2s resolution.
ASSERT_TRUE(base::Time::FromString(L"Tue, 15 Nov 1994, 12:45:26 GMT",
&modification_time));
-
- ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time));
+ ASSERT_TRUE(file_util::SetLastModifiedTime(foobar, modification_time));
base::PlatformFileInfo file_info;
ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info));
- EXPECT_EQ(file_info.last_accessed.ToInternalValue(),
- access_time.ToInternalValue());
- EXPECT_EQ(file_info.last_modified.ToInternalValue(),
- modification_time.ToInternalValue());
+ ASSERT_TRUE(file_info.last_modified == modification_time);
}
TEST_F(FileUtilTest, IsDirectoryEmpty) {
diff --git a/base/file_util_win.cc b/base/file_util_win.cc
index b3ce896..f538c20 100644
--- a/base/file_util_win.cc
+++ b/base/file_util_win.cc
@@ -696,6 +696,15 @@ bool GetFileInfo(const FilePath& file_path, base::PlatformFileInfo* results) {
return true;
}
+bool SetLastModifiedTime(const FilePath& file_path, base::Time last_modified) {
+ FILETIME timestamp(last_modified.ToFileTime());
+ ScopedHandle file_handle(
+ CreateFile(file_path.value().c_str(), FILE_WRITE_ATTRIBUTES,
+ kFileShareAll, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL));
+ BOOL ret = SetFileTime(file_handle.Get(), NULL, &timestamp, &timestamp);
+ return ret != 0;
+}
+
FILE* OpenFile(const FilePath& filename, const char* mode) {
std::wstring w_mode = ASCIIToWide(std::string(mode));
return _wfsopen(filename.value().c_str(), w_mode.c_str(), _SH_DENYNO);
diff --git a/base/platform_file_posix.cc b/base/platform_file_posix.cc
index 9b1ce17..1d8ef4c 100644
--- a/base/platform_file_posix.cc
+++ b/base/platform_file_posix.cc
@@ -52,8 +52,7 @@ PlatformFile CreatePlatformFile(const FilePath& name, int flags,
if (flags & PLATFORM_FILE_WRITE && flags & PLATFORM_FILE_READ) {
open_flags |= O_RDWR;
- } else if (flags & PLATFORM_FILE_WRITE ||
- flags & PLATFORM_FILE_WRITE_ATTRIBUTES) {
+ } else if (flags & PLATFORM_FILE_WRITE) {
open_flags |= O_WRONLY;
} else if (!(flags & PLATFORM_FILE_READ)) {
NOTREACHED();
diff --git a/base/platform_file_win.cc b/base/platform_file_win.cc
index 63dfef9..5fd7892 100644
--- a/base/platform_file_win.cc
+++ b/base/platform_file_win.cc
@@ -47,8 +47,6 @@ PlatformFile CreatePlatformFile(const FilePath& name,
DWORD access = (flags & PLATFORM_FILE_READ) ? GENERIC_READ : 0;
if (flags & PLATFORM_FILE_WRITE)
access |= GENERIC_WRITE;
- if (flags & PLATFORM_FILE_WRITE_ATTRIBUTES)
- access |= FILE_WRITE_ATTRIBUTES;
DWORD sharing = (flags & PLATFORM_FILE_EXCLUSIVE_READ) ? 0 : FILE_SHARE_READ;
if (!(flags & PLATFORM_FILE_EXCLUSIVE_WRITE))
@@ -63,6 +61,8 @@ PlatformFile CreatePlatformFile(const FilePath& name,
create_flags |= FILE_ATTRIBUTE_HIDDEN;
if (flags & PLATFORM_FILE_DELETE_ON_CLOSE)
create_flags |= FILE_FLAG_DELETE_ON_CLOSE;
+ if (flags & PLATFORM_FILE_WRITE_ATTRIBUTES)
+ create_flags |= FILE_WRITE_ATTRIBUTES;
HANDLE file = CreateFile(name.value().c_str(), access, sharing, NULL,
disposition, create_flags, NULL);