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

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

Issue 3007703002: [dart:io] Namespaces for file IO (Closed)
Patch Set: Remove namespace_unsupported.cc 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(
236 openat64(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,
253 return NO_RETRY_EXPECTED(symlink(target, name)) == 0; 259 const char* name,
260 const char* target) {
261 NamespaceScope ns(namespc, name);
262 return NO_RETRY_EXPECTED(symlinkat(target, ns.fd(), ns.path())) == 0;
254 } 263 }
255 264
256 File::Type File::GetType(const char* pathname, bool follow_links) { 265 File::Type File::GetType(Namespace* namespc,
257 struct stat entry_info; 266 const char* name,
267 bool follow_links) {
268 NamespaceScope ns(namespc, name);
269 struct stat64 entry_info;
258 int stat_success; 270 int stat_success;
259 if (follow_links) { 271 if (follow_links) {
260 stat_success = NO_RETRY_EXPECTED(stat(pathname, &entry_info)); 272 stat_success =
273 TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &entry_info, 0));
261 } else { 274 } else {
262 stat_success = NO_RETRY_EXPECTED(lstat(pathname, &entry_info)); 275 stat_success = TEMP_FAILURE_RETRY(
276 fstatat64(ns.fd(), ns.path(), &entry_info, AT_SYMLINK_NOFOLLOW));
263 } 277 }
264 if (stat_success == -1) { 278 if (stat_success == -1) {
265 return File::kDoesNotExist; 279 return File::kDoesNotExist;
266 } 280 }
267 if (S_ISDIR(entry_info.st_mode)) { 281 if (S_ISDIR(entry_info.st_mode)) {
268 return File::kIsDirectory; 282 return File::kIsDirectory;
269 } 283 }
270 if (S_ISREG(entry_info.st_mode)) { 284 if (S_ISREG(entry_info.st_mode)) {
271 return File::kIsFile; 285 return File::kIsFile;
272 } 286 }
273 if (S_ISLNK(entry_info.st_mode)) { 287 if (S_ISLNK(entry_info.st_mode)) {
274 return File::kIsLink; 288 return File::kIsLink;
275 } 289 }
276 return File::kDoesNotExist; 290 return File::kDoesNotExist;
277 } 291 }
278 292
279 static bool CheckTypeAndSetErrno(const char* name, 293 static bool CheckTypeAndSetErrno(Namespace* namespc,
294 const char* name,
280 File::Type expected, 295 File::Type expected,
281 bool follow_links) { 296 bool follow_links) {
282 File::Type actual = File::GetType(name, follow_links); 297 File::Type actual = File::GetType(namespc, name, follow_links);
283 if (actual == expected) { 298 if (actual == expected) {
284 return true; 299 return true;
285 } 300 }
286 switch (actual) { 301 switch (actual) {
287 case File::kIsDirectory: 302 case File::kIsDirectory:
288 errno = EISDIR; 303 errno = EISDIR;
289 break; 304 break;
290 case File::kDoesNotExist: 305 case File::kDoesNotExist:
291 errno = ENOENT; 306 errno = ENOENT;
292 break; 307 break;
293 default: 308 default:
294 errno = EINVAL; 309 errno = EINVAL;
295 break; 310 break;
296 } 311 }
297 return false; 312 return false;
298 } 313 }
299 314
300 bool File::Delete(const char* name) { 315 bool File::Delete(Namespace* namespc, const char* name) {
301 return CheckTypeAndSetErrno(name, kIsFile, true) && 316 NamespaceScope ns(namespc, name);
302 (NO_RETRY_EXPECTED(unlink(name)) == 0); 317 return CheckTypeAndSetErrno(namespc, name, kIsFile, true) &&
318 (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
303 } 319 }
304 320
305 bool File::DeleteLink(const char* name) { 321 bool File::DeleteLink(Namespace* namespc, const char* name) {
306 return CheckTypeAndSetErrno(name, kIsLink, false) && 322 NamespaceScope ns(namespc, name);
307 (NO_RETRY_EXPECTED(unlink(name)) == 0); 323 return CheckTypeAndSetErrno(namespc, name, kIsLink, false) &&
324 (NO_RETRY_EXPECTED(unlinkat(ns.fd(), ns.path(), 0)) == 0);
308 } 325 }
309 326
310 bool File::Rename(const char* old_path, const char* new_path) { 327 bool File::Rename(Namespace* namespc,
311 return CheckTypeAndSetErrno(old_path, kIsFile, true) && 328 const char* old_path,
312 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); 329 const char* new_path) {
330 NamespaceScope oldns(namespc, old_path);
331 NamespaceScope newns(namespc, new_path);
332 return CheckTypeAndSetErrno(namespc, old_path, kIsFile, true) &&
333 (NO_RETRY_EXPECTED(renameat(oldns.fd(), oldns.path(), newns.fd(),
334 newns.path())) == 0);
313 } 335 }
314 336
315 bool File::RenameLink(const char* old_path, const char* new_path) { 337 bool File::RenameLink(Namespace* namespc,
316 return CheckTypeAndSetErrno(old_path, kIsLink, false) && 338 const char* old_path,
317 (NO_RETRY_EXPECTED(rename(old_path, new_path)) == 0); 339 const char* new_path) {
340 NamespaceScope oldns(namespc, old_path);
341 NamespaceScope newns(namespc, new_path);
342 return CheckTypeAndSetErrno(namespc, old_path, kIsLink, false) &&
343 (NO_RETRY_EXPECTED(renameat(oldns.fd(), oldns.path(), newns.fd(),
344 newns.path())) == 0);
318 } 345 }
319 346
320 bool File::Copy(const char* old_path, const char* new_path) { 347 bool File::Copy(Namespace* namespc,
321 if (!CheckTypeAndSetErrno(old_path, kIsFile, true)) { 348 const char* old_path,
349 const char* new_path) {
350 if (!CheckTypeAndSetErrno(namespc, old_path, kIsFile, true)) {
322 return false; 351 return false;
323 } 352 }
353 NamespaceScope oldns(namespc, old_path);
324 struct stat64 st; 354 struct stat64 st;
325 if (NO_RETRY_EXPECTED(stat64(old_path, &st)) != 0) { 355 if (NO_RETRY_EXPECTED(fstatat64(oldns.fd(), oldns.path(), &st, 0)) != 0) {
326 return false; 356 return false;
327 } 357 }
328 int old_fd = NO_RETRY_EXPECTED(open64(old_path, O_RDONLY | O_CLOEXEC)); 358 const int old_fd = NO_RETRY_EXPECTED(
359 openat64(oldns.fd(), oldns.path(), O_RDONLY | O_CLOEXEC));
329 if (old_fd < 0) { 360 if (old_fd < 0) {
330 return false; 361 return false;
331 } 362 }
332 int new_fd = NO_RETRY_EXPECTED( 363 NamespaceScope newns(namespc, new_path);
333 open64(new_path, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode)); 364 const int new_fd = NO_RETRY_EXPECTED(
365 openat64(newns.fd(), newns.path(),
366 O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, st.st_mode));
334 if (new_fd < 0) { 367 if (new_fd < 0) {
335 VOID_TEMP_FAILURE_RETRY(close(old_fd)); 368 VOID_TEMP_FAILURE_RETRY(close(old_fd));
336 return false; 369 return false;
337 } 370 }
338 // TODO(MG-429): Use sendfile/copyfile or equivalent when there is one. 371 // TODO(MG-429): Use sendfile/copyfile or equivalent when there is one.
339 intptr_t result; 372 intptr_t result;
340 const intptr_t kBufferSize = 8 * KB; 373 const intptr_t kBufferSize = 8 * KB;
341 uint8_t buffer[kBufferSize]; 374 uint8_t buffer[kBufferSize];
342 while ((result = NO_RETRY_EXPECTED(read(old_fd, buffer, kBufferSize))) > 0) { 375 while ((result = NO_RETRY_EXPECTED(read(old_fd, buffer, kBufferSize))) > 0) {
343 int wrote = NO_RETRY_EXPECTED(write(new_fd, buffer, result)); 376 int wrote = NO_RETRY_EXPECTED(write(new_fd, buffer, result));
344 if (wrote != result) { 377 if (wrote != result) {
345 result = -1; 378 result = -1;
346 break; 379 break;
347 } 380 }
348 } 381 }
349 FDUtils::SaveErrorAndClose(old_fd); 382 FDUtils::SaveErrorAndClose(old_fd);
350 FDUtils::SaveErrorAndClose(new_fd); 383 FDUtils::SaveErrorAndClose(new_fd);
351 if (result < 0) { 384 if (result < 0) {
352 int e = errno; 385 int e = errno;
353 VOID_NO_RETRY_EXPECTED(unlink(new_path)); 386 VOID_NO_RETRY_EXPECTED(unlinkat(newns.fd(), newns.path(), 0));
354 errno = e; 387 errno = e;
355 return false; 388 return false;
356 } 389 }
357 return true; 390 return true;
358 } 391 }
359 392
360 static bool StatHelper(const char* name, struct stat64* st) { 393 static bool StatHelper(Namespace* namespc,
361 if (NO_RETRY_EXPECTED(stat64(name, st)) != 0) { 394 const char* name,
395 struct stat64* st) {
396 NamespaceScope ns(namespc, name);
397 if (NO_RETRY_EXPECTED(fstatat64(ns.fd(), ns.path(), st, 0)) != 0) {
362 return false; 398 return false;
363 } 399 }
364 // Signal an error if it's a directory. 400 // Signal an error if it's a directory.
365 if (S_ISDIR(st->st_mode)) { 401 if (S_ISDIR(st->st_mode)) {
366 errno = EISDIR; 402 errno = EISDIR;
367 return false; 403 return false;
368 } 404 }
369 // Otherwise assume the caller knows what it's doing. 405 // Otherwise assume the caller knows what it's doing.
370 return true; 406 return true;
371 } 407 }
372 408
373 int64_t File::LengthFromPath(const char* name) { 409 int64_t File::LengthFromPath(Namespace* namespc, const char* name) {
374 struct stat64 st; 410 struct stat64 st;
375 if (!StatHelper(name, &st)) { 411 if (!StatHelper(namespc, name, &st)) {
376 return -1; 412 return -1;
377 } 413 }
378 return st.st_size; 414 return st.st_size;
379 } 415 }
380 416
381 void File::Stat(const char* name, int64_t* data) { 417 static int64_t TimespecToMilliseconds(const struct timespec& t) {
382 struct stat st; 418 return static_cast<int64_t>(t.tv_sec) * 1000L +
383 if (NO_RETRY_EXPECTED(stat(name, &st)) == 0) { 419 static_cast<int64_t>(t.tv_nsec) / 1000000L;
420 }
421
422 static void MillisecondsToTimespec(int64_t millis, struct timespec* t) {
423 ASSERT(t != NULL);
424 t->tv_sec = millis / kMillisecondsPerSecond;
425 t->tv_nsec = (millis - (t->tv_sec * kMillisecondsPerSecond)) * 1000L;
426 }
427
428 void File::Stat(Namespace* namespc, const char* name, int64_t* data) {
429 NamespaceScope ns(namespc, name);
430 struct stat64 st;
431 if (TEMP_FAILURE_RETRY(fstatat64(ns.fd(), ns.path(), &st, 0)) == 0) {
384 if (S_ISREG(st.st_mode)) { 432 if (S_ISREG(st.st_mode)) {
385 data[kType] = kIsFile; 433 data[kType] = kIsFile;
386 } else if (S_ISDIR(st.st_mode)) { 434 } else if (S_ISDIR(st.st_mode)) {
387 data[kType] = kIsDirectory; 435 data[kType] = kIsDirectory;
388 } else if (S_ISLNK(st.st_mode)) { 436 } else if (S_ISLNK(st.st_mode)) {
389 data[kType] = kIsLink; 437 data[kType] = kIsLink;
390 } else { 438 } else {
391 data[kType] = kDoesNotExist; 439 data[kType] = kDoesNotExist;
392 } 440 }
393 data[kCreatedTime] = static_cast<int64_t>(st.st_ctime) * 1000; 441 data[kCreatedTime] = TimespecToMilliseconds(st.st_ctim);
394 data[kModifiedTime] = static_cast<int64_t>(st.st_mtime) * 1000; 442 data[kModifiedTime] = TimespecToMilliseconds(st.st_mtim);
395 data[kAccessedTime] = static_cast<int64_t>(st.st_atime) * 1000; 443 data[kAccessedTime] = TimespecToMilliseconds(st.st_atim);
396 data[kMode] = st.st_mode; 444 data[kMode] = st.st_mode;
397 data[kSize] = st.st_size; 445 data[kSize] = st.st_size;
398 } else { 446 } else {
399 data[kType] = kDoesNotExist; 447 data[kType] = kDoesNotExist;
400 } 448 }
401 } 449 }
402 450
403 time_t File::LastModified(const char* name) { 451 time_t File::LastModified(Namespace* namespc, const char* name) {
404 struct stat st; 452 struct stat64 st;
405 if (!StatHelper(name, &st)) { 453 if (!StatHelper(namespc, name, &st)) {
406 return -1; 454 return -1;
407 } 455 }
408 return st.st_mtime; 456 return st.st_mtime;
409 } 457 }
410 458
411 time_t File::LastAccessed(const char* name) { 459 time_t File::LastAccessed(Namespace* namespc, const char* name) {
412 struct stat st; 460 struct stat64 st;
413 if (!StatHelper(name, &st)) { 461 if (!StatHelper(namespc, name, &st)) {
414 return -1; 462 return -1;
415 } 463 }
416 return st.st_atime; 464 return st.st_atime;
417 } 465 }
418 466
419 bool File::SetLastAccessed(const char* name, int64_t millis) { 467 bool File::SetLastAccessed(Namespace* namespc,
468 const char* name,
469 int64_t millis) {
420 // First get the current times. 470 // First get the current times.
421 struct stat st; 471 struct stat64 st;
422 if (!StatHelper(name, &st)) { 472 if (!StatHelper(namespc, name, &st)) {
423 return false; 473 return false;
424 } 474 }
425 475
426 // Set the new time: 476 // Set the new time:
427 struct utimbuf times; 477 NamespaceScope ns(namespc, name);
428 times.actime = millis / kMillisecondsPerSecond; 478 struct timespec times[2];
429 times.modtime = st.st_mtime; 479 MillisecondsToTimespec(millis, &times[0]);
430 return utime(name, &times) == 0; 480 times[1] = st.st_mtim;
481 return utimensat(ns.fd(), ns.path(), times, 0) == 0;
431 } 482 }
432 483
433 bool File::SetLastModified(const char* name, int64_t millis) { 484 bool File::SetLastModified(Namespace* namespc,
485 const char* name,
486 int64_t millis) {
434 // First get the current times. 487 // First get the current times.
435 struct stat st; 488 struct stat64 st;
436 if (!StatHelper(name, &st)) { 489 if (!StatHelper(namespc, name, &st)) {
437 return false; 490 return false;
438 } 491 }
439 492
440 // Set the new time: 493 // Set the new time:
441 struct utimbuf times; 494 NamespaceScope ns(namespc, name);
442 times.actime = st.st_atime; 495 struct timespec times[2];
443 times.modtime = millis / kMillisecondsPerSecond; 496 times[0] = st.st_atim;
444 return utime(name, &times) == 0; 497 MillisecondsToTimespec(millis, &times[1]);
498 return utimensat(ns.fd(), ns.path(), times, 0) == 0;
445 } 499 }
446 500
447 const char* File::LinkTarget(const char* pathname) { 501 const char* File::LinkTarget(Namespace* namespc, const char* name) {
448 struct stat link_stats; 502 NamespaceScope ns(namespc, name);
449 if (lstat(pathname, &link_stats) != 0) { 503 struct stat64 link_stats;
504 const int status = TEMP_FAILURE_RETRY(
505 fstatat64(ns.fd(), ns.path(), &link_stats, AT_SYMLINK_NOFOLLOW));
506 if (status != 0) {
450 return NULL; 507 return NULL;
451 } 508 }
452 if (!S_ISLNK(link_stats.st_mode)) { 509 if (!S_ISLNK(link_stats.st_mode)) {
453 errno = ENOENT; 510 errno = ENOENT;
454 return NULL; 511 return NULL;
455 } 512 }
456 size_t target_size = link_stats.st_size; 513 // Don't rely on the link_stats.st_size for the size of the link
514 // target. For some filesystems, e.g. procfs, this value is always
515 // 0. Also the link might have changed before the readlink call.
516 const int kBufferSize = PATH_MAX + 1;
517 char target[kBufferSize];
518 const int target_size =
519 TEMP_FAILURE_RETRY(readlinkat(ns.fd(), ns.path(), target, kBufferSize));
520 if (target_size <= 0) {
521 return NULL;
522 }
457 char* target_name = DartUtils::ScopedCString(target_size + 1); 523 char* target_name = DartUtils::ScopedCString(target_size + 1);
458 ASSERT(target_name != NULL); 524 ASSERT(target_name != NULL);
459 size_t read_size = readlink(pathname, target_name, target_size + 1); 525 memmove(target_name, target, target_size);
460 if (read_size != target_size) {
461 return NULL;
462 }
463 target_name[target_size] = '\0'; 526 target_name[target_size] = '\0';
464 return target_name; 527 return target_name;
465 } 528 }
466 529
467 bool File::IsAbsolutePath(const char* pathname) { 530 bool File::IsAbsolutePath(const char* pathname) {
468 return ((pathname != NULL) && (pathname[0] == '/')); 531 return ((pathname != NULL) && (pathname[0] == '/'));
469 } 532 }
470 533
471 const char* File::GetCanonicalPath(const char* pathname) { 534 const char* File::GetCanonicalPath(Namespace* namespc, const char* name) {
472 char* abs_path = NULL; 535 if (name == NULL) {
473 if (pathname != NULL) { 536 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 } 537 }
538 if (!Namespace::IsDefault(namespc)) {
539 // TODO(zra): There is no realpathat(). Also chasing a symlink might result
540 // in a path to something outside of the namespace, so canonicalizing paths
541 // would have to be done carefully. For now, don't do anything.
542 return name;
543 }
544 char* abs_path;
545 char* resolved_path = DartUtils::ScopedCString(PATH_MAX + 1);
546 ASSERT(resolved_path != NULL);
547 do {
548 abs_path = realpath(name, resolved_path);
549 } while ((abs_path == NULL) && (errno == EINTR));
550 ASSERT(abs_path == NULL || IsAbsolutePath(abs_path));
551 ASSERT(abs_path == NULL || (abs_path == resolved_path));
480 return abs_path; 552 return abs_path;
481 } 553 }
482 554
483 const char* File::PathSeparator() { 555 const char* File::PathSeparator() {
484 return "/"; 556 return "/";
485 } 557 }
486 558
487 const char* File::StringEscapedPathSeparator() { 559 const char* File::StringEscapedPathSeparator() {
488 return "/"; 560 return "/";
489 } 561 }
490 562
491 File::StdioHandleType File::GetStdioHandleType(int fd) { 563 File::StdioHandleType File::GetStdioHandleType(int fd) {
492 ASSERT((0 <= fd) && (fd <= 2)); 564 ASSERT((0 <= fd) && (fd <= 2));
493 struct stat buf; 565 struct stat64 buf;
494 int result = fstat(fd, &buf); 566 int result = TEMP_FAILURE_RETRY(fstat64(fd, &buf));
495 if (result == -1) { 567 if (result == -1) {
496 return kOther; 568 return kOther;
497 } 569 }
498 if (S_ISCHR(buf.st_mode)) { 570 if (S_ISCHR(buf.st_mode)) {
499 return kTerminal; 571 return kTerminal;
500 } 572 }
501 if (S_ISFIFO(buf.st_mode)) { 573 if (S_ISFIFO(buf.st_mode)) {
502 return kPipe; 574 return kPipe;
503 } 575 }
504 if (S_ISSOCK(buf.st_mode)) { 576 if (S_ISSOCK(buf.st_mode)) {
505 return kSocket; 577 return kSocket;
506 } 578 }
507 if (S_ISREG(buf.st_mode)) { 579 if (S_ISREG(buf.st_mode)) {
508 return kFile; 580 return kFile;
509 } 581 }
510 return kOther; 582 return kOther;
511 } 583 }
512 584
513 File::Identical File::AreIdentical(const char* file_1, const char* file_2) { 585 File::Identical File::AreIdentical(Namespace* namespc,
514 struct stat file_1_info; 586 const char* file_1,
515 struct stat file_2_info; 587 const char* file_2) {
516 if ((NO_RETRY_EXPECTED(lstat(file_1, &file_1_info)) == -1) || 588 NamespaceScope ns1(namespc, file_1);
517 (NO_RETRY_EXPECTED(lstat(file_2, &file_2_info)) == -1)) { 589 NamespaceScope ns2(namespc, file_2);
590 struct stat64 file_1_info;
591 struct stat64 file_2_info;
592 int status = TEMP_FAILURE_RETRY(
593 fstatat64(ns1.fd(), ns1.path(), &file_1_info, AT_SYMLINK_NOFOLLOW));
594 if (status == -1) {
595 return File::kError;
596 }
597 status = TEMP_FAILURE_RETRY(
598 fstatat64(ns2.fd(), ns2.path(), &file_2_info, AT_SYMLINK_NOFOLLOW));
599 if (status == -1) {
518 return File::kError; 600 return File::kError;
519 } 601 }
520 return ((file_1_info.st_ino == file_2_info.st_ino) && 602 return ((file_1_info.st_ino == file_2_info.st_ino) &&
521 (file_1_info.st_dev == file_2_info.st_dev)) 603 (file_1_info.st_dev == file_2_info.st_dev))
522 ? File::kIdentical 604 ? File::kIdentical
523 : File::kDifferent; 605 : File::kDifferent;
524 } 606 }
525 607
526 } // namespace bin 608 } // namespace bin
527 } // namespace dart 609 } // namespace dart
528 610
529 #endif // defined(HOST_OS_FUCHSIA) 611 #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