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(FILE_SYSTEM) 34 35 #include "FileWriterSync.h" 36 37 #include "AsyncFileWriter.h" 38 #include "Blob.h" 39 #include "FileException.h" 40 41 namespace WebCore { 42 43 void FileWriterSync::write(Blob* data, ExceptionCode& ec) 44 { 45 ASSERT(writer()); 46 ASSERT(m_complete); 47 ec = 0; 48 if (!data) { 49 ec = FileException::TYPE_MISMATCH_ERR; 50 return; 51 } 52 53 prepareForWrite(); 54 writer()->write(position(), data); 55 writer()->waitForOperationToComplete(); 56 ASSERT(m_complete); 57 ec = FileException::ErrorCodeToExceptionCode(m_error); 58 if (ec) 59 return; 60 setPosition(position() + data->size()); 61 if (position() > length()) 62 setLength(position()); 63 } 64 65 void FileWriterSync::seek(long long position, ExceptionCode& ec) 66 { 67 ASSERT(writer()); 68 ASSERT(m_complete); 69 ec = 0; 70 seekInternal(position); 71 } 72 73 void FileWriterSync::truncate(long long offset, ExceptionCode& ec) 74 { 75 ASSERT(writer()); 76 ASSERT(m_complete); 77 ec = 0; 78 if (offset < 0) { 79 ec = FileException::INVALID_STATE_ERR; 80 return; 81 } 82 prepareForWrite(); 83 writer()->truncate(offset); 84 writer()->waitForOperationToComplete(); 85 ASSERT(m_complete); 86 ec = FileException::ErrorCodeToExceptionCode(m_error); 87 if (ec) 88 return; 89 if (offset < position()) 90 setPosition(offset); 91 setLength(offset); 92 } 93 94 void FileWriterSync::didWrite(long long bytes, bool complete) 95 { 96 ASSERT(m_error == FileError::OK); 97 ASSERT(!m_complete); 98 #ifndef NDEBUG 99 m_complete = complete; 100 #else 101 ASSERT_UNUSED(complete, complete); 102 #endif 103 } 104 105 void FileWriterSync::didTruncate() 106 { 107 ASSERT(m_error == FileError::OK); 108 ASSERT(!m_complete); 109 #ifndef NDEBUG 110 m_complete = true; 111 #endif 112 } 113 114 void FileWriterSync::didFail(FileError::ErrorCode error) 115 { 116 ASSERT(m_error == FileError::OK); 117 m_error = error; 118 ASSERT(!m_complete); 119 #ifndef NDEBUG 120 m_complete = true; 121 #endif 122 } 123 124 FileWriterSync::FileWriterSync() 125 : m_error(FileError::OK) 126 #ifndef NDEBUG 127 , m_complete(true) 128 #endif 129 { 130 } 131 132 void FileWriterSync::prepareForWrite() 133 { 134 ASSERT(m_complete); 135 m_error = FileError::OK; 136 #ifndef NDEBUG 137 m_complete = false; 138 #endif 139 } 140 141 FileWriterSync::~FileWriterSync() 142 { 143 } 144 145 146 } // namespace WebCore 147 148 #endif // ENABLE(FILE_SYSTEM) 149