1e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block/* 2e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * Copyright (C) 2010 Company 100, Inc. All rights reserved. 3e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * 4e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * Redistribution and use in source and binary forms, with or without 5e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * modification, are permitted provided that the following conditions 6e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * are met: 7e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * 1. Redistributions of source code must retain the above copyright 8e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * notice, this list of conditions and the following disclaimer. 9e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * 2. Redistributions in binary form must reproduce the above copyright 10e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * notice, this list of conditions and the following disclaimer in the 11e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * documentation and/or other materials provided with the distribution. 12e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * 13e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 14e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 16e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 17e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 18e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 19e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 20e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 21e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block */ 25e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 26e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include "config.h" 27e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include "SharedBuffer.h" 28e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 29e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include "FileSystem.h" 30e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 31e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include <fcntl.h> 32e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include <sys/stat.h> 33e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include <unistd.h> 34e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block#include <wtf/text/CString.h> 35e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 36e78cbe89e6f337f2f1fe40315be88f742b547151Steve Blocknamespace WebCore { 37e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 38e78cbe89e6f337f2f1fe40315be88f742b547151Steve BlockPassRefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& filePath) 39e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block{ 40e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block if (filePath.isEmpty()) 41e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block return 0; 42e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 43545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch CString filename = fileSystemRepresentation(filePath); 44545e470e52f0ac6a3a072bf559c796b42c6066b6Ben Murdoch int fd = open(filename.data(), O_RDONLY); 45e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block if (fd == -1) 46e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block return 0; 47e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 48e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block struct stat fileStat; 49e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block if (fstat(fd, &fileStat)) { 50e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block close(fd); 51e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block return 0; 52e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block } 53e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 54e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block size_t bytesToRead = fileStat.st_size; 55e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block if (bytesToRead != fileStat.st_size) { 56e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block close(fd); 57e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block return 0; 58e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block } 59e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 60e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block RefPtr<SharedBuffer> result = create(); 61e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block result->m_buffer.grow(bytesToRead); 62e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 63e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block size_t totalBytesRead = 0; 64e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block ssize_t bytesRead; 65e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block while ((bytesRead = read(fd, result->m_buffer.data() + totalBytesRead, bytesToRead - totalBytesRead)) > 0) 66e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block totalBytesRead += bytesRead; 67e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 68e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block close(fd); 69e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 70e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block return totalBytesRead == bytesToRead ? result.release() : 0; 71e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block} 72e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block 73e78cbe89e6f337f2f1fe40315be88f742b547151Steve Block} // namespace WebCore 74