Mike Dodd | 8cfa702 | 2010-11-17 11:12:26 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file file_manip.h |
| 3 | * Useful file management helpers |
| 4 | * |
| 5 | * @remark Copyright 2002 OProfile authors |
| 6 | * @remark Read the file COPYING |
| 7 | * |
| 8 | * @author Philippe Elie |
| 9 | * @author John Levon |
| 10 | */ |
| 11 | |
| 12 | #ifndef FILE_MANIP_H |
| 13 | #define FILE_MANIP_H |
| 14 | |
| 15 | #include <string> |
| 16 | #include <list> |
| 17 | |
| 18 | |
| 19 | /** |
| 20 | * copy_file - copy a file. |
| 21 | * @param source filename to copy from |
| 22 | * @param destination filename to copy into |
| 23 | * |
| 24 | * the last modification time of the source file is preserved, file attribute |
| 25 | * and owner are preserved if possible. Return true if copying successful. |
| 26 | */ |
| 27 | bool copy_file(std::string const & source, std::string const & destination); |
| 28 | |
| 29 | /// return true if dir is an existing directory |
| 30 | bool is_directory(std::string const & dirname); |
| 31 | |
| 32 | /** |
| 33 | * is_file_identical - check for identical files |
| 34 | * @param file1 first filename |
| 35 | * @param file2 second filename |
| 36 | * |
| 37 | * return true if the two filenames belong to the same file |
| 38 | */ |
| 39 | bool is_files_identical(std::string const & file1, std::string const & file2); |
| 40 | |
| 41 | /** |
| 42 | * op_realpath - resolve symlinks etc. |
| 43 | * Resolve a path as much as possible. Accounts for relative |
| 44 | * paths (from cwd), ".." and ".". For success, the target |
| 45 | * file must exist ! |
| 46 | * |
| 47 | * Resolve a symbolic link as far as possible. |
| 48 | * Returns the original string on failure. |
| 49 | */ |
| 50 | std::string const op_realpath(std::string const & name); |
| 51 | |
| 52 | /// return true if the given file is readable |
| 53 | bool op_file_readable(std::string const & file); |
| 54 | |
| 55 | /** |
| 56 | * @param file_list where to store result |
| 57 | * @param base_dir directory from where lookup start |
| 58 | * @param filter a filename filter |
| 59 | * @param recursive if true lookup in sub-directory |
| 60 | * |
| 61 | * create a filelist under base_dir, filtered by filter and optionally |
| 62 | * looking in sub-directory. If we look in sub-directory only sub-directory |
| 63 | * which match filter are traversed. |
| 64 | */ |
| 65 | bool create_file_list(std::list<std::string> & file_list, |
| 66 | std::string const & base_dir, |
| 67 | std::string const & filter = "*", |
| 68 | bool recursive = false); |
| 69 | |
| 70 | /** |
| 71 | * op_dirname - get the path component of a filename |
| 72 | * @param file_name filename |
| 73 | * |
| 74 | * Returns the path name of a filename with trailing '/' removed. |
| 75 | */ |
| 76 | std::string op_dirname(std::string const & file_name); |
| 77 | |
| 78 | /** |
| 79 | * op_basename - get the basename of a path |
| 80 | * @param path_name path |
| 81 | * |
| 82 | * Returns the basename of a path with trailing '/' removed. |
| 83 | * |
| 84 | * Always use this instead of the C basename() - header order |
| 85 | * can affect behaviour for basename("/") |
| 86 | */ |
| 87 | std::string op_basename(std::string const & path_name); |
| 88 | |
| 89 | #endif /* !FILE_MANIP_H */ |