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#if ENABLE(BLOB) 34 35#include "FileStream.h" 36 37#include "FileSystem.h" 38#include <wtf/text/WTFString.h> 39 40namespace WebCore { 41 42FileStream::FileStream() 43 : m_handle(invalidPlatformFileHandle) 44 , m_bytesProcessed(0) 45 , m_totalBytesToRead(0) 46{ 47} 48 49FileStream::~FileStream() 50{ 51 ASSERT(!isHandleValid(m_handle)); 52} 53 54// FIXME: To be removed when we switch to using BlobData. 55void FileStream::start() 56{ 57} 58 59void FileStream::stop() 60{ 61 close(); 62} 63 64long long FileStream::getSize(const String& path, double expectedModificationTime) 65{ 66 // Check the modification time for the possible file change. 67 time_t modificationTime; 68 if (!getFileModificationTime(path, modificationTime)) 69 return -1; 70 if (isValidFileTime(expectedModificationTime)) { 71 if (static_cast<time_t>(expectedModificationTime) != modificationTime) 72 return -1; 73 } 74 75 // Now get the file size. 76 long long length; 77 if (!getFileSize(path, length)) 78 return -1; 79 80 return length; 81} 82 83bool FileStream::openForRead(const String& path, long long offset, long long length) 84{ 85 if (isHandleValid(m_handle)) 86 return true; 87 88 // Open the file. 89 m_handle = openFile(path, OpenForRead); 90 if (!isHandleValid(m_handle)) 91 return false; 92 93 // Jump to the beginning position if the file has been sliced. 94 if (offset > 0) { 95 if (seekFile(m_handle, offset, SeekFromBeginning) < 0) 96 return false; 97 } 98 99 m_totalBytesToRead = length; 100 m_bytesProcessed = 0; 101 102 return true; 103} 104 105bool FileStream::openForWrite(const String&) 106{ 107 // FIXME: to be implemented. 108 return false; 109} 110 111void FileStream::close() 112{ 113 if (isHandleValid(m_handle)) { 114 closeFile(m_handle); 115 m_handle = invalidPlatformFileHandle; 116 } 117} 118 119int FileStream::read(char* buffer, int bufferSize) 120{ 121 if (!isHandleValid(m_handle)) 122 return -1; 123 124 long long remaining = m_totalBytesToRead - m_bytesProcessed; 125 int bytesToRead = (remaining < bufferSize) ? static_cast<int>(remaining) : bufferSize; 126 int bytesRead = 0; 127 if (bytesToRead > 0) 128 bytesRead = readFromFile(m_handle, buffer, bytesToRead); 129 if (bytesRead < 0) 130 return -1; 131 if (bytesRead > 0) 132 m_bytesProcessed += bytesRead; 133 134 return bytesRead; 135} 136 137int FileStream::write(const KURL&, long long, int) 138{ 139 // FIXME: to be implemented. 140 return -1; 141} 142 143bool FileStream::truncate(long long) 144{ 145 // FIXME: to be implemented. 146 return false; 147} 148 149} // namespace WebCore 150 151#endif // ENABLE(BLOB) 152