diff options
author | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-27 17:13:02 +0000 |
---|---|---|
committer | jeremy@chromium.org <jeremy@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-01-27 17:13:02 +0000 |
commit | 6e01dae641ad11e7600dec30de45b506b8d21c0c (patch) | |
tree | bc27f693251a8e602fbdf65c50fd4379cd48959c /base | |
parent | 25fa78c2ffe3e98231cfb22f4005452da85b201c (diff) | |
download | chromium_src-6e01dae641ad11e7600dec30de45b506b8d21c0c.zip chromium_src-6e01dae641ad11e7600dec30de45b506b8d21c0c.tar.gz chromium_src-6e01dae641ad11e7600dec30de45b506b8d21c0c.tar.bz2 |
First step of porting VisitedLinkMaster to POSIX:
* Use POSIX file access APIs rather than HANDLEs.
* Add stubs so that VisitedLinkMaster compiles on POSIX.
Still to be done:
* Bring up Surrounding infrastructure to turn on unit tests.
Review URL: http://codereview.chromium.org/18530
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@8721 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.xcodeproj/project.pbxproj | 2 | ||||
-rw-r--r-- | base/file_util.cc | 18 | ||||
-rw-r--r-- | base/file_util.h | 17 |
3 files changed, 37 insertions, 0 deletions
diff --git a/base/base.xcodeproj/project.pbxproj b/base/base.xcodeproj/project.pbxproj index e347a84..5c9ab7a 100644 --- a/base/base.xcodeproj/project.pbxproj +++ b/base/base.xcodeproj/project.pbxproj @@ -644,6 +644,7 @@ ABF68B280EB0F93100E72835 /* field_trial.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = field_trial.cc; sourceTree = "<group>"; }; B290BFCBD30E45A63758BFC7 /* waitable_event_posix.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = waitable_event_posix.cc; sourceTree = "<group>"; }; B52C916B0E9428F500208D01 /* clipboard_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clipboard_unittest.cc; sourceTree = "<group>"; }; + B57D788E0F26983200685566 /* scoped_file.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = scoped_file.h; sourceTree = "<group>"; }; B57E4D770E9C26340090055D /* idletimer_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = idletimer_unittest.cc; sourceTree = "<group>"; }; B5A8618D0EC1257900B332C2 /* clipboard.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = clipboard.cc; sourceTree = "<group>"; }; B5D544AA0EAFB7E000272A1C /* sys_string_conversions_unittest.cc */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = sys_string_conversions_unittest.cc; sourceTree = "<group>"; }; @@ -1002,6 +1003,7 @@ 7B5AD60D0D9DD8050012BCF1 /* scoped_cftyperef.h */, B5E8F6CB0EBFB38E008DD1E9 /* scoped_clipboard_writer.cc */, B5E8F6CA0EBFB38E008DD1E9 /* scoped_clipboard_writer.h */, + B57D788E0F26983200685566 /* scoped_file.h */, 7BA35DD10E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.h */, 7BA35DD20E8C0D5F0023C8B9 /* scoped_nsautorelease_pool.mm */, 825403610D92D27C0006B936 /* scoped_ptr.h */, diff --git a/base/file_util.cc b/base/file_util.cc index 078e3afa..f21ff07 100644 --- a/base/file_util.cc +++ b/base/file_util.cc @@ -276,6 +276,24 @@ bool CloseFile(FILE* file) { return fclose(file) == 0; } +bool TruncateFile(FILE* file) { + if (file == NULL) + return false; + long current_offset = ftell(file); + if (current_offset == -1) + return false; +#if defined(OS_WIN) + int fd = _fileno(file); + if (_chsize(fd, current_offset) != 0) + return false; +#else + int fd = fileno(file); + if (ftruncate(fd, current_offset) != 0) + return false; +#endif + return true; +} + bool ContainsPath(const FilePath &parent, const FilePath& child) { FilePath abs_parent = FilePath(parent); FilePath abs_child = FilePath(child); diff --git a/base/file_util.h b/base/file_util.h index 4154856..c546b9a 100644 --- a/base/file_util.h +++ b/base/file_util.h @@ -24,6 +24,7 @@ #include <vector> #include "base/basictypes.h" +#include "base/scoped_ptr.h" #include "base/file_path.h" namespace file_util { @@ -318,6 +319,10 @@ FILE* OpenFile(const std::wstring& filename, const char* mode); // Closes file opened by OpenFile. Returns true on success. bool CloseFile(FILE* file); +// Truncates an open file to end at the location of the current file pointer. +// This is a cross-platform analog to Windows' SetEndOfFile() function. +bool TruncateFile(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 std::wstring& filename, char* data, int size); @@ -336,6 +341,18 @@ bool SetCurrentDirectory(const FilePath& path); // Deprecated temporary compatibility function. bool SetCurrentDirectory(const std::wstring& current_directory); +// A class to handle auto-closing of FILE*'s. +class ScopedFILEClose { + public: + inline void operator()(FILE* x) const { + if (x) { + fclose(x); + } + } +}; + +typedef scoped_ptr_malloc<FILE, ScopedFILEClose> ScopedFILE; + // A class for enumerating the files in a provided path. The order of the // results is not guaranteed. // |