OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/files/file.h" | 5 #include "base/files/file.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <sys/stat.h> | 9 #include <sys/stat.h> |
10 #include <unistd.h> | 10 #include <unistd.h> |
(...skipping 14 matching lines...) Expand all Loading... | |
25 namespace base { | 25 namespace base { |
26 | 26 |
27 // Make sure our Whence mappings match the system headers. | 27 // Make sure our Whence mappings match the system headers. |
28 COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET && | 28 COMPILE_ASSERT(File::FROM_BEGIN == SEEK_SET && |
29 File::FROM_CURRENT == SEEK_CUR && | 29 File::FROM_CURRENT == SEEK_CUR && |
30 File::FROM_END == SEEK_END, whence_matches_system); | 30 File::FROM_END == SEEK_END, whence_matches_system); |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) | 34 #if defined(OS_BSD) || defined(OS_MACOSX) || defined(OS_NACL) |
35 typedef struct stat stat_wrapper_t; | |
36 static int CallFstat(int fd, stat_wrapper_t *sb) { | 35 static int CallFstat(int fd, stat_wrapper_t *sb) { |
37 base::ThreadRestrictions::AssertIOAllowed(); | 36 base::ThreadRestrictions::AssertIOAllowed(); |
38 return fstat(fd, sb); | 37 return fstat(fd, sb); |
39 } | 38 } |
40 #else | 39 #else |
41 typedef struct stat64 stat_wrapper_t; | |
42 static int CallFstat(int fd, stat_wrapper_t *sb) { | 40 static int CallFstat(int fd, stat_wrapper_t *sb) { |
43 base::ThreadRestrictions::AssertIOAllowed(); | 41 base::ThreadRestrictions::AssertIOAllowed(); |
44 return fstat64(fd, sb); | 42 return fstat64(fd, sb); |
45 } | 43 } |
46 #endif | 44 #endif |
47 | 45 |
48 // NaCl doesn't provide the following system calls, so either simulate them or | 46 // NaCl doesn't provide the following system calls, so either simulate them or |
49 // wrap them in order to minimize the number of #ifdef's in this file. | 47 // wrap them in order to minimize the number of #ifdef's in this file. |
50 #if !defined(OS_NACL) | 48 #if !defined(OS_NACL) |
51 static bool IsOpenAppend(PlatformFile file) { | 49 static bool IsOpenAppend(PlatformFile file) { |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
112 } | 110 } |
113 | 111 |
114 static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { | 112 static File::Error CallFctnlFlock(PlatformFile file, bool do_lock) { |
115 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. | 113 NOTIMPLEMENTED(); // NaCl doesn't implement flock struct. |
116 return File::FILE_ERROR_INVALID_OPERATION; | 114 return File::FILE_ERROR_INVALID_OPERATION; |
117 } | 115 } |
118 #endif // defined(OS_NACL) | 116 #endif // defined(OS_NACL) |
119 | 117 |
120 } // namespace | 118 } // namespace |
121 | 119 |
120 void File::Info::FromStat(const stat_wrapper_t& stat_info) { | |
121 is_directory = S_ISDIR(stat_info.st_mode); | |
122 is_symbolic_link = S_ISLNK(stat_info.st_mode); | |
123 size = stat_info.st_size; | |
124 | |
125 #if defined(OS_LINUX) | |
126 time_t last_modified_sec = stat_info.st_mtim.tv_sec; | |
127 int64 last_modified_nsec = stat_info.st_mtim.tv_nsec; | |
128 time_t last_accessed_sec = stat_info.st_atim.tv_sec; | |
129 int64 last_accessed_nsec = stat_info.st_atim.tv_nsec; | |
130 time_t creation_time_sec = stat_info.st_ctim.tv_sec; | |
131 int64 creation_time_nsec = stat_info.st_ctim.tv_nsec; | |
132 #elif defined(OS_ANDROID) | |
133 time_t last_modified_sec = stat_info.st_mtime; | |
134 int64 last_modified_nsec = stat_info.st_mtime_nsec; | |
135 time_t last_accessed_sec = stat_info.st_atime; | |
136 int64 last_accessed_nsec = stat_info.st_atime_nsec; | |
137 time_t creation_time_sec = stat_info.st_ctime; | |
138 int64 creation_time_nsec = stat_info.st_ctime_nsec; | |
139 #elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) | |
140 time_t last_modified_sec = stat_info.st_mtimespec.tv_sec; | |
141 int64 last_modified_nsec = stat_info.st_mtimespec.tv_nsec; | |
142 time_t last_accessed_sec = stat_info.st_atimespec.tv_sec; | |
143 int64 last_accessed_nsec = stat_info.st_atimespec.tv_nsec; | |
144 time_t creation_time_sec = stat_info.st_ctimespec.tv_sec; | |
145 int64 creation_time_nsec = stat_info.st_ctimespec.tv_nsec; | |
146 #else | |
147 // TODO(gavinp): Investigate a good high resolution option for OS_NACL. | |
nfullagar1
2014/03/28 19:40:15
couple of nits
- #else is for default, but commen
rvargas (doing something else)
2014/03/28 19:53:31
Sure. I was basically just moving this code :)
Eve
nfullagar1
2014/03/31 19:59:07
Talking to Roland NaCl should be zero'ing the nsec
rvargas (doing something else)
2014/03/31 23:02:28
Given that a default option is needed even if we a
| |
148 time_t last_modified_sec = stat_info.st_mtime; | |
149 int64 last_modified_nsec = 0; | |
150 time_t last_accessed_sec = stat_info.st_atime; | |
151 int64 last_accessed_nsec = 0; | |
152 time_t creation_time_sec = stat_info.st_ctime; | |
153 int64 creation_time_nsec = 0; | |
154 #endif | |
155 | |
156 last_modified = | |
157 Time::FromTimeT(last_modified_sec) + | |
158 TimeDelta::FromMicroseconds(last_modified_nsec / | |
159 Time::kNanosecondsPerMicrosecond); | |
160 | |
161 last_accessed = | |
162 Time::FromTimeT(last_accessed_sec) + | |
163 TimeDelta::FromMicroseconds(last_accessed_nsec / | |
164 Time::kNanosecondsPerMicrosecond); | |
165 | |
166 creation_time = | |
167 Time::FromTimeT(creation_time_sec) + | |
168 TimeDelta::FromMicroseconds(creation_time_nsec / | |
169 Time::kNanosecondsPerMicrosecond); | |
170 } | |
171 | |
122 // NaCl doesn't implement system calls to open files directly. | 172 // NaCl doesn't implement system calls to open files directly. |
123 #if !defined(OS_NACL) | 173 #if !defined(OS_NACL) |
124 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? | 174 // TODO(erikkay): does it make sense to support FLAG_EXCLUSIVE_* here? |
125 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { | 175 void File::InitializeUnsafe(const FilePath& name, uint32 flags) { |
126 base::ThreadRestrictions::AssertIOAllowed(); | 176 base::ThreadRestrictions::AssertIOAllowed(); |
127 DCHECK(!IsValid()); | 177 DCHECK(!IsValid()); |
128 | 178 |
129 int open_flags = 0; | 179 int open_flags = 0; |
130 if (flags & FLAG_CREATE) | 180 if (flags & FLAG_CREATE) |
131 open_flags = O_CREAT | O_EXCL; | 181 open_flags = O_CREAT | O_EXCL; |
(...skipping 245 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
377 return !CallFutimes(file_.get(), times); | 427 return !CallFutimes(file_.get(), times); |
378 } | 428 } |
379 | 429 |
380 bool File::GetInfo(Info* info) { | 430 bool File::GetInfo(Info* info) { |
381 DCHECK(IsValid()); | 431 DCHECK(IsValid()); |
382 | 432 |
383 stat_wrapper_t file_info; | 433 stat_wrapper_t file_info; |
384 if (CallFstat(file_.get(), &file_info)) | 434 if (CallFstat(file_.get(), &file_info)) |
385 return false; | 435 return false; |
386 | 436 |
387 info->is_directory = S_ISDIR(file_info.st_mode); | 437 info->FromStat(file_info); |
388 info->is_symbolic_link = S_ISLNK(file_info.st_mode); | |
389 info->size = file_info.st_size; | |
390 | |
391 #if defined(OS_LINUX) | |
392 const time_t last_modified_sec = file_info.st_mtim.tv_sec; | |
393 const int64 last_modified_nsec = file_info.st_mtim.tv_nsec; | |
394 const time_t last_accessed_sec = file_info.st_atim.tv_sec; | |
395 const int64 last_accessed_nsec = file_info.st_atim.tv_nsec; | |
396 const time_t creation_time_sec = file_info.st_ctim.tv_sec; | |
397 const int64 creation_time_nsec = file_info.st_ctim.tv_nsec; | |
398 #elif defined(OS_ANDROID) | |
399 const time_t last_modified_sec = file_info.st_mtime; | |
400 const int64 last_modified_nsec = file_info.st_mtime_nsec; | |
401 const time_t last_accessed_sec = file_info.st_atime; | |
402 const int64 last_accessed_nsec = file_info.st_atime_nsec; | |
403 const time_t creation_time_sec = file_info.st_ctime; | |
404 const int64 creation_time_nsec = file_info.st_ctime_nsec; | |
405 #elif defined(OS_MACOSX) || defined(OS_IOS) || defined(OS_BSD) | |
406 const time_t last_modified_sec = file_info.st_mtimespec.tv_sec; | |
407 const int64 last_modified_nsec = file_info.st_mtimespec.tv_nsec; | |
408 const time_t last_accessed_sec = file_info.st_atimespec.tv_sec; | |
409 const int64 last_accessed_nsec = file_info.st_atimespec.tv_nsec; | |
410 const time_t creation_time_sec = file_info.st_ctimespec.tv_sec; | |
411 const int64 creation_time_nsec = file_info.st_ctimespec.tv_nsec; | |
412 #else | |
413 // TODO(gavinp): Investigate a good high resolution option for OS_NACL. | |
414 const time_t last_modified_sec = file_info.st_mtime; | |
415 const int64 last_modified_nsec = 0; | |
416 const time_t last_accessed_sec = file_info.st_atime; | |
417 const int64 last_accessed_nsec = 0; | |
418 const time_t creation_time_sec = file_info.st_ctime; | |
419 const int64 creation_time_nsec = 0; | |
420 #endif | |
421 | |
422 info->last_modified = | |
423 base::Time::FromTimeT(last_modified_sec) + | |
424 base::TimeDelta::FromMicroseconds(last_modified_nsec / | |
425 base::Time::kNanosecondsPerMicrosecond); | |
426 info->last_accessed = | |
427 base::Time::FromTimeT(last_accessed_sec) + | |
428 base::TimeDelta::FromMicroseconds(last_accessed_nsec / | |
429 base::Time::kNanosecondsPerMicrosecond); | |
430 info->creation_time = | |
431 base::Time::FromTimeT(creation_time_sec) + | |
432 base::TimeDelta::FromMicroseconds(creation_time_nsec / | |
433 base::Time::kNanosecondsPerMicrosecond); | |
434 return true; | 438 return true; |
435 } | 439 } |
436 | 440 |
437 File::Error File::Lock() { | 441 File::Error File::Lock() { |
438 return CallFctnlFlock(file_.get(), true); | 442 return CallFctnlFlock(file_.get(), true); |
439 } | 443 } |
440 | 444 |
441 File::Error File::Unlock() { | 445 File::Error File::Unlock() { |
442 return CallFctnlFlock(file_.get(), false); | 446 return CallFctnlFlock(file_.get(), false); |
443 } | 447 } |
(...skipping 30 matching lines...) Expand all Loading... | |
474 return FILE_ERROR_FAILED; | 478 return FILE_ERROR_FAILED; |
475 } | 479 } |
476 } | 480 } |
477 | 481 |
478 void File::SetPlatformFile(PlatformFile file) { | 482 void File::SetPlatformFile(PlatformFile file) { |
479 DCHECK(!file_.is_valid()); | 483 DCHECK(!file_.is_valid()); |
480 file_.reset(file); | 484 file_.reset(file); |
481 } | 485 } |
482 | 486 |
483 } // namespace base | 487 } // namespace base |
OLD | NEW |