2 +----------------------------------------------------------------------+
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 //////////////////////////////////////////////////////////////////////
19 #ifndef incl_HPHP_FILE_UTIL_H_
20 #define incl_HPHP_FILE_UTIL_H_
22 #include "hphp/util/file.h"
24 #include "hphp/runtime/base/type-string.h"
30 namespace HPHP
{ namespace FileUtil
{
32 ///////////////////////////////////////////////////////////////////////////////
35 * Make sure path exists. Same as "mkdir -p", but "a/b" will only make sure
36 * "a/" exists, treating "b" as a file name.
38 bool mkdir(const std::string
&path
, int mode
= 0777);
41 * Make dest directory look identical to src by copying files and directories,
42 * without copying identical files (so they keep the same timestamp as before).
44 void syncdir(const std::string
&dest
, const std::string
&src
,
45 bool keepSrc
= false);
48 * Copy srcfile to dstfile, return 0 on success, -1 otherwise
50 int copy(const char *srcfile
, const char *dstfile
);
53 * Like copy but using little disk-cache
55 int directCopy(const char *srcfile
, const char *dstfile
);
58 * Like rename(2), but takes care of cross-filesystem rename.
60 int rename(const char *oldname
, const char *newname
);
63 * Like rename but using little disk-cache
65 int directRename(const char *oldname
, const char *newname
);
68 * Like system(3), but automatically print errors if execution fails.
70 int ssystem(const char *command
);
73 * Find the relative path from a directory with trailing slash to the file
75 String
relativePath(const std::string
& fromDir
, const String
& toFile
);
78 * Canonicalize path to remove "..", "." and "\/", etc..
80 String
canonicalize(const String
& path
);
81 String
canonicalize(const std::string
& path
);
82 String
canonicalize(const char* path
, size_t len
,
83 bool collapse_slashes
= true);
85 std::string
expandUser(const std::string
& path
,
86 const std::string
& sysUser
= "");
88 * Makes sure there is ending slash by changing "path/name" to "path/name/".
90 std::string
normalizeDir(const std::string
&dirname
);
93 * Thread-safe dirname().
95 String
dirname(const String
& path
);
98 * Search for PHP, JS, or other files under a directory.
100 void find(std::vector
<std::string
> &out
,
101 const std::string
&root
, const std::string
& path
, bool php
,
102 const std::set
<std::string
> *excludeDirs
= nullptr,
103 const std::set
<std::string
> *excludeFiles
= nullptr);
106 * Search for PHP or non-PHP files under a directory, calling callback for
109 template <typename F
>
110 void find(std::vector
<std::string
> &out
,
111 const std::string
&root
, const std::string
& path
, bool php
,
115 * Determines if a given string is a valid path or not
116 * (ie: contains no null bytes)
118 bool isValidPath(const String
& path
);
121 * Helper functions for use with FileUtil::isValidPath
123 bool checkPathAndWarn(const String
& path
,
124 const char* func_name
,
126 void checkPathAndError(const String
& path
,
127 const char* func_name
,
131 * If any parent of the directory the script cmd was run from includes the
132 * relative path suffix, and the file at that path is readable, then perform
133 * action() on that file. Continues processing up the directory tree until
134 * action() returns true.
136 template <class Action
>
137 bool runRelative(std::string suffix
, String cmd
,
138 const char* currentDir
, Action action
);
140 bool isSystemName(folly::StringPiece path
);
142 ///////////////////////////////////////////////////////////////////////////////