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