Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(264)

Side by Side Diff: runtime/bin/file_fuchsia.cc

Issue 3001963002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Fuchsia fix Created 3 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « runtime/bin/file_android.cc ('k') | runtime/bin/file_linux.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #include "platform/globals.h" 5 #include "platform/globals.h"
6 #if defined(HOST_OS_FUCHSIA) 6 #if defined(HOST_OS_FUCHSIA)
7 7
8 #include "bin/file.h" 8 #include "bin/file.h"
9 9
10 #include <errno.h> // NOLINT 10 #include <errno.h> // NOLINT
11 #include <fcntl.h> // NOLINT 11 #include <fcntl.h> // NOLINT
12 #include <libgen.h> // NOLINT 12 #include <libgen.h> // NOLINT
13 #include <sys/mman.h> // NOLINT 13 #include <mxio/namespace.h> // NOLINT
14 #include <sys/stat.h> // NOLINT 14 #include <sys/mman.h> // NOLINT
15 #include <sys/types.h> // NOLINT 15 #include <sys/stat.h> // NOLINT
16 #include <unistd.h> // NOLINT 16 #include <sys/types.h> // NOLINT
17 #include <utime.h> // NOLINT 17 #include <unistd.h> // NOLINT
18 #include <utime.h> // NOLINT
18 19
19 #include "bin/builtin.h" 20 #include "bin/builtin.h"
20 #include "bin/fdutils.h" 21 #include "bin/fdutils.h"
21 #include "bin/log.h" 22 #include "bin/log.h"
23 #include "bin/namespace.h"
22 #include "platform/signal_blocker.h" 24 #include "platform/signal_blocker.h"
23 #include "platform/utils.h" 25 #include "platform/utils.h"
24 26
25 namespace dart { 27 namespace dart {
26 namespace bin { 28 namespace bin {
27 29
28 class FileHandle { 30 class FileHandle {
29 public: 31 public:
30 explicit FileHandle(int fd) : fd_(fd) {} 32 explicit FileHandle(int fd) : fd_(fd) {}
31 ~FileHandle() {} 33 ~FileHandle() {}
32 int fd() const { return fd_; } 34 int fd() const { return fd_; }
33 void set_fd(int fd) { fd_ = fd; } 35 void set_fd(int fd) { fd_ = fd; }
34 36
35 private: 37 private:
36 int fd_; 38 int fd_;
37 39
38 DISALLOW_COPY_AND_ASSIGN(FileHandle); 40 DISALLOW_COPY_AND_ASSIGN(FileHandle);
39 }; 41 };
40 42
41 File::~File() { 43 File::~File() {
42 if (!IsClosed()) { 44 if (!IsClosed() && (handle_->fd() != STDOUT_FILENO) &&
45 (handle_->fd() != STDERR_FILENO)) {
43 Close(); 46 Close();
44 } 47 }
45 delete handle_; 48 delete handle_;
46 } 49 }
47 50
48 void File::Close() { 51 void File::Close() {
49 ASSERT(handle_->fd() >= 0); 52 ASSERT(handle_->fd() >= 0);
50 if (handle_->fd() == STDOUT_FILENO) { 53 if (handle_->fd() == STDOUT_FILENO) {
51 // If stdout, redirect fd to /dev/null. 54 // If stdout, redirect fd to /dev/null.
52 int null_fd = NO_RETRY_EXPECTED(open("/dev/null", O_WRONLY)); 55 int null_fd = NO_RETRY_EXPECTED(open("/dev/null", O_WRONLY));
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
168 return st.st_size; 171 return st.st_size;
169 } 172 }
170 return -1; 173 return -1;
171 } 174 }
172 175
173 File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) { 176 File* File::FileOpenW(const wchar_t* system_name, FileOpenMode mode) {
174 UNREACHABLE(); 177 UNREACHABLE();
175 return NULL; 178 return NULL;
176 } 179 }
177 180
178 File* File::Open(const char* name, FileOpenMode mode) { 181 File* File::Open(Namespace* namespc, const char* name, FileOpenMode mode) {
182 NamespaceScope ns(namespc, name);
179 // Report errors for non-regular files. 183 // Report errors for non-regular files.
180 struct stat st; 184 struct stat64 st;
181 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { 185 if (NO_RETRY_EXPECTED(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) {
182 if (S_ISDIR(st.st_mode)) { 186 if (S_ISDIR(st.st_mode)) {
183 errno = EISDIR; 187 errno = EISDIR;
184 return NULL; 188 return NULL;
185 } 189 }
186 } 190 }
187 int flags = O_RDONLY; 191 int flags = O_RDONLY;
188 if ((mode & kWrite) != 0) { 192 if ((mode & kWrite) != 0) {
189 ASSERT((mode & kWriteOnly) == 0); 193 ASSERT((mode & kWriteOnly) == 0);
190 flags = (O_RDWR | O_CREAT); 194 flags = (O_RDWR | O_CREAT);
191 } 195 }
192 if ((mode & kWriteOnly) != 0) { 196 if ((mode & kWriteOnly) != 0) {
193 ASSERT((mode & kWrite) == 0); 197 ASSERT((mode & kWrite) == 0);
194 flags = (O_WRONLY | O_CREAT); 198 flags = (O_WRONLY | O_CREAT);
195 } 199 }
196 if ((mode & kTruncate) != 0) { 200 if ((mode & kTruncate) != 0) {
197 flags = flags | O_TRUNC; 201 flags = flags | O_TRUNC;
198 } 202 }
199 flags |= O_CLOEXEC; 203 flags |= O_CLOEXEC;
200 int fd = NO_RETRY_EXPECTED(open(name, flags, 0666)); 204 int fd = NO_RETRY_EXPECTED(openat64(ns.fd(), name, flags, 0666));
201 if (fd < 0) { 205 if (fd < 0) {
202 return NULL; 206 return NULL;
203 } 207 }
204 if ((((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) || 208 if ((((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) ||
205 (((mode & kWriteOnly) != 0) && ((mode & kTruncate) == 0))) { 209 (((mode & kWriteOnly) != 0) && ((mode & kTruncate) == 0))) {
206 int64_t position = lseek(fd, 0, SEEK_END); 210 int64_t position = lseek(fd, 0, SEEK_END);
207 if (position < 0) { 211 if (position < 0) {
208 return NULL; 212 return NULL;
209 } 213 }
210 } 214 }
211 return new File(new FileHandle(fd)); 215 return new File(new FileHandle(fd));
212 } 216 }
213 217
214 File* File::OpenStdio(int fd) { 218 File* File::OpenStdio(int fd) {
215 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd)); 219 return ((fd < 0) || (2 < fd)) ? NULL : new File(new FileHandle(fd));
216 } 220 }
217 221
218 bool File::Exists(const char* name) { 222 bool File::Exists(Namespace* namespc, const char* name) {
223 NamespaceScope ns(namespc, name);
219 struct stat64 st; 224 struct stat64 st;
220 if (NO_RETRY_EXPECTED(stat64(name, &st)) == 0) { 225 if (NO_RETRY_EXPECTED(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) {
221 // Everything but a directory and a link is a file to Dart. 226 // Everything but a directory and a link is a file to Dart.
222 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode); 227 return !S_ISDIR(st.st_mode) && !S_ISLNK(st.st_mode);
223 } else { 228 } else {
224 return false; 229 return false;
225 } 230 }
226 } 231 }
227 232
228 bool File::Create(const char* name) { 233 bool File::Create(Namespace* namespc, const char* name) {
229 int fd = 234 NamespaceScope ns(namespc, name);
230 NO_RETRY_EXPECTED(open64(name, O_RDONLY | O_CREAT | O_CLOEXEC, 0666)); 235 const int fd = NO_RETRY_EXPECTED(openat64(
236 ns.fd(), ns.path(), O_RDONLY | O_CREAT | O_CLOEXEC, 0666));
231 if (fd < 0) { 237 if (fd < 0) {
232 return false; 238 return false;
233 } 239 }
234 // File.create returns a File, so we shouldn't be giving the illusion that the 240 // File.create returns a File, so we shouldn't be giving the illusion that the
235 // call has created a file or that a file already exists if there is already 241 // call has created a file or that a file already exists if there is already
236 // an entity at the same path that is a directory or a link. 242 // an entity at the same path that is a directory or a link.
237 bool is_file = true; 243 bool is_file = true;
238 struct stat64 st; 244 struct stat64 st;
239 if (NO_RETRY_EXPECTED(fstat64(fd, &st)) == 0) { 245 if (NO_RETRY_EXPECTED(fstat64(fd, &st)) == 0) {
240 if (S_ISDIR(st.st_mode)) { 246 if (S_ISDIR(st.st_mode)) {
241 errno = EISDIR; 247 errno = EISDIR;
242 is_file = false; 248 is_file = false;
243 } else if (S_ISLNK(st.st_mode)) { 249 } else if (S_ISLNK(st.st_mode)) {
244 errno = ENOENT; 250 errno = ENOENT;
245 is_file = false; 251 is_file = false;
246 } 252 }
247 } 253 }
248 FDUtils::SaveErrorAndClose(fd); 254 FDUtils::SaveErrorAndClose(fd);
249 return is_file; 255 return is_file;
250 } 256 }
251 257
252 bool File::CreateLink(const char* name, const char* target) { 258 bool File::CreateLink(Namespace* namespc, const char* name, const char* target) {
253 return NO_RETRY_EXPECTED(symlink(target, name)) == 0; 259 NamespaceScope ns(namespc, name);
260 return NO_RETRY_EXPECTED(symlinkat(target, ns.fd(), ns.path())) == 0;
254 } 261 }
255 262
256 File::Type File::GetType(const char* pathname, bool follow_links) { 263 File::Type File::GetType(
257 struct stat entry_info; 264 Namespace* namespc, const char* name, bool follow_links) {
265 NamespaceScope ns(namespc, name);
266 struct stat64 entry_info;
258 int stat_success; 267 int stat_success;
259 if (follow_links) { 268 if (follow_links) {
260 stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); 269 stat_success = TEMP_FAILURE_RETRY(fstatat64(
270 ns.fd(), ns.path(), &entry_info, 0));
261 } else { 271 } else {
262 stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); 272 stat_success = TEMP_FAILURE_RETRY(fstatat64(
273 ns.fd(), ns.path(), &entry_info, AT_SYMLINK_NOFOLLOW));
263 } 274 }
264 if (stat_success == -1) { 275 if (stat_success == -1) {
265 return File::kDoesNotExist; 276 return File::kDoesNotExist;
266 } 277 }
267 if (S_ISDIR(entry_info.st_mode)) { 278 if (S_ISDIR(entry_info.st_mode)) {
268 return File::kIsDirectory; 279 return File::kIsDirectory;
269 } 280 }
270 if (S_ISREG(entry_info.st_mode)) { 281 if (S_ISREG(entry_info.st_mode)) {
271 return File::kIsFile; 282 return File::kIsFile;
272 } 283 }
273 if (S_ISLNK(entry_info.st_mode)) { 284 if (S_ISLNK(entry_info.st_mode)) {
274 return File::kIsLink; 285 return File::kIsLink;
275 } 286 }
276 return File::kDoesNotExist; 287 return File::kDoesNotExist;
277 } 288 }
278 289
279 static bool CheckTypeAndSetErrno(const char* name, 290 static bool CheckTypeAndSetErrno(Namespace* namespc,
291 const char* name,
280 File::Type expected, 292 File::Type expected,
281 bool follow_links) { 293 bool follow_links) {
282 File::Type actual = File::GetType(name, follow_links); 294 File::Type actual = File::GetType(namespc, name, follow_links);
283 if (actual == expected) { 295 if (actual == expected) {
284 return true; 296 return true;
285 } 297 }
286 switch (actual) { 298 switch (actual) {
287 case File::kIsDirectory: 299 case File::kIsDirectory:
288 errno = EISDIR; 300 errno = EISDIR;
289 break; 301 break;
290 case File::kDoesNotExist: 302 case File::kDoesNotExist:
291 errno = ENOENT; 303 errno = ENOENT;
292 break; 304 break;
293 default: 305 default:
294 errno = EINVAL; 306 errno = EINVAL;
295 break; 307 break;
296 } 308 }
297 return false; 309 return false;
298 } 310 }
299 311
300 bool File::Delete(const char* name) { 312 bool File::Delete(Namespace* namespc, const char* name) {
301 return CheckTypeAndSetErrno(name, kIsFile, true) && 313 NamespaceScope ns(namespc, name);
302 (NO_RETRY_EXPECTED(unlink(name)) == 0); 314 return CheckTypeAndSetErrno(namespc, name, kIsFile, true) &&
315 (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
303 } 316 }
304 317
305 bool File::DeleteLink(const char* name) { 318 bool File::DeleteLink(Namespace* namespc, const char* name) {
306 return CheckTypeAndSetErrno(name, kIsLink, false) && 319 NamespaceScope ns(namespc, name);
307 (NO_RETRY_EXPECTED(unlink(name)) == 0); 320 return CheckTypeAndSetErrno(namespc, name, kIsLink, false) &&
321 (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
308 } 322 }
309 323
310 bool File::Rename(const char* old_path, const char* new_path) { 324 bool File::Rename(
311 return CheckTypeAndSetErrno(old_path, kIsFile, true) && 325 Namespace* namespc, const char* old_path, const char* new_path) {
312 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); 326 NamespaceScope oldns(namespc, old_path);
327 NamespaceScope newns(namespc, new_path);
328 return CheckTypeAndSetErrno(namespc, old_path, kIsFile, true) &&
329 (NO_RETRY_EXPECTED(renameat(
330 oldns.fd(), oldns.path(), newns.fd(), newns.path())) == 0);
313 } 331 }
314 332
315 bool File::RenameLink(const char* old_path, const char* new_path) { 333 bool File::RenameLink(
316 return CheckTypeAndSetErrno(old_path, kIsLink, false) && 334 Namespace* namespc, const char* old_path, const char* new_path) {
317 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); 335 NamespaceScope oldns(namespc, old_path);
336 NamespaceScope newns(namespc, new_path);
337 return CheckTypeAndSetErrno(namespc, old_path, kIsLink, false) &&
338 (NO_RETRY_EXPECTED(renameat(
339 oldns.fd(), oldns.path(), newns.fd(), newns.path())) == 0);
318 } 340 }
319 341
320 bool File::Copy(const char* old_path, const char* new_path) { 342 bool File::Copy(Namespace* namespc, const char* old_path, const char* new_path) {
321 if (!CheckTypeAndSetErrno(old_path, kIsFile, true)) { 343 if (!CheckTypeAndSetErrno(namespc, old_path, kIsFile, true)) {
322 return false; 344 return false;
323 } 345 }
346 NamespaceScope oldns(namespc, old_path);
324 struct stat64 st; 347 struct stat64 st;
325 if (NO_RETRY_EXPECTED(stat64(old_path, &st)) != 0) { 348 if (NO_RETRY_EXPECTED(fstatat64(oldns.fd(), oldns.path(), &st, 0)) != 0) {
326 return false; 349 return false;
327 } 350 }
328 int old_fd = NO_RETRY_EXPECTED(open64(old_path, O_RDONLY | O_CLOEXEC)); 351 const int old_fd =
352 NO_RETRY_EXPECTED(openat64(oldns.fd(), oldns.path(), O_RDONLY | O_CLOEXEC) );
329 if (old_fd < 0) { 353 if (old_fd < 0) {
330 return false; 354 return false;
331 } 355 }
332 int new_fd = NO_RETRY_EXPECTED( 356 NamespaceScope newns(namespc, new_path);
333 open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode)); 357 const int new_fd = NO_RETRY_EXPECTED(openat64(
358 newns.fd(), newns.path(), O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_ mode));
334 if (new_fd < 0) { 359 if (new_fd < 0) {
335 VOID_TEMP_FAILURE_RETRY(close(old_fd)); 360 VOID_TEMP_FAILURE_RETRY(close(old_fd));
336 return false; 361 return false;
337 } 362 }
338 // TODO(MG-429): Use sendfile/copyfile or equivalent when there is one. 363 // TODO(MG-429): Use sendfile/copyfile or equivalent when there is one.
339 intptr_t result; 364 intptr_t result;
340 const intptr_t kBufferSize = 8 * KB; 365 const intptr_t kBufferSize = 8 * KB;
341 uint8_t buffer[kBufferSize]; 366 uint8_t buffer[kBufferSize];
342 while ((result = NO_RETRY_EXPECTED(read(old_fd, buffer, kBufferSize))) > 0) { 367 while ((result = NO_RETRY_EXPECTED(read(old_fd, buffer, kBufferSize))) > 0) {
343 int wrote = NO_RETRY_EXPECTED(write(new_fd, buffer, result)); 368 int wrote = NO_RETRY_EXPECTED(write(new_fd, buffer, result));
344 if (wrote != result) { 369 if (wrote != result) {
345 result = -1; 370 result = -1;
346 break; 371 break;
347 } 372 }
348 } 373 }
349 FDUtils::SaveErrorAndClose(old_fd); 374 FDUtils::SaveErrorAndClose(old_fd);
350 FDUtils::SaveErrorAndClose(new_fd); 375 FDUtils::SaveErrorAndClose(new_fd);
351 if (result < 0) { 376 if (result < 0) {
352 int e = errno; 377 int e = errno;
353 VOID_NO_RETRY_EXPECTED(unlink(new_path)); 378 VOID_NO_RETRY_EXPECTED(unlinkat(newns.fd(), newns.path(), 0));
354 errno = e; 379 errno = e;
355 return false; 380 return false;
356 } 381 }
357 return true; 382 return true;
358 } 383 }
359 384
360 static bool StatHelper(const char* name, struct stat64* st) { 385 static bool StatHelper(Namespace* namespc, const char* name, struct stat64* st) {
361 if (NO_RETRY_EXPECTED(stat64(name, st)) != 0) { 386 NamespaceScope ns(namespc, name);
387 if (NO_RETRY_EXPECTED(fstatat64(ns.fd(), ns.path(), st, 0)) != 0) {
362 return false; 388 return false;
363 } 389 }
364 // Signal an error if it's a directory. 390 // Signal an error if it's a directory.
365 if (S_ISDIR(st->st_mode)) { 391 if (S_ISDIR(st->st_mode)) {
366 errno = EISDIR; 392 errno = EISDIR;
367 return false; 393 return false;
368 } 394 }
369 // Otherwise assume the caller knows what it's doing. 395 // Otherwise assume the caller knows what it's doing.
370 return true; 396 return true;
371 } 397 }
372 398
373 int64_t File::LengthFromPath(const char* name) { 399 int64_t File::LengthFromPath(Namespace* namespc, const char* name) {
374 struct stat64 st; 400 struct stat64 st;
375 if (!StatHelper(name, &st)) { 401 if (!StatHelper(namespc, name, &st)) {
376 return -1; 402 return -1;
377 } 403 }
378 return st.st_size; 404 return st.st_size;
379 } 405 }
380 406
381 void File::Stat(const char* name, int64_t* data) { 407 static int64_t TimespecToMilliseconds(const struct timespec& t) {
382 struct stat st; 408 return static_cast<int64_t>(t.tv_sec) * 1000L +
383 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { 409 static_cast<int64_t>(t.tv_nsec) / 1000000L;
410 }
411
412 static void MillisecondsToTimespec(int64_t millis, struct timespec* t) {
413 ASSERT(t != NULL);
414 t->tv_sec = millis / kMillisecondsPerSecond;
415 t->tv_nsec = (millis - (t->tv_nsec * kMillisecondsPerSecond)) * 1000L;
416 }
417
418 void File::Stat(Namespace* namespc, const char* name, int64_t* data) {
419 NamespaceScope ns(namespc, name);
420 struct stat64 st;
421 if (TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) {
384 if (S_ISREG(st.st_mode)) { 422 if (S_ISREG(st.st_mode)) {
385 data[kType] = kIsFile; 423 data[kType] = kIsFile;
386 } else if (S_ISDIR(st.st_mode)) { 424 } else if (S_ISDIR(st.st_mode)) {
387 data[kType] = kIsDirectory; 425 data[kType] = kIsDirectory;
388 } else if (S_ISLNK(st.st_mode)) { 426 } else if (S_ISLNK(st.st_mode)) {
389 data[kType] = kIsLink; 427 data[kType] = kIsLink;
390 } else { 428 } else {
391 data[kType] = kDoesNotExist; 429 data[kType] = kDoesNotExist;
392 } 430 }
393 data[kCreatedTime] = static_cast<int64_t>(st.st_ctime) * 1000; 431 data[kCreatedTime] = TimespecToMilliseconds(st.st_ctim);
394 data[kModifiedTime] = static_cast<int64_t>(st.st_mtime) * 1000; 432 data[kModifiedTime] = TimespecToMilliseconds(st.st_mtim);
395 data[kAccessedTime] = static_cast<int64_t>(st.st_atime) * 1000; 433 data[kAccessedTime] = TimespecToMilliseconds(st.st_atim);
396 data[kMode] = st.st_mode; 434 data[kMode] = st.st_mode;
397 data[kSize] = st.st_size; 435 data[kSize] = st.st_size;
398 } else { 436 } else {
399 data[kType] = kDoesNotExist; 437 data[kType] = kDoesNotExist;
400 } 438 }
401 } 439 }
402 440
403 time_t File::LastModified(const char* name) { 441 time_t File::LastModified(Namespace* namespc, const char* name) {
404 struct stat st; 442 struct stat64 st;
405 if (!StatHelper(name, &st)) { 443 if (!StatHelper(namespc, name, &st)) {
406 return -1; 444 return -1;
407 } 445 }
408 return st.st_mtime; 446 return st.st_mtime;
409 } 447 }
410 448
411 time_t File::LastAccessed(const char* name) { 449 time_t File::LastAccessed(Namespace* namespc, const char* name) {
412 struct stat st; 450 struct stat64 st;
413 if (!StatHelper(name, &st)) { 451 if (!StatHelper(namespc, name, &st)) {
414 return -1; 452 return -1;
415 } 453 }
416 return st.st_atime; 454 return st.st_atime;
417 } 455 }
418 456
419 bool File::SetLastAccessed(const char* name, int64_t millis) { 457 bool File::SetLastAccessed(
458 Namespace* namespc, const char* name, int64_t millis) {
420 // First get the current times. 459 // First get the current times.
421 struct stat st; 460 struct stat64 st;
422 if (!StatHelper(name, &st)) { 461 if (!StatHelper(namespc, name, &st)) {
423 return false; 462 return false;
424 } 463 }
425 464
426 // Set the new time: 465 // Set the new time:
427 struct utimbuf times; 466 NamespaceScope ns(namespc, name);
428 times.actime = millis / kMillisecondsPerSecond; 467 struct timespec times[2];
429 times.modtime = st.st_mtime; 468 MillisecondsToTimespec(millis, &times[0]);
430 return utime(name, &times) == 0; 469 times[1] = st.st_mtim;
470 return utimensat(ns.fd(), ns.path(), times, 0) == 0;
431 } 471 }
432 472
433 bool File::SetLastModified(const char* name, int64_t millis) { 473 bool File::SetLastModified(
474 Namespace* namespc, const char* name, int64_t millis) {
434 // First get the current times. 475 // First get the current times.
435 struct stat st; 476 struct stat64 st;
436 if (!StatHelper(name, &st)) { 477 if (!StatHelper(namespc, name, &st)) {
437 return false; 478 return false;
438 } 479 }
439 480
440 // Set the new time: 481 // Set the new time:
441 struct utimbuf times; 482 NamespaceScope ns(namespc, name);
442 times.actime = st.st_atime; 483 struct timespec times[2];
443 times.modtime = millis / kMillisecondsPerSecond; 484 times[0] = st.st_atim;
444 return utime(name, &times) == 0; 485 MillisecondsToTimespec(millis, &times[1]);
486 return utimensat(ns.fd(), ns.path(), times, 0) == 0;
445 } 487 }
446 488
447 const char* File::LinkTarget(const char* pathname) { 489 const char* File::LinkTarget(Namespace* namespc, const char* name) {
448 struct stat link_stats; 490 NamespaceScope ns(namespc, name);
449 if (lstat(pathname, &link_stats) != 0) { 491 struct stat64 link_stats;
492 const int status = TEMP_FAILURE_RETRY(fstatat64(
493 ns.fd(), ns.path(), &link_stats, AT_SYMLINK_NOFOLLOW));
494 if (status != 0) {
450 return NULL; 495 return NULL;
451 } 496 }
452 if (!S_ISLNK(link_stats.st_mode)) { 497 if (!S_ISLNK(link_stats.st_mode)) {
453 errno = ENOENT; 498 errno = ENOENT;
454 return NULL; 499 return NULL;
455 } 500 }
456 size_t target_size = link_stats.st_size; 501 // Don't rely on the link_stats.st_size for the size of the link
502 // target. For some filesystems, e.g. procfs, this value is always
503 // 0. Also the link might have changed before the readlink call.
504 const int kBufferSize = PATH_MAX + 1;
505 char target[kBufferSize];
506 const int target_size =
507 TEMP_FAILURE_RETRY(readlinkat(ns.fd(), ns.path(), target, kBufferSize));
508 if (target_size <= 0) {
509 return NULL;
510 }
457 char* target_name = DartUtils::ScopedCString(target_size + 1); 511 char* target_name = DartUtils::ScopedCString(target_size + 1);
458 ASSERT(target_name != NULL); 512 ASSERT(target_name != NULL);
459 size_t read_size = readlink(pathname, target_name, target_size + 1); 513 memmove(target_name, target, target_size);
460 if (read_size != target_size) {
461 return NULL;
462 }
463 target_name[target_size] = '\0'; 514 target_name[target_size] = '\0';
464 return target_name; 515 return target_name;
465 } 516 }
466 517
467 bool File::IsAbsolutePath(const char* pathname) { 518 bool File::IsAbsolutePath(const char* pathname) {
468 return ((pathname != NULL) && (pathname[0] == '/')); 519 return ((pathname != NULL) && (pathname[0] == '/'));
469 } 520 }
470 521
471 const char* File::GetCanonicalPath(const char* pathname) { 522 const char* File::GetCanonicalPath(Namespace* namespc, const char* name) {
472 char* abs_path = NULL; 523 if (name == NULL) {
473 if (pathname != NULL) { 524 return NULL;
474 char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1);
475 ASSERT(resolved_path != NULL);
476 abs_path = realpath(pathname, resolved_path);
477 ASSERT((abs_path == NULL) || IsAbsolutePath(abs_path));
478 ASSERT((abs_path == NULL) || (abs_path == resolved_path));
479 } 525 }
526 if (!Namespace::IsDefault(namespc)) {
527 // TODO(zra): There is no realpathat(). Also chasing a symlink might result
528 // in a path to something outside of the namespace, so canonicalizing paths
529 // would have to be done carefully. For now, don't do anything.
530 return name;
531 }
532 char* abs_path;
533 char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1);
534 ASSERT(resolved_path != NULL);
535 do {
536 abs_path = realpath(name, resolved_path);
537 } while ((abs_path == NULL) && (errno == EINTR));
538 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path));
539 ASSERT(abs_path == NULL || (abs_path == resolved_path));
480 return abs_path; 540 return abs_path;
481 } 541 }
482 542
483 const char* File::PathSeparator() { 543 const char* File::PathSeparator() {
484 return "/"; 544 return "/";
485 } 545 }
486 546
487 const char* File::StringEscapedPathSeparator() { 547 const char* File::StringEscapedPathSeparator() {
488 return "/"; 548 return "/";
489 } 549 }
490 550
491 File::StdioHandleType File::GetStdioHandleType(int fd) { 551 File::StdioHandleType File::GetStdioHandleType(int fd) {
492 ASSERT((0 <= fd) && (fd <= 2)); 552 ASSERT((0 <= fd) && (fd <= 2));
493 struct stat buf; 553 struct stat64 buf;
494 int result = fstat(fd, &buf); 554 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf));
495 if (result == -1) { 555 if (result == -1) {
496 return kOther; 556 return kOther;
497 } 557 }
498 if (S_ISCHR(buf.st_mode)) { 558 if (S_ISCHR(buf.st_mode)) {
499 return kTerminal; 559 return kTerminal;
500 } 560 }
501 if (S_ISFIFO(buf.st_mode)) { 561 if (S_ISFIFO(buf.st_mode)) {
502 return kPipe; 562 return kPipe;
503 } 563 }
504 if (S_ISSOCK(buf.st_mode)) { 564 if (S_ISSOCK(buf.st_mode)) {
505 return kSocket; 565 return kSocket;
506 } 566 }
507 if (S_ISREG(buf.st_mode)) { 567 if (S_ISREG(buf.st_mode)) {
508 return kFile; 568 return kFile;
509 } 569 }
510 return kOther; 570 return kOther;
511 } 571 }
512 572
513 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { 573 File::Identical File::AreIdentical(
514 struct stat file_1_info; 574 Namespace* namespc, const char* file_1, const char* file_2) {
515 struct stat file_2_info; 575 NamespaceScope ns1(namespc, file_1);
516 if ((NO_RETRY_EXPECTED(lstat(file_1, &file_1_info)) == -1) || 576 NamespaceScope ns2(namespc, file_2);
517 (NO_RETRY_EXPECTED(lstat(file_2, &file_2_info)) == -1)) { 577 struct stat64 file_1_info;
578 struct stat64 file_2_info;
579 int status = TEMP_FAILURE_RETRY(fstatat64(
580 ns1.fd(), ns1.path(), &file_1_info, AT_SYMLINK_NOFOLLOW));
581 if (status == -1) {
582 return File::kError;
583 }
584 status = TEMP_FAILURE_RETRY(fstatat64(
585 ns2.fd(), ns2.path(), &file_2_info, AT_SYMLINK_NOFOLLOW));
586 if (status == -1) {
518 return File::kError; 587 return File::kError;
519 } 588 }
520 return ((file_1_info.st_ino == file_2_info.st_ino) && 589 return ((file_1_info.st_ino == file_2_info.st_ino) &&
521 (file_1_info.st_dev == file_2_info.st_dev)) 590 (file_1_info.st_dev == file_2_info.st_dev))
522 ? File::kIdentical 591 ? File::kIdentical
523 : File::kDifferent; 592 : File::kDifferent;
524 } 593 }
525 594
526 } // namespace bin 595 } // namespace bin
527 } // namespace dart 596 } // namespace dart
528 597
529 #endif // defined(HOST_OS_FUCHSIA) 598 #endif // defined(HOST_OS_FUCHSIA)
OLDNEW
« no previous file with comments | « runtime/bin/file_android.cc ('k') | runtime/bin/file_linux.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698