OLD | NEW |
---|---|
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/file_util.h" | 5 #include "base/file_util.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <fnmatch.h> | 9 #include <fnmatch.h> |
10 #include <fts.h> | 10 #include <fts.h> |
11 #include <libgen.h> | 11 #include <libgen.h> |
12 #include <stdio.h> | 12 #include <stdio.h> |
13 #include <string.h> | 13 #include <string.h> |
14 #include <sys/errno.h> | 14 #include <sys/errno.h> |
15 #include <sys/mman.h> | |
15 #include <sys/stat.h> | 16 #include <sys/stat.h> |
16 #include <time.h> | 17 #include <time.h> |
17 | 18 |
18 #include <fstream> | 19 #include <fstream> |
19 | 20 |
20 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
21 #include "base/file_path.h" | 22 #include "base/file_path.h" |
22 #include "base/logging.h" | 23 #include "base/logging.h" |
23 #include "base/string_util.h" | 24 #include "base/string_util.h" |
24 | 25 |
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
371 *dir = FilePath(system_buffer); | 372 *dir = FilePath(system_buffer); |
372 return true; | 373 return true; |
373 } | 374 } |
374 | 375 |
375 // Sets the current working directory for the process. | 376 // Sets the current working directory for the process. |
376 bool SetCurrentDirectory(const FilePath& path) { | 377 bool SetCurrentDirectory(const FilePath& path) { |
377 int ret = chdir(path.value().c_str()); | 378 int ret = chdir(path.value().c_str()); |
378 return !ret; | 379 return !ret; |
379 } | 380 } |
380 | 381 |
382 /////////////////////////////////////////////// | |
383 // FileEnumerator | |
384 | |
381 FileEnumerator::FileEnumerator(const FilePath& root_path, | 385 FileEnumerator::FileEnumerator(const FilePath& root_path, |
382 bool recursive, | 386 bool recursive, |
383 FileEnumerator::FILE_TYPE file_type) | 387 FileEnumerator::FILE_TYPE file_type) |
384 : recursive_(recursive), | 388 : recursive_(recursive), |
385 file_type_(file_type), | 389 file_type_(file_type), |
386 is_in_find_op_(false), | 390 is_in_find_op_(false), |
387 fts_(NULL) { | 391 fts_(NULL) { |
388 pending_paths_.push(root_path); | 392 pending_paths_.push(root_path); |
389 } | 393 } |
390 | 394 |
(...skipping 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
472 if (!recursive_) | 476 if (!recursive_) |
473 fts_set(fts_, fts_ent_, FTS_SKIP); | 477 fts_set(fts_, fts_ent_, FTS_SKIP); |
474 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); | 478 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); |
475 } else if (fts_ent_->fts_info == FTS_F) { | 479 } else if (fts_ent_->fts_info == FTS_F) { |
476 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); | 480 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); |
477 } | 481 } |
478 // TODO(erikkay) - verify that the other fts_info types aren't interesting | 482 // TODO(erikkay) - verify that the other fts_info types aren't interesting |
479 return Next(); | 483 return Next(); |
480 } | 484 } |
481 | 485 |
486 /////////////////////////////////////////////// | |
487 // MemoryMappedFile | |
488 | |
489 MemoryMappedFile::MemoryMappedFile() | |
490 : file_(-1), | |
491 data_(NULL), | |
492 length_(0) { | |
493 } | |
494 | |
495 bool MemoryMappedFile::MapFileToMemory(const FilePath& file_name) { | |
496 file_ = open(file_name.value().c_str(), O_RDONLY); | |
497 if (file_ == -1) | |
498 return false; | |
499 | |
500 struct stat file_stat; | |
501 if (fstat(file_, &file_stat) == -1) | |
502 return false; | |
503 length_ = file_stat.st_size; | |
504 | |
505 data_ = static_cast<uint8*>(mmap(NULL, length_, PROT_READ, MAP_SHARED, | |
Peter Kasting
2008/12/12 21:27:49
Super-tiny nit: It'd be nice to make the wrapping
| |
506 file_, 0)); | |
507 if (data_ == MAP_FAILED) | |
508 data_ = NULL; | |
509 return data_ != NULL; | |
510 } | |
511 | |
512 void MemoryMappedFile::CloseHandles() { | |
513 if (data_ != NULL) | |
514 munmap(data_, length_); | |
515 if (file_ != -1) | |
516 close(file_); | |
517 | |
518 data_ = NULL; | |
519 length_ = 0; | |
520 file_ = -1; | |
521 } | |
482 | 522 |
483 } // namespace file_util | 523 } // namespace file_util |
OLD | NEW |