1/* 2 * Copyright (C) 2010 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include "config.h" 32 33#include "FileStream.h" 34 35#include "FileSystem.h" 36#include <wtf/text/WTFString.h> 37 38namespace WebCore { 39 40FileStream::FileStream() 41 : m_handle(invalidPlatformFileHandle) 42 , m_bytesProcessed(0) 43 , m_totalBytesToRead(0) 44{ 45} 46 47FileStream::~FileStream() 48{ 49 ASSERT(!isHandleValid(m_handle)); 50} 51 52// FIXME: To be removed when we switch to using BlobData. 53void FileStream::start() 54{ 55} 56 57void FileStream::stop() 58{ 59 close(); 60} 61 62long long FileStream::getSize(const String& path, double expectedModificationTime) 63{ 64 // Check the modification time for the possible file change. 65 time_t modificationTime; 66 if (!getFileModificationTime(path, modificationTime)) 67 return -1; 68 if (isValidFileTime(expectedModificationTime)) { 69 if (static_cast<time_t>(expectedModificationTime) != modificationTime) 70 return -1; 71 } 72 73 // Now get the file size. 74 long long length; 75 if (!getFileSize(path, length)) 76 return -1; 77 78 return length; 79} 80 81bool FileStream::openForRead(const String& path, long long offset, long long length) 82{ 83 if (isHandleValid(m_handle)) 84 return true; 85 86 // Open the file. 87 m_handle = openFile(path, OpenForRead); 88 if (!isHandleValid(m_handle)) 89 return false; 90 91 // Jump to the beginning position if the file has been sliced. 92 if (offset > 0) { 93 if (seekFile(m_handle, offset, SeekFromBeginning) < 0) 94 return false; 95 } 96 97 m_totalBytesToRead = length; 98 m_bytesProcessed = 0; 99 100 return true; 101} 102 103bool FileStream::openForWrite(const String&) 104{ 105 // FIXME: to be implemented. 106 return false; 107} 108 109void FileStream::close() 110{ 111 if (isHandleValid(m_handle)) { 112 closeFile(m_handle); 113 m_handle = invalidPlatformFileHandle; 114 } 115} 116 117int FileStream::read(char* buffer, int bufferSize) 118{ 119 if (!isHandleValid(m_handle)) 120 return -1; 121 122 long long remaining = m_totalBytesToRead - m_bytesProcessed; 123 int bytesToRead = (remaining < bufferSize) ? static_cast<int>(remaining) : bufferSize; 124 int bytesRead = 0; 125 if (bytesToRead > 0) 126 bytesRead = readFromFile(m_handle, buffer, bytesToRead); 127 if (bytesRead < 0) 128 return -1; 129 if (bytesRead > 0) 130 m_bytesProcessed += bytesRead; 131 132 return bytesRead; 133} 134 135int FileStream::write(const URL&, long long, int) 136{ 137 // FIXME: to be implemented. 138 return -1; 139} 140 141bool FileStream::truncate(long long) 142{ 143 // FIXME: to be implemented. 144 return false; 145} 146 147} // namespace WebCore 148