@@ -102,30 +102,18 @@ Result<int> getFDFromUri(const char* file_descriptor_uri) {
102
102
return fd;
103
103
}
104
104
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,
107
108
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
-
121
109
// Cache the file size.
122
110
struct stat st;
123
111
int err = ::fstat (fd, &st);
124
112
if (err < 0 ) {
125
113
ET_LOG (
126
114
Error,
127
115
" Could not get length of %s: %s (%d)" ,
128
- file_descriptor_uri ,
116
+ file_name ,
129
117
::strerror (errno),
130
118
errno);
131
119
::close (fd);
@@ -134,16 +122,35 @@ Result<FileDataLoader> FileDataLoader::fromFileDescriptorUri(
134
122
size_t file_size = st.st_size ;
135
123
136
124
// 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 );
138
126
if (file_name_copy == nullptr ) {
139
- ET_LOG (Error, " strdup(%s) failed" , file_descriptor_uri );
127
+ ET_LOG (Error, " strdup(%s) failed" , file_name );
140
128
::close (fd);
141
129
return Error::MemoryAllocationFailed;
142
130
}
143
131
144
132
return FileDataLoader (fd, file_size, alignment, file_name_copy);
145
133
}
146
134
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
+
147
154
Result<FileDataLoader> FileDataLoader::from (
148
155
const char * file_name,
149
156
size_t alignment) {
@@ -163,30 +170,7 @@ Result<FileDataLoader> FileDataLoader::from(
163
170
return Error::AccessFailed;
164
171
}
165
172
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);
190
174
}
191
175
192
176
namespace {
0 commit comments