Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
file.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <fcntl.h>
8#include <sys/stat.h>
9
10#include <climits>
11#include <cstdint>
12
14
15#if defined(OS_WIN)
16#define BINARY_MODE _O_BINARY
17#else
18#define BINARY_MODE 0
19#endif
20
21#if defined(OS_WIN)
22#include <BaseTsd.h>
23typedef SSIZE_T ssize_t;
24#endif
25
28
29namespace filesystem {
30namespace {
31
32template <typename T>
33bool ReadFileDescriptor(int fd, T* result) {
34 if (!result) {
35 return false;
36 }
37
38 result->clear();
39
40 if (fd < 0)
41 return false;
42
43 constexpr size_t kBufferSize = 1 << 16;
44 size_t offset = 0;
45 ssize_t bytes_read = 0;
46 do {
47 offset += bytes_read;
48 result->resize(offset + kBufferSize);
49 bytes_read = HANDLE_EINTR(read(fd, &(*result)[offset], kBufferSize));
50 } while (bytes_read > 0);
51
52 if (bytes_read < 0) {
53 result->clear();
54 return false;
55 }
56
57 result->resize(offset + bytes_read);
58 return true;
59}
60
61} // namespace
62
63std::pair<uint8_t*, intptr_t> ReadFileDescriptorToBytes(int fd) {
64 std::pair<uint8_t*, intptr_t> failure_pair{nullptr, -1};
65 struct stat st;
66 if (fstat(fd, &st) != 0) {
67 return failure_pair;
68 }
69 intptr_t file_size = st.st_size;
70 uint8_t* ptr = (uint8_t*)malloc(file_size);
71
72 size_t bytes_left = file_size;
73 size_t offset = 0;
74 while (bytes_left > 0) {
75 ssize_t bytes_read = HANDLE_EINTR(read(fd, &ptr[offset], bytes_left));
76 if (bytes_read < 0) {
77 return failure_pair;
78 }
79 offset += bytes_read;
80 bytes_left -= bytes_read;
81 }
82 return std::pair<uint8_t*, intptr_t>(ptr, file_size);
83}
84
85bool ReadFileToString(const std::string& path, std::string* result) {
86 Descriptor fd(open(path.c_str(), O_RDONLY));
87 return ReadFileDescriptor(fd.get(), result);
88}
89
90bool ReadFileDescriptorToString(int fd, std::string* result) {
91 return ReadFileDescriptor(fd, result);
92}
93
94std::pair<uint8_t*, intptr_t> ReadFileToBytes(const std::string& path) {
95 std::pair<uint8_t*, intptr_t> failure_pair{nullptr, -1};
96 Descriptor fd(open(path.c_str(), O_RDONLY | BINARY_MODE));
97 if (!fd.is_valid())
98 return failure_pair;
99 return ReadFileDescriptorToBytes(fd.get());
100}
101
102} // namespace filesystem
static bool read(SkStream *stream, void *buffer, size_t amount)
static const size_t kBufferSize
Definition SkString.cpp:27
GAsyncResult * result
bool ReadFileDescriptorToString(int fd, std::string *result)
Definition file.cc:90
bool ReadFileToString(const std::string &path, std::string *result)
Definition file.cc:85
std::pair< uint8_t *, intptr_t > ReadFileToBytes(const std::string &path)
Definition file.cc:94
std::pair< uint8_t *, intptr_t > ReadFileDescriptorToBytes(int fd)
Definition file.cc:63
#define T
Point offset
#define HANDLE_EINTR(x)
#define BINARY_MODE
Definition file.cc:18
LONG_PTR SSIZE_T