Home | History | Annotate | Download | only in win
      1 /*
      2  * Copyright (C) 2007 Apple 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
      6  * are met:
      7  * 1. Redistributions of source code must retain the above copyright
      8  *    notice, this list of conditions and the following disclaimer.
      9  * 2. Redistributions in binary form must reproduce the above copyright
     10  *    notice, this list of conditions and the following disclaimer in the
     11  *    documentation and/or other materials provided with the distribution.
     12  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
     13  *     its contributors may be used to endorse or promote products derived
     14  *     from this software without specific prior written permission.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
     17  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
     20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
     23  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
     24  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  */
     28 
     29 #include "config.h"
     30 #include "SharedBuffer.h"
     31 
     32 namespace WebCore {
     33 
     34 PassRefPtr<SharedBuffer> SharedBuffer::createWithContentsOfFile(const String& filePath)
     35 {
     36     if (filePath.isEmpty())
     37         return 0;
     38 
     39     String nullifiedPath = filePath;
     40     FILE* fileDescriptor = 0;
     41     if (_wfopen_s(&fileDescriptor, nullifiedPath.charactersWithNullTermination(), TEXT("rb")) || !fileDescriptor) {
     42         LOG_ERROR("Failed to open file %s to create shared buffer, errno(%i)", filePath.ascii().data(), errno);
     43         return 0;
     44     }
     45 
     46     RefPtr<SharedBuffer> result;
     47 
     48     // Stat the file to get its size
     49     struct _stat64 fileStat;
     50     if (_fstat64(_fileno(fileDescriptor), &fileStat))
     51         goto exit;
     52 
     53     result = SharedBuffer::create();
     54     result->m_buffer.resize(fileStat.st_size);
     55     if (result->m_buffer.size() != fileStat.st_size) {
     56         result = 0;
     57         goto exit;
     58     }
     59 
     60     result->m_size = result->m_buffer.size();
     61 
     62     if (fread(result->m_buffer.data(), 1, fileStat.st_size, fileDescriptor) != fileStat.st_size)
     63         LOG_ERROR("Failed to fully read contents of file %s - errno(%i)", filePath.ascii().data(), errno);
     64 
     65 exit:
     66     fclose(fileDescriptor);
     67     return result.release();
     68 }
     69 
     70 }; // namespace WebCore
     71