2 * Copyright (C) 2010 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
34 #include "FileSystem.h"
36 #include <wtf/Assertions.h>
41 static const double invalidModificationTime = 0;
43 static double getFileSnapshotModificationTime(const String& path)
45 // FIXME: synchronized file call
46 time_t modificationTime;
47 if (getFileModificationTime(path, modificationTime))
48 return static_cast<double>(modificationTime);
49 return invalidModificationTime;
51 #endif // ENABLE(BLOB)
53 // DataBlobItem ----------------------------------------------------------------
56 PassRefPtr<BlobItem> DataBlobItem::slice(long long start, long long length)
58 ASSERT(start >= 0 && length >= 0);
59 ASSERT(static_cast<unsigned long long>(start) < size());
60 if (!start && size() <= static_cast<unsigned long long>(length))
62 if (static_cast<unsigned long long>(start + length) > size())
63 length = size() - start;
64 return DataRangeBlobItem::create(this, start, length);
68 // FileBlobItem ----------------------------------------------------------------
70 PassRefPtr<BlobItem> FileBlobItem::create(const String& path)
72 return adoptRef(static_cast<BlobItem*>(new FileBlobItem(path)));
75 FileBlobItem::FileBlobItem(const String& path)
77 , m_fileName(pathGetFileName(m_path))
81 #if ENABLE(DIRECTORY_UPLOAD)
82 PassRefPtr<BlobItem> FileBlobItem::create(const String& path, const String& relativePath)
84 return adoptRef(static_cast<BlobItem*>(new FileBlobItem(path, relativePath)));
87 FileBlobItem::FileBlobItem(const String& path, const String& relativePath)
89 , m_fileName(pathGetFileName(m_path))
90 , m_relativePath(relativePath)
95 unsigned long long FileBlobItem::size() const
97 // FIXME: synchronized file call
99 if (!getFileSize(m_path, size))
101 return static_cast<unsigned long long>(size);
105 PassRefPtr<BlobItem> FileBlobItem::slice(long long start, long long length)
107 ASSERT(start >= 0 && length >= 0);
108 long long fileSize = size();
109 ASSERT(start < fileSize);
110 if (!start && fileSize <= length)
112 if (start + length > fileSize)
113 length = fileSize - start;
114 const FileRangeBlobItem* fileRangeItem = toFileRangeBlobItem();
115 double modificationTime = fileRangeItem ? fileRangeItem->snapshotModificationTime() : getFileSnapshotModificationTime(path());
116 return FileRangeBlobItem::create(path(), start, length, modificationTime);
118 #endif // ENABLE(BLOB)
120 // StringBlobItem --------------------------------------------------------------
122 PassRefPtr<BlobItem> StringBlobItem::create(const CString& text)
124 return adoptRef(static_cast<BlobItem*>(new StringBlobItem(text)));
127 StringBlobItem::StringBlobItem(const CString& text)
132 // ByteArrayBlobItem ----------------------------------------------------------
134 PassRefPtr<BlobItem> ByteArrayBlobItem::create(const char* data, size_t size)
136 return adoptRef(static_cast<BlobItem*>(new ByteArrayBlobItem(data, size)));
139 ByteArrayBlobItem::ByteArrayBlobItem(const char* data, size_t size)
141 m_bytesArray.append(data, size);
146 // DataRangeBlobItem -----------------------------------------------------------
148 PassRefPtr<BlobItem> DataRangeBlobItem::create(PassRefPtr<DataBlobItem> item, long long start, long long length)
150 return adoptRef(static_cast<BlobItem*>(new DataRangeBlobItem(item, start, length)));
153 DataRangeBlobItem::DataRangeBlobItem(PassRefPtr<DataBlobItem> item, long long start, long long length)
156 const DataRangeBlobItem* rangeItem = item->toDataRangeBlobItem();
158 m_item = rangeItem->m_item;
159 m_start = start + rangeItem->m_start;
160 ASSERT(!m_item->toDataRangeBlobItem());
161 ASSERT(static_cast<unsigned long long>(m_start + m_length) <= m_item->size());
168 const char* DataRangeBlobItem::data() const
170 return m_item->data() + m_start;
173 // FileRangeBlobItem -----------------------------------------------------------
175 PassRefPtr<BlobItem> FileRangeBlobItem::create(const String& path, long long start, long long length, double snapshotModificationTime)
177 return adoptRef(static_cast<BlobItem*>(new FileRangeBlobItem(path, start, length, snapshotModificationTime)));
180 FileRangeBlobItem::FileRangeBlobItem(const String& path, long long start, long long length, double modificationTime)
184 , m_snapshotModificationTime(modificationTime)
186 m_uniqueName = "Blob" + createCanonicalUUIDString();
187 m_uniqueName.replace("-", ""); // For safty, remove '-' from the filename snce some servers may not like it.
190 #endif // ENABLE(BLOB)
192 } // namespace WebCore