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