Skip to content

Commit 83f3ff9

Browse files
author
lind
committed
centralize logic
1 parent a67fd4f commit 83f3ff9

File tree

2 files changed

+31
-42
lines changed

2 files changed

+31
-42
lines changed

extension/data_loader/file_data_loader.cpp

Lines changed: 26 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -102,30 +102,18 @@ Result<int> getFDFromUri(const char* file_descriptor_uri) {
102102
return fd;
103103
}
104104

105-
Result<FileDataLoader> FileDataLoader::fromFileDescriptorUri(
106-
const char* file_descriptor_uri,
105+
Result<FileDataLoader> FileDataLoader::fromFileDescriptor(
106+
const char* file_name,
107+
const int fd,
107108
size_t alignment) {
108-
ET_CHECK_OR_RETURN_ERROR(
109-
is_power_of_2(alignment),
110-
InvalidArgument,
111-
"Alignment %zu is not a power of 2",
112-
alignment);
113-
114-
auto parsed_fd = getFDFromUri(file_descriptor_uri);
115-
if (!parsed_fd.ok()) {
116-
return parsed_fd.error();
117-
}
118-
119-
int fd = parsed_fd.get();
120-
121109
// Cache the file size.
122110
struct stat st;
123111
int err = ::fstat(fd, &st);
124112
if (err < 0) {
125113
ET_LOG(
126114
Error,
127115
"Could not get length of %s: %s (%d)",
128-
file_descriptor_uri,
116+
file_name,
129117
::strerror(errno),
130118
errno);
131119
::close(fd);
@@ -134,16 +122,35 @@ Result<FileDataLoader> FileDataLoader::fromFileDescriptorUri(
134122
size_t file_size = st.st_size;
135123

136124
// Copy the filename so we can print better debug messages if reads fail.
137-
const char* file_name_copy = ::strdup(file_descriptor_uri);
125+
const char* file_name_copy = ::strdup(file_name);
138126
if (file_name_copy == nullptr) {
139-
ET_LOG(Error, "strdup(%s) failed", file_descriptor_uri);
127+
ET_LOG(Error, "strdup(%s) failed", file_name);
140128
::close(fd);
141129
return Error::MemoryAllocationFailed;
142130
}
143131

144132
return FileDataLoader(fd, file_size, alignment, file_name_copy);
145133
}
146134

135+
Result<FileDataLoader> FileDataLoader::fromFileDescriptorUri(
136+
const char* file_descriptor_uri,
137+
size_t alignment) {
138+
ET_CHECK_OR_RETURN_ERROR(
139+
is_power_of_2(alignment),
140+
InvalidArgument,
141+
"Alignment %zu is not a power of 2",
142+
alignment);
143+
144+
auto parsed_fd = getFDFromUri(file_descriptor_uri);
145+
if (!parsed_fd.ok()) {
146+
return parsed_fd.error();
147+
}
148+
149+
int fd = parsed_fd.get();
150+
151+
return fromFileDescriptor(file_descriptor_uri, fd, alignment);
152+
}
153+
147154
Result<FileDataLoader> FileDataLoader::from(
148155
const char* file_name,
149156
size_t alignment) {
@@ -163,30 +170,7 @@ Result<FileDataLoader> FileDataLoader::from(
163170
return Error::AccessFailed;
164171
}
165172

166-
// Cache the file size.
167-
struct stat st;
168-
int err = ::fstat(fd, &st);
169-
if (err < 0) {
170-
ET_LOG(
171-
Error,
172-
"Could not get length of %s: %s (%d)",
173-
file_name,
174-
::strerror(errno),
175-
errno);
176-
::close(fd);
177-
return Error::AccessFailed;
178-
}
179-
size_t file_size = st.st_size;
180-
181-
// Copy the filename so we can print better debug messages if reads fail.
182-
const char* file_name_copy = ::strdup(file_name);
183-
if (file_name_copy == nullptr) {
184-
ET_LOG(Error, "strdup(%s) failed", file_name);
185-
::close(fd);
186-
return Error::MemoryAllocationFailed;
187-
}
188-
189-
return FileDataLoader(fd, file_size, alignment, file_name_copy);
173+
return fromFileDescriptor(file_name, fd, alignment);
190174
}
191175

192176
namespace {

extension/data_loader/file_data_loader.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ class FileDataLoader final : public executorch::runtime::DataLoader {
100100
void* buffer) const override;
101101

102102
private:
103+
static executorch::runtime::Result<FileDataLoader> fromFileDescriptor(
104+
const char* file_name,
105+
const int fd,
106+
size_t alignment = alignof(std::max_align_t));
107+
103108
FileDataLoader(
104109
int fd,
105110
size_t file_size,

0 commit comments

Comments
 (0)