xref: /aosp_15_r20/external/libbrillo/brillo/file_utils.h (revision 1a96fba65179ea7d3f56207137718607415c5953)
1*1a96fba6SXin Li // Copyright 2014 The Chromium OS Authors. All rights reserved.
2*1a96fba6SXin Li // Use of this source code is governed by a BSD-style license that can be
3*1a96fba6SXin Li // found in the LICENSE file.
4*1a96fba6SXin Li 
5*1a96fba6SXin Li #ifndef LIBBRILLO_BRILLO_FILE_UTILS_H_
6*1a96fba6SXin Li #define LIBBRILLO_BRILLO_FILE_UTILS_H_
7*1a96fba6SXin Li 
8*1a96fba6SXin Li #include <sys/types.h>
9*1a96fba6SXin Li 
10*1a96fba6SXin Li #include <string>
11*1a96fba6SXin Li 
12*1a96fba6SXin Li #include <base/files/file_path.h>
13*1a96fba6SXin Li #include <base/files/scoped_file.h>
14*1a96fba6SXin Li #include <brillo/brillo_export.h>
15*1a96fba6SXin Li #include <brillo/secure_blob.h>
16*1a96fba6SXin Li 
17*1a96fba6SXin Li namespace brillo {
18*1a96fba6SXin Li 
19*1a96fba6SXin Li // Ensures a regular file owned by user |uid| and group |gid| exists at |path|.
20*1a96fba6SXin Li // Any other entity at |path| will be deleted and replaced with an empty
21*1a96fba6SXin Li // regular file. If a new file is needed, any missing parent directories will
22*1a96fba6SXin Li // be created, and the file will be assigned |new_file_permissions|.
23*1a96fba6SXin Li // Should be safe to use in all directories, including tmpdirs with the sticky
24*1a96fba6SXin Li // bit set.
25*1a96fba6SXin Li // Returns true if the file existed or was able to be created.
26*1a96fba6SXin Li BRILLO_EXPORT bool TouchFile(const base::FilePath& path,
27*1a96fba6SXin Li                              int new_file_permissions,
28*1a96fba6SXin Li                              uid_t uid,
29*1a96fba6SXin Li                              gid_t gid);
30*1a96fba6SXin Li 
31*1a96fba6SXin Li // Convenience version of TouchFile() defaulting to 600 permissions and the
32*1a96fba6SXin Li // current euid/egid.
33*1a96fba6SXin Li // Should be safe to use in all directories, including tmpdirs with the sticky
34*1a96fba6SXin Li // bit set.
35*1a96fba6SXin Li BRILLO_EXPORT bool TouchFile(const base::FilePath& path);
36*1a96fba6SXin Li 
37*1a96fba6SXin Li // Opens the absolute |path| to a regular file or directory ensuring that none
38*1a96fba6SXin Li // of the path components are symbolic links and returns a FD. If |path| is
39*1a96fba6SXin Li // relative, or contains any symbolic links, or points to a non-regular file or
40*1a96fba6SXin Li // directory, an invalid FD is returned instead. |mode| is ignored unless
41*1a96fba6SXin Li // |flags| has either O_CREAT or O_TMPFILE. Note that O_CLOEXEC is set so the
42*1a96fba6SXin Li // file descriptor will not be inherited across exec calls.
43*1a96fba6SXin Li //
44*1a96fba6SXin Li // Parameters
45*1a96fba6SXin Li //  path - An absolute path of the file to open
46*1a96fba6SXin Li //  flags - Flags to pass to open.
47*1a96fba6SXin Li //  mode - Mode to pass to open.
48*1a96fba6SXin Li BRILLO_EXPORT base::ScopedFD OpenSafely(const base::FilePath& path,
49*1a96fba6SXin Li                                         int flags,
50*1a96fba6SXin Li                                         mode_t mode);
51*1a96fba6SXin Li 
52*1a96fba6SXin Li // Opens the |path| relative to the |parent_fd| to a regular file or directory
53*1a96fba6SXin Li // ensuring that none of the path components are symbolic links and returns a
54*1a96fba6SXin Li // FD. If |path| contains any symbolic links, or points to a non-regular file or
55*1a96fba6SXin Li // directory, an invalid FD is returned instead. |mode| is ignored unless
56*1a96fba6SXin Li // |flags| has either O_CREAT or O_TMPFILE. Note that O_CLOEXEC is set so the
57*1a96fba6SXin Li // file descriptor will not be inherited across exec calls.
58*1a96fba6SXin Li //
59*1a96fba6SXin Li // Parameters
60*1a96fba6SXin Li //  parent_fd - The file descriptor of the parent directory
61*1a96fba6SXin Li //  path - An absolute path of the file to open
62*1a96fba6SXin Li //  flags - Flags to pass to open.
63*1a96fba6SXin Li //  mode - Mode to pass to open.
64*1a96fba6SXin Li BRILLO_EXPORT base::ScopedFD OpenAtSafely(int parent_fd,
65*1a96fba6SXin Li                                           const base::FilePath& path,
66*1a96fba6SXin Li                                           int flags,
67*1a96fba6SXin Li                                           mode_t mode);
68*1a96fba6SXin Li 
69*1a96fba6SXin Li // Opens the absolute |path| to a FIFO ensuring that none of the path components
70*1a96fba6SXin Li // are symbolic links and returns a FD. If |path| is relative, or contains any
71*1a96fba6SXin Li // symbolic links, or points to a non-regular file or directory, an invalid FD
72*1a96fba6SXin Li // is returned instead. |mode| is ignored unless |flags| has either O_CREAT or
73*1a96fba6SXin Li // O_TMPFILE.
74*1a96fba6SXin Li //
75*1a96fba6SXin Li // Parameters
76*1a96fba6SXin Li //  path - An absolute path of the file to open
77*1a96fba6SXin Li //  flags - Flags to pass to open.
78*1a96fba6SXin Li //  mode - Mode to pass to open.
79*1a96fba6SXin Li BRILLO_EXPORT base::ScopedFD OpenFifoSafely(const base::FilePath& path,
80*1a96fba6SXin Li                                             int flags,
81*1a96fba6SXin Li                                             mode_t mode);
82*1a96fba6SXin Li 
83*1a96fba6SXin Li // Iterates through the path components and creates any missing ones. Guarantees
84*1a96fba6SXin Li // the ancestor paths are not symlinks. This function returns an invalid FD on
85*1a96fba6SXin Li // failure. Newly created directories will have |mode| permissions. The returned
86*1a96fba6SXin Li // file descriptor was opened with both O_RDONLY and O_CLOEXEC.
87*1a96fba6SXin Li //
88*1a96fba6SXin Li // Parameters
89*1a96fba6SXin Li //  full_path - An absolute path of the directory to create and open.
90*1a96fba6SXin Li BRILLO_EXPORT base::ScopedFD MkdirRecursively(const base::FilePath& full_path,
91*1a96fba6SXin Li                                               mode_t mode);
92*1a96fba6SXin Li 
93*1a96fba6SXin Li // Writes the entirety of the given data to |path| with 0640 permissions
94*1a96fba6SXin Li // (modulo umask).  If missing, parent (and parent of parent etc.) directories
95*1a96fba6SXin Li // are created with 0700 permissions (modulo umask).  Returns true on success.
96*1a96fba6SXin Li //
97*1a96fba6SXin Li // Parameters
98*1a96fba6SXin Li //  path      - Path of the file to write
99*1a96fba6SXin Li //  blob/data - blob/string/array to populate from
100*1a96fba6SXin Li // (size      - array size)
101*1a96fba6SXin Li BRILLO_EXPORT bool WriteStringToFile(const base::FilePath& path,
102*1a96fba6SXin Li                                      const std::string& data);
103*1a96fba6SXin Li BRILLO_EXPORT bool WriteToFile(const base::FilePath& path,
104*1a96fba6SXin Li                                const char* data,
105*1a96fba6SXin Li                                size_t size);
106*1a96fba6SXin Li template <class T>
WriteBlobToFile(const base::FilePath & path,const T & blob)107*1a96fba6SXin Li BRILLO_EXPORT bool WriteBlobToFile(const base::FilePath& path, const T& blob) {
108*1a96fba6SXin Li   return WriteToFile(path, reinterpret_cast<const char*>(blob.data()),
109*1a96fba6SXin Li                      blob.size());
110*1a96fba6SXin Li }
111*1a96fba6SXin Li 
112*1a96fba6SXin Li // Calls fdatasync() on file if data_sync is true or fsync() on directory or
113*1a96fba6SXin Li // file when data_sync is false.  Returns true on success.
114*1a96fba6SXin Li //
115*1a96fba6SXin Li // Parameters
116*1a96fba6SXin Li //   path - File/directory to be sync'ed
117*1a96fba6SXin Li //   is_directory - True if |path| is a directory
118*1a96fba6SXin Li //   data_sync - True if |path| does not need metadata to be synced
119*1a96fba6SXin Li BRILLO_EXPORT bool SyncFileOrDirectory(const base::FilePath& path,
120*1a96fba6SXin Li                                        bool is_directory,
121*1a96fba6SXin Li                                        bool data_sync);
122*1a96fba6SXin Li 
123*1a96fba6SXin Li // Atomically writes the entirety of the given data to |path| with |mode|
124*1a96fba6SXin Li // permissions (modulo umask).  If missing, parent (and parent of parent etc.)
125*1a96fba6SXin Li // directories are created with 0700 permissions (modulo umask).  Returns true
126*1a96fba6SXin Li // if the file has been written successfully and it has physically hit the
127*1a96fba6SXin Li // disk.  Returns false if either writing the file has failed or if it cannot
128*1a96fba6SXin Li // be guaranteed that it has hit the disk.
129*1a96fba6SXin Li //
130*1a96fba6SXin Li // Parameters
131*1a96fba6SXin Li //   path - Path of the file to write
132*1a96fba6SXin Li //   blob/data - blob/array to populate from
133*1a96fba6SXin Li //   (size - array size)
134*1a96fba6SXin Li //   mode - File permission bit-pattern, eg. 0644 for rw-r--r--
135*1a96fba6SXin Li BRILLO_EXPORT bool WriteToFileAtomic(const base::FilePath& path,
136*1a96fba6SXin Li                                      const char* data,
137*1a96fba6SXin Li                                      size_t size,
138*1a96fba6SXin Li                                      mode_t mode);
139*1a96fba6SXin Li template <class T>
WriteBlobToFileAtomic(const base::FilePath & path,const T & blob,mode_t mode)140*1a96fba6SXin Li BRILLO_EXPORT bool WriteBlobToFileAtomic(const base::FilePath& path,
141*1a96fba6SXin Li                                          const T& blob,
142*1a96fba6SXin Li                                          mode_t mode) {
143*1a96fba6SXin Li   return WriteToFileAtomic(path, reinterpret_cast<const char*>(blob.data()),
144*1a96fba6SXin Li                            blob.size(), mode);
145*1a96fba6SXin Li }
146*1a96fba6SXin Li 
147*1a96fba6SXin Li // ComputeDirectoryDiskUsage() is similar to base::ComputeDirectorySize() in
148*1a96fba6SXin Li // libbase, but it returns the actual disk usage instead of the apparent size.
149*1a96fba6SXin Li // In another word, ComputeDirectoryDiskUsage() behaves like "du -s
150*1a96fba6SXin Li // --apparent-size", and ComputeDirectorySize() behaves like "du -s". The
151*1a96fba6SXin Li // primary difference is that sparse file and files on filesystem with
152*1a96fba6SXin Li // transparent compression will report smaller file size than
153*1a96fba6SXin Li // ComputeDirectorySize(). Returns the total used bytes.
154*1a96fba6SXin Li // The following behaviours of this function is guaranteed and is verified by
155*1a96fba6SXin Li // unit tests:
156*1a96fba6SXin Li // - This function recursively processes directory down the tree, so disk space
157*1a96fba6SXin Li // used by files in all the subdirectories are counted.
158*1a96fba6SXin Li // - Symbolic links will not be followed (the size of link itself is counted,
159*1a96fba6SXin Li // the target is not)
160*1a96fba6SXin Li // - Hidden files are counted as well.
161*1a96fba6SXin Li // The following behaviours are not guaranteed, and it is recommended to avoid
162*1a96fba6SXin Li // them in the field. Their current behaviour is provided for reference only:
163*1a96fba6SXin Li // - This function doesn't care about filesystem boundaries, so it'll cross
164*1a96fba6SXin Li // filesystem boundary to count file size if there's one in the specified
165*1a96fba6SXin Li // directory.
166*1a96fba6SXin Li // - Hard links will be treated like normal files, so they could be
167*1a96fba6SXin Li // over-reported.
168*1a96fba6SXin Li // - Directories that the current user doesn't have permission to list/stat will
169*1a96fba6SXin Li // be ignored, and an error will be logged but the returned result could be
170*1a96fba6SXin Li // under-reported without error in the returned value.
171*1a96fba6SXin Li // - Deduplication (should the filesystem support it) is ignored, and the result
172*1a96fba6SXin Li // could be over-reported.
173*1a96fba6SXin Li // - Doesn't check if |root_path| exists, a non-existent directory will results
174*1a96fba6SXin Li // in 0 bytes without any error.
175*1a96fba6SXin Li // - There are no limit on the depth of file system tree, the program will crash
176*1a96fba6SXin Li // if it run out of memory to hold the entire depth of file system tree.
177*1a96fba6SXin Li // - If the directory is modified during this function call, there's no
178*1a96fba6SXin Li // guarantee on if the function will count the updated or original file system
179*1a96fba6SXin Li // state. The function could choose to count the updated state for one file and
180*1a96fba6SXin Li // original state for another file.
181*1a96fba6SXin Li // - Non-POSIX system is not supported.
182*1a96fba6SXin Li // - Disk space used by directory (and its subdirectories) itself is counted.
183*1a96fba6SXin Li //
184*1a96fba6SXin Li // Parameters
185*1a96fba6SXin Li //   root_path - The directory to compute the size for
186*1a96fba6SXin Li BRILLO_EXPORT int64_t
187*1a96fba6SXin Li ComputeDirectoryDiskUsage(const base::FilePath& root_path);
188*1a96fba6SXin Li 
189*1a96fba6SXin Li }  // namespace brillo
190*1a96fba6SXin Li 
191*1a96fba6SXin Li #endif  // LIBBRILLO_BRILLO_FILE_UTILS_H_
192