Changeset 57182 in webkit


Ignore:
Timestamp:
Apr 6, 2010 6:29:04 PM (14 years ago)
Author:
eric@webkit.org
Message:

2010-04-06 Kinuko Yasuda <kinuko@chromium.org>

Reviewed by Jian Li.

Add basic FileSystem operations for FileReader/FileWriter support for POSIX (incl. Mac)
https://bugs.webkit.org/show_bug.cgi?id=36938

No new tests; will be added when we implement upper layers.

  • platform/FileSystem.h: (WebCore::):
  • platform/posix/FileSystemPOSIX.cpp: (WebCore::openFile): (WebCore::closeFile): (WebCore::seekFile): (WebCore::truncateFile): (WebCore::writeToFile): (WebCore::readFromFile):
Location:
trunk/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r57180 r57182  
     12010-04-06  Kinuko Yasuda  <kinuko@chromium.org>
     2
     3        Reviewed by Jian Li.
     4
     5        Add basic FileSystem operations for FileReader/FileWriter support for POSIX (incl. Mac)
     6        https://bugs.webkit.org/show_bug.cgi?id=36938
     7
     8        No new tests; will be added when we implement upper layers.
     9
     10        * platform/FileSystem.h:
     11        (WebCore::):
     12        * platform/posix/FileSystemPOSIX.cpp:
     13        (WebCore::openFile):
     14        (WebCore::closeFile):
     15        (WebCore::seekFile):
     16        (WebCore::truncateFile):
     17        (WebCore::writeToFile):
     18        (WebCore::readFromFile):
     19
    1202010-04-06  Nicolas Weber  <thakis@chromium.org>
    221
  • trunk/WebCore/platform/FileSystem.h

    r56825 r57182  
    2727 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    2828 */
    29  
     29
    3030#ifndef FileSystem_h
    3131#define FileSystem_h
     
    123123#endif
    124124
     125enum FileOpenMode {
     126    OpenForRead = 0,
     127    OpenForWrite
     128};
     129
     130enum FileSeekOrigin {
     131    SeekFromBeginning = 0,
     132    SeekFromCurrent,
     133    SeekFromEnd
     134};
     135
    125136bool fileExists(const String&);
    126137bool deleteFile(const String&);
     
    142153// Prefix is what the filename should be prefixed with, not the full path.
    143154WTF::CString openTemporaryFile(const char* prefix, PlatformFileHandle&);
     155PlatformFileHandle openFile(const String& path, FileOpenMode);
    144156void closeFile(PlatformFileHandle&);
     157// Returns the resulting offset from the beginning of the file if successful, -1 otherwise.
     158long long seekFile(PlatformFileHandle, long long offset, FileSeekOrigin);
     159bool truncateFile(PlatformFileHandle, long long offset);
     160// Returns number of bytes actually read if successful, -1 otherwise.
    145161int writeToFile(PlatformFileHandle, const char* data, int length);
     162// Returns number of bytes actually written if successful, -1 otherwise.
     163int readFromFile(PlatformFileHandle, char* data, int length);
    146164
    147165// Methods for dealing with loadable modules
  • trunk/WebCore/platform/posix/FileSystemPOSIX.cpp

    r56825 r57182  
    3131
    3232#include "PlatformString.h"
     33#include <errno.h>
     34#include <libgen.h>
     35#include <sys/stat.h>
     36#include <unistd.h>
    3337#include <wtf/text/CString.h>
    34 
    35 #include <sys/stat.h>
    36 #include <libgen.h>
    37 #include <unistd.h>
    3838
    3939namespace WebCore {
     
    6464    // unlink(...) returns 0 on successful deletion of the path and non-zero in any other case (including invalid permissions or non-existent file)
    6565    return !unlink(fsRep.data());
     66}
     67
     68PlatformFileHandle openFile(const String& path, FileOpenMode mode)
     69{
     70    int platformFlag = 0;
     71    if (mode == OpenForRead)
     72        platformFlag |= O_RDONLY;
     73    else if (mode == OpenForWrite)
     74        platformFlag |= (O_WRONLY | O_CREAT | O_TRUNC);
     75    return open(path.utf8().data(), platformFlag, 0666);
     76}
     77
     78void closeFile(PlatformFileHandle& handle)
     79{
     80    if (isHandleValid(handle)) {
     81        close(handle);
     82        handle = invalidPlatformFileHandle;
     83    }
     84}
     85
     86long long seekFile(PlatformFileHandle handle, long long offset, FileSeekOrigin origin)
     87{
     88    int whence = SEEK_SET;
     89    switch (origin) {
     90    case SeekFromBeginning:
     91        whence = SEEK_SET;
     92        break;
     93    case SeekFromCurrent:
     94        whence = SEEK_CUR;
     95        break;
     96    case SeekFromEnd:
     97        whence = SEEK_END;
     98        break;
     99    default:
     100        ASSERT_NOT_REACHED();
     101    }
     102    return static_cast<long long>(lseek(handle, offset, whence));
     103}
     104
     105bool truncateFile(PlatformFileHandle handle, long long offset)
     106{
     107    // ftruncate returns 0 to indicate the success.
     108    return !ftruncate(handle, offset);
     109}
     110
     111int writeToFile(PlatformFileHandle handle, const char* data, int length)
     112{
     113    do {
     114        int bytesWritten = write(handle, data, static_cast<size_t>(length));
     115        if (bytesWritten >= 0)
     116            return bytesWritten;
     117    } while (errno == EINTR);
     118    return -1;
     119}
     120
     121int readFromFile(PlatformFileHandle handle, char* data, int length)
     122{
     123    do {
     124        int bytesRead = read(handle, data, static_cast<size_t>(length));
     125        if (bytesRead >= 0)
     126            return bytesRead;
     127    } while (errno == EINTR);
     128    return -1;
    66129}
    67130
Note: See TracChangeset for help on using the changeset viewer.