1 // Copyright 2014 The Chromium OS Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef LIBBRILLO_BRILLO_FILE_UTILS_H_ 6 #define LIBBRILLO_BRILLO_FILE_UTILS_H_ 7 8 #include <sys/types.h> 9 10 #include <base/files/file_path.h> 11 #include <brillo/brillo_export.h> 12 #include <brillo/secure_blob.h> 13 14 namespace brillo { 15 16 // Ensures a regular file owned by user |uid| and group |gid| exists at |path|. 17 // Any other entity at |path| will be deleted and replaced with an empty 18 // regular file. If a new file is needed, any missing parent directories will 19 // be created, and the file will be assigned |new_file_permissions|. 20 // Should be safe to use in all directories, including tmpdirs with the sticky 21 // bit set. 22 // Returns true if the file existed or was able to be created. 23 BRILLO_EXPORT bool TouchFile(const base::FilePath& path, 24 int new_file_permissions, 25 uid_t uid, 26 gid_t gid); 27 28 // Convenience version of TouchFile() defaulting to 600 permissions and the 29 // current euid/egid. 30 // Should be safe to use in all directories, including tmpdirs with the sticky 31 // bit set. 32 BRILLO_EXPORT bool TouchFile(const base::FilePath& path); 33 34 // Writes the entirety of the given data to |path| with 0640 permissions 35 // (modulo umask). If missing, parent (and parent of parent etc.) directories 36 // are created with 0700 permissions (modulo umask). Returns true on success. 37 // 38 // Parameters 39 // path - Path of the file to write 40 // blob/data - blob/string/array to populate from 41 // (size - array size) 42 BRILLO_EXPORT bool WriteBlobToFile(const base::FilePath& path, 43 const Blob& blob); 44 BRILLO_EXPORT bool WriteStringToFile(const base::FilePath& path, 45 const std::string& data); 46 BRILLO_EXPORT bool WriteToFile(const base::FilePath& path, 47 const char* data, 48 size_t size); 49 50 // Calls fdatasync() on file if data_sync is true or fsync() on directory or 51 // file when data_sync is false. Returns true on success. 52 // 53 // Parameters 54 // path - File/directory to be sync'ed 55 // is_directory - True if |path| is a directory 56 // data_sync - True if |path| does not need metadata to be synced 57 BRILLO_EXPORT bool SyncFileOrDirectory(const base::FilePath& path, 58 bool is_directory, 59 bool data_sync); 60 61 // Atomically writes the entirety of the given data to |path| with |mode| 62 // permissions (modulo umask). If missing, parent (and parent of parent etc.) 63 // directories are created with 0700 permissions (modulo umask). Returns true 64 // if the file has been written successfully and it has physically hit the 65 // disk. Returns false if either writing the file has failed or if it cannot 66 // be guaranteed that it has hit the disk. 67 // 68 // Parameters 69 // path - Path of the file to write 70 // blob/data - blob/array to populate from 71 // (size - array size) 72 // mode - File permission bit-pattern, eg. 0644 for rw-r--r-- 73 BRILLO_EXPORT bool WriteBlobToFileAtomic(const base::FilePath& path, 74 const Blob& blob, 75 mode_t mode); 76 BRILLO_EXPORT bool WriteToFileAtomic(const base::FilePath& path, 77 const char* data, 78 size_t size, 79 mode_t mode); 80 81 } // namespace brillo 82 83 #endif // LIBBRILLO_BRILLO_FILE_UTILS_H_ 84