1 // Copyright 2019 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 // Filesystem-related utility functions. 6 7 #ifndef LIBBRILLO_BRILLO_FILES_FILE_UTIL_H_ 8 #define LIBBRILLO_BRILLO_FILES_FILE_UTIL_H_ 9 10 #include <string> 11 12 #include <brillo/files/safe_fd.h> 13 14 namespace brillo { 15 16 SafeFD::Error IsValidFilename(const std::string& filename); 17 18 // Obtain the canonical path of the file descriptor or base::FilePath() on 19 // failure. 20 BRILLO_EXPORT base::FilePath GetFDPath(int fd); 21 22 // Open or create a child directory named |name| as a child of |parent| with 23 // the specified permissions and ownership. Custom open flags can be set with 24 // |flags|. The directory will be re-created if: 25 // * The open operation fails (e.g. if |name| is not a directory). 26 // * The permissions do not match. 27 // * The ownership is different. 28 // 29 // Parameters 30 // parent - An open SafeFD to the parent directory. 31 // name - the name of the directory being created. It cannot have more than one 32 // path component. 33 BRILLO_EXPORT SafeFD::SafeFDResult OpenOrRemakeDir( 34 SafeFD* parent, 35 const std::string& name, 36 int permissions = SafeFD::kDefaultDirPermissions, 37 uid_t uid = getuid(), 38 gid_t gid = getgid(), 39 int flags = O_RDONLY | O_CLOEXEC); 40 41 // Open or create a file named |name| under the directory |parent| with 42 // the specified permissions and ownership. Custom open flags can be set with 43 // |flags|. The file will be re-created if: 44 // * The open operation fails (e.g. |name| is a directory). 45 // * The permissions do not match. 46 // * The ownership is different. 47 // 48 // Parameters 49 // parent - An open SafeFD to the parent directory. 50 // name - the name of the file being created. It cannot have more than one 51 // path component. 52 BRILLO_EXPORT SafeFD::SafeFDResult OpenOrRemakeFile( 53 SafeFD* parent, 54 const std::string& name, 55 int permissions = SafeFD::kDefaultFilePermissions, 56 uid_t uid = getuid(), 57 gid_t gid = getgid(), 58 int flags = O_RDWR | O_CLOEXEC); 59 60 } // namespace brillo 61 62 #endif // LIBBRILLO_BRILLO_FILES_FILE_UTIL_H_ 63