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

Side by Side Diff: base/file_util_posix.cc

Issue 11208: Implement some missing file util functions. (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: Created 12 years, 1 month 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 | Annotate | Revision Log
« no previous file with comments | « base/file_util_linux.cc ('k') | base/file_util_win.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) 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>
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 bool GetFileInfo(const FilePath& file_path, FileInfo* results) { 315 bool GetFileInfo(const FilePath& file_path, FileInfo* results) {
316 struct stat64 file_info; 316 struct stat64 file_info;
317 if (stat64(file_path.value().c_str(), &file_info) != 0) 317 if (stat64(file_path.value().c_str(), &file_info) != 0)
318 return false; 318 return false;
319 results->is_directory = S_ISDIR(file_info.st_mode); 319 results->is_directory = S_ISDIR(file_info.st_mode);
320 results->size = file_info.st_size; 320 results->size = file_info.st_size;
321 return true; 321 return true;
322 } 322 }
323 323
324 FILE* OpenFile(const std::string& filename, const char* mode) { 324 FILE* OpenFile(const std::string& filename, const char* mode) {
325 return fopen(filename.c_str(), mode); 325 return OpenFile(FilePath(filename), mode);
326 } 326 }
327 327
328 FILE* OpenFile(const std::wstring& filename, const char* mode) { 328 FILE* OpenFile(const FilePath& filename, const char* mode) {
329 return fopen(WideToUTF8(filename).c_str(), mode); 329 return fopen(filename.value().c_str(), mode);
330 } 330 }
331 331
332 int ReadFile(const std::wstring& filename, char* data, int size) { 332 int ReadFile(const std::wstring& filename, char* data, int size) {
333 int fd = open(WideToUTF8(filename).c_str(), O_RDONLY); 333 int fd = open(WideToUTF8(filename).c_str(), O_RDONLY);
334 if (fd < 0) 334 if (fd < 0)
335 return -1; 335 return -1;
336 336
337 int ret_value = read(fd, data, size); 337 int ret_value = read(fd, data, size);
338 close(fd); 338 close(fd);
339 return ret_value; 339 return ret_value;
340 } 340 }
341 341
342 int WriteFile(const std::wstring& filename, const char* data, int size) { 342 int WriteFile(const std::wstring& filename, const char* data, int size) {
343 int fd = creat(WideToUTF8(filename).c_str(), 0666); 343 int fd = creat(WideToUTF8(filename).c_str(), 0666);
344 if (fd < 0) 344 if (fd < 0)
345 return -1; 345 return -1;
346 346
347 // Allow for partial writes 347 // Allow for partial writes
348 ssize_t bytes_written_total = 0; 348 ssize_t bytes_written_total = 0;
349 do { 349 do {
350 ssize_t bytes_written_partial = write(fd, 350 ssize_t bytes_written_partial = write(fd,
351 data + bytes_written_total, 351 data + bytes_written_total,
352 size - bytes_written_total); 352 size - bytes_written_total);
353 if (bytes_written_partial < 0) { 353 if (bytes_written_partial < 0) {
354 close(fd); 354 close(fd);
355 return -1; 355 return -1;
356 } 356 }
357 bytes_written_total += bytes_written_partial; 357 bytes_written_total += bytes_written_partial;
358 } while (bytes_written_total < size); 358 } while (bytes_written_total < size);
359 359
360 close(fd); 360 close(fd);
361 return bytes_written_total; 361 return bytes_written_total;
362 } 362 }
363 363
364 // Gets the current working directory for the process. 364 // Gets the current working directory for the process.
365 bool GetCurrentDirectory(FilePath* dir) { 365 bool GetCurrentDirectory(FilePath* dir) {
366 char system_buffer[PATH_MAX] = ""; 366 char system_buffer[PATH_MAX] = "";
367 if (!getcwd(system_buffer, sizeof(system_buffer))) { 367 if (!getcwd(system_buffer, sizeof(system_buffer))) {
368 NOTREACHED(); 368 NOTREACHED();
369 return false; 369 return false;
370 } 370 }
371 *dir = FilePath(system_buffer); 371 *dir = FilePath(system_buffer);
372 return true; 372 return true;
373 } 373 }
374 374
375 // Sets the current working directory for the process. 375 // Sets the current working directory for the process.
376 bool SetCurrentDirectory(const std::wstring& current_directory) { 376 bool SetCurrentDirectory(const FilePath& path) {
377 int ret = chdir(WideToUTF8(current_directory).c_str()); 377 int ret = chdir(path.value().c_str());
378 return (ret == 0); 378 return !ret;
379 } 379 }
380 380
381 FileEnumerator::FileEnumerator(const std::wstring& root_path, 381 FileEnumerator::FileEnumerator(const std::wstring& root_path,
382 bool recursive, 382 bool recursive,
383 FileEnumerator::FILE_TYPE file_type) 383 FileEnumerator::FILE_TYPE file_type)
384 : recursive_(recursive), 384 : recursive_(recursive),
385 file_type_(file_type), 385 file_type_(file_type),
386 is_in_find_op_(false), 386 is_in_find_op_(false),
387 fts_(NULL) { 387 fts_(NULL) {
388 pending_paths_.push(root_path); 388 pending_paths_.push(root_path);
389 } 389 }
390 390
391 FileEnumerator::FileEnumerator(const std::wstring& root_path, 391 FileEnumerator::FileEnumerator(const std::wstring& root_path,
392 bool recursive, 392 bool recursive,
393 FileEnumerator::FILE_TYPE file_type, 393 FileEnumerator::FILE_TYPE file_type,
394 const std::wstring& pattern) 394 const std::wstring& pattern)
395 : recursive_(recursive), 395 : recursive_(recursive),
396 file_type_(file_type), 396 file_type_(file_type),
397 pattern_(root_path), 397 pattern_(root_path),
398 is_in_find_op_(false), 398 is_in_find_op_(false),
399 fts_(NULL) { 399 fts_(NULL) {
400 // The Windows version of this code only matches against items in the top-most 400 // The Windows version of this code only matches against items in the top-most
401 // directory, and we're comparing fnmatch against full paths, so this is the 401 // directory, and we're comparing fnmatch against full paths, so this is the
402 // easiest way to get the right pattern. 402 // easiest way to get the right pattern.
403 AppendToPath(&pattern_, pattern); 403 AppendToPath(&pattern_, pattern);
404 pending_paths_.push(root_path); 404 pending_paths_.push(root_path);
405 } 405 }
406 406
407 FileEnumerator::~FileEnumerator() { 407 FileEnumerator::~FileEnumerator() {
408 if (fts_) 408 if (fts_)
409 fts_close(fts_); 409 fts_close(fts_);
410 } 410 }
411 411
412 // As it stands, this method calls itself recursively when the next item of 412 // As it stands, this method calls itself recursively when the next item of
413 // the fts enumeration doesn't match (type, pattern, etc.). In the case of 413 // the fts enumeration doesn't match (type, pattern, etc.). In the case of
414 // large directories with many files this can be quite deep. 414 // large directories with many files this can be quite deep.
415 // TODO(erikkay) - get rid of this recursive pattern 415 // TODO(erikkay) - get rid of this recursive pattern
416 std::wstring FileEnumerator::Next() { 416 std::wstring FileEnumerator::Next() {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next(); 464 return (file_type_ & FileEnumerator::DIRECTORIES) ? cur_file : Next();
465 } else if (fts_ent->fts_info == FTS_F) { 465 } else if (fts_ent->fts_info == FTS_F) {
466 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next(); 466 return (file_type_ & FileEnumerator::FILES) ? cur_file : Next();
467 } 467 }
468 // TODO(erikkay) - verify that the other fts_info types aren't interesting 468 // TODO(erikkay) - verify that the other fts_info types aren't interesting
469 return Next(); 469 return Next();
470 } 470 }
471 471
472 472
473 } // namespace file_util 473 } // namespace file_util
OLDNEW
« no previous file with comments | « base/file_util_linux.cc ('k') | base/file_util_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698