diff options
-rw-r--r-- | base/file_util.cc | 22 | ||||
-rw-r--r-- | base/file_util.h | 9 | ||||
-rw-r--r-- | base/file_util_posix.cc | 7 | ||||
-rw-r--r-- | base/file_util_unittest.cc | 16 | ||||
-rw-r--r-- | base/file_util_win.cc | 9 | ||||
-rw-r--r-- | base/platform_file_posix.cc | 3 | ||||
-rw-r--r-- | base/platform_file_win.cc | 4 |
7 files changed, 46 insertions, 24 deletions
diff --git a/base/file_util.cc b/base/file_util.cc index e618903..747232b 100644 --- a/base/file_util.cc +++ b/base/file_util.cc @@ -200,6 +200,28 @@ 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 415a192..7825d5b 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -31,7 +31,6 @@ #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" @@ -318,8 +317,14 @@ 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& file_path, base::Time last_modified); +bool SetLastModifiedTime(const FilePath& path, + const 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 99e15fe..3709afd 100644 --- a/base/file_util_posix.cc +++ b/base/file_util_posix.cc @@ -486,13 +486,6 @@ 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 068e5e50..ca36b6a 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, LastModified) { +TEST_F(FileUtilTest, TouchFile) { FilePath data_dir = temp_dir_.path().Append(FILE_PATH_LITERAL("FilePathTest")); @@ -1767,15 +1767,25 @@ TEST_F(FileUtilTest, LastModified) { 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::SetLastModifiedTime(foobar, modification_time)); + + ASSERT_TRUE(file_util::TouchFile(foobar, access_time, modification_time)); base::PlatformFileInfo file_info; ASSERT_TRUE(file_util::GetFileInfo(foobar, &file_info)); - ASSERT_TRUE(file_info.last_modified == modification_time); + EXPECT_EQ(file_info.last_accessed.ToInternalValue(), + access_time.ToInternalValue()); + EXPECT_EQ(file_info.last_modified.ToInternalValue(), + modification_time.ToInternalValue()); } TEST_F(FileUtilTest, IsDirectoryEmpty) { diff --git a/base/file_util_win.cc b/base/file_util_win.cc index f538c20..b3ce896 100644 --- a/base/file_util_win.cc +++ b/base/file_util_win.cc @@ -696,15 +696,6 @@ 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, ×tamp, ×tamp); - 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 1d8ef4c..9b1ce17 100644 --- a/base/platform_file_posix.cc +++ b/base/platform_file_posix.cc @@ -52,7 +52,8 @@ 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) { + } else if (flags & PLATFORM_FILE_WRITE || + flags & PLATFORM_FILE_WRITE_ATTRIBUTES) { 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 5fd7892..63dfef9 100644 --- a/base/platform_file_win.cc +++ b/base/platform_file_win.cc @@ -47,6 +47,8 @@ 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)) @@ -61,8 +63,6 @@ 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); |