1*6777b538SAndroid Build Coastguard Worker // Copyright 2012 The Chromium Authors 2*6777b538SAndroid Build Coastguard Worker // Use of this source code is governed by a BSD-style license that can be 3*6777b538SAndroid Build Coastguard Worker // found in the LICENSE file. 4*6777b538SAndroid Build Coastguard Worker 5*6777b538SAndroid Build Coastguard Worker // FilePath is a container for pathnames stored in a platform's native string 6*6777b538SAndroid Build Coastguard Worker // type, providing containers for manipulation in according with the 7*6777b538SAndroid Build Coastguard Worker // platform's conventions for pathnames. It supports the following path 8*6777b538SAndroid Build Coastguard Worker // types: 9*6777b538SAndroid Build Coastguard Worker // 10*6777b538SAndroid Build Coastguard Worker // POSIX Windows 11*6777b538SAndroid Build Coastguard Worker // --------------- ---------------------------------- 12*6777b538SAndroid Build Coastguard Worker // Fundamental type char[] wchar_t[] 13*6777b538SAndroid Build Coastguard Worker // Encoding unspecified* UTF-16 14*6777b538SAndroid Build Coastguard Worker // Separator / \, tolerant of / 15*6777b538SAndroid Build Coastguard Worker // Drive letters no case-insensitive A-Z followed by : 16*6777b538SAndroid Build Coastguard Worker // Alternate root // (surprise!) \\ (2 Separators), for UNC paths 17*6777b538SAndroid Build Coastguard Worker // 18*6777b538SAndroid Build Coastguard Worker // * The encoding need not be specified on POSIX systems, although some 19*6777b538SAndroid Build Coastguard Worker // POSIX-compliant systems do specify an encoding. Mac OS X uses UTF-8. 20*6777b538SAndroid Build Coastguard Worker // Chrome OS also uses UTF-8. 21*6777b538SAndroid Build Coastguard Worker // Linux does not specify an encoding, but in practice, the locale's 22*6777b538SAndroid Build Coastguard Worker // character set may be used. 23*6777b538SAndroid Build Coastguard Worker // 24*6777b538SAndroid Build Coastguard Worker // For more arcane bits of path trivia, see below. 25*6777b538SAndroid Build Coastguard Worker // 26*6777b538SAndroid Build Coastguard Worker // FilePath objects are intended to be used anywhere paths are. An 27*6777b538SAndroid Build Coastguard Worker // application may pass FilePath objects around internally, masking the 28*6777b538SAndroid Build Coastguard Worker // underlying differences between systems, only differing in implementation 29*6777b538SAndroid Build Coastguard Worker // where interfacing directly with the system. For example, a single 30*6777b538SAndroid Build Coastguard Worker // OpenFile(const FilePath &) function may be made available, allowing all 31*6777b538SAndroid Build Coastguard Worker // callers to operate without regard to the underlying implementation. On 32*6777b538SAndroid Build Coastguard Worker // POSIX-like platforms, OpenFile might wrap fopen, and on Windows, it might 33*6777b538SAndroid Build Coastguard Worker // wrap _wfopen_s, perhaps both by calling file_path.value().c_str(). This 34*6777b538SAndroid Build Coastguard Worker // allows each platform to pass pathnames around without requiring conversions 35*6777b538SAndroid Build Coastguard Worker // between encodings, which has an impact on performance, but more imporantly, 36*6777b538SAndroid Build Coastguard Worker // has an impact on correctness on platforms that do not have well-defined 37*6777b538SAndroid Build Coastguard Worker // encodings for pathnames. 38*6777b538SAndroid Build Coastguard Worker // 39*6777b538SAndroid Build Coastguard Worker // Several methods are available to perform common operations on a FilePath 40*6777b538SAndroid Build Coastguard Worker // object, such as determining the parent directory (DirName), isolating the 41*6777b538SAndroid Build Coastguard Worker // final path component (BaseName), and appending a relative pathname string 42*6777b538SAndroid Build Coastguard Worker // to an existing FilePath object (Append). These methods are highly 43*6777b538SAndroid Build Coastguard Worker // recommended over attempting to split and concatenate strings directly. 44*6777b538SAndroid Build Coastguard Worker // These methods are based purely on string manipulation and knowledge of 45*6777b538SAndroid Build Coastguard Worker // platform-specific pathname conventions, and do not consult the filesystem 46*6777b538SAndroid Build Coastguard Worker // at all, making them safe to use without fear of blocking on I/O operations. 47*6777b538SAndroid Build Coastguard Worker // These methods do not function as mutators but instead return distinct 48*6777b538SAndroid Build Coastguard Worker // instances of FilePath objects, and are therefore safe to use on const 49*6777b538SAndroid Build Coastguard Worker // objects. The objects themselves are safe to share between threads. 50*6777b538SAndroid Build Coastguard Worker // 51*6777b538SAndroid Build Coastguard Worker // To aid in initialization of FilePath objects from string literals, a 52*6777b538SAndroid Build Coastguard Worker // FILE_PATH_LITERAL macro is provided, which accounts for the difference 53*6777b538SAndroid Build Coastguard Worker // between char[]-based pathnames on POSIX systems and wchar_t[]-based 54*6777b538SAndroid Build Coastguard Worker // pathnames on Windows. 55*6777b538SAndroid Build Coastguard Worker // 56*6777b538SAndroid Build Coastguard Worker // As a precaution against premature truncation, paths can't contain NULs. 57*6777b538SAndroid Build Coastguard Worker // 58*6777b538SAndroid Build Coastguard Worker // Because a FilePath object should not be instantiated at the global scope, 59*6777b538SAndroid Build Coastguard Worker // instead, use a FilePath::CharType[] and initialize it with 60*6777b538SAndroid Build Coastguard Worker // FILE_PATH_LITERAL. At runtime, a FilePath object can be created from the 61*6777b538SAndroid Build Coastguard Worker // character array. Example: 62*6777b538SAndroid Build Coastguard Worker // 63*6777b538SAndroid Build Coastguard Worker // | const FilePath::CharType kLogFileName[] = FILE_PATH_LITERAL("log.txt"); 64*6777b538SAndroid Build Coastguard Worker // | 65*6777b538SAndroid Build Coastguard Worker // | void Function() { 66*6777b538SAndroid Build Coastguard Worker // | FilePath log_file_path(kLogFileName); 67*6777b538SAndroid Build Coastguard Worker // | [...] 68*6777b538SAndroid Build Coastguard Worker // | } 69*6777b538SAndroid Build Coastguard Worker // 70*6777b538SAndroid Build Coastguard Worker // WARNING: FilePaths should ALWAYS be displayed with LTR directionality, even 71*6777b538SAndroid Build Coastguard Worker // when the UI language is RTL. This means you always need to pass filepaths 72*6777b538SAndroid Build Coastguard Worker // through base::i18n::WrapPathWithLTRFormatting() before displaying it in the 73*6777b538SAndroid Build Coastguard Worker // RTL UI. 74*6777b538SAndroid Build Coastguard Worker // 75*6777b538SAndroid Build Coastguard Worker // This is a very common source of bugs, please try to keep this in mind. 76*6777b538SAndroid Build Coastguard Worker // 77*6777b538SAndroid Build Coastguard Worker // ARCANE BITS OF PATH TRIVIA 78*6777b538SAndroid Build Coastguard Worker // 79*6777b538SAndroid Build Coastguard Worker // - A double leading slash is actually part of the POSIX standard. Systems 80*6777b538SAndroid Build Coastguard Worker // are allowed to treat // as an alternate root, as Windows does for UNC 81*6777b538SAndroid Build Coastguard Worker // (network share) paths. Most POSIX systems don't do anything special 82*6777b538SAndroid Build Coastguard Worker // with two leading slashes, but FilePath handles this case properly 83*6777b538SAndroid Build Coastguard Worker // in case it ever comes across such a system. FilePath needs this support 84*6777b538SAndroid Build Coastguard Worker // for Windows UNC paths, anyway. 85*6777b538SAndroid Build Coastguard Worker // References: 86*6777b538SAndroid Build Coastguard Worker // The Open Group Base Specifications Issue 7, sections 3.267 ("Pathname") 87*6777b538SAndroid Build Coastguard Worker // and 4.12 ("Pathname Resolution"), available at: 88*6777b538SAndroid Build Coastguard Worker // http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_267 89*6777b538SAndroid Build Coastguard Worker // http://www.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_12 90*6777b538SAndroid Build Coastguard Worker // 91*6777b538SAndroid Build Coastguard Worker // - Windows treats c:\\ the same way it treats \\. This was intended to 92*6777b538SAndroid Build Coastguard Worker // allow older applications that require drive letters to support UNC paths 93*6777b538SAndroid Build Coastguard Worker // like \\server\share\path, by permitting c:\\server\share\path as an 94*6777b538SAndroid Build Coastguard Worker // equivalent. Since the OS treats these paths specially, FilePath needs 95*6777b538SAndroid Build Coastguard Worker // to do the same. Since Windows can use either / or \ as the separator, 96*6777b538SAndroid Build Coastguard Worker // FilePath treats c://, c:\\, //, and \\ all equivalently. 97*6777b538SAndroid Build Coastguard Worker // Reference: 98*6777b538SAndroid Build Coastguard Worker // The Old New Thing, "Why is a drive letter permitted in front of UNC 99*6777b538SAndroid Build Coastguard Worker // paths (sometimes)?", available at: 100*6777b538SAndroid Build Coastguard Worker // http://blogs.msdn.com/oldnewthing/archive/2005/11/22/495740.aspx 101*6777b538SAndroid Build Coastguard Worker 102*6777b538SAndroid Build Coastguard Worker #ifndef BASE_FILES_FILE_PATH_H_ 103*6777b538SAndroid Build Coastguard Worker #define BASE_FILES_FILE_PATH_H_ 104*6777b538SAndroid Build Coastguard Worker 105*6777b538SAndroid Build Coastguard Worker #include <cstddef> 106*6777b538SAndroid Build Coastguard Worker #include <iosfwd> 107*6777b538SAndroid Build Coastguard Worker #include <string> 108*6777b538SAndroid Build Coastguard Worker #include <string_view> 109*6777b538SAndroid Build Coastguard Worker #include <vector> 110*6777b538SAndroid Build Coastguard Worker 111*6777b538SAndroid Build Coastguard Worker #include "base/base_export.h" 112*6777b538SAndroid Build Coastguard Worker #include "base/strings/string_piece.h" 113*6777b538SAndroid Build Coastguard Worker #include "base/trace_event/base_tracing_forward.h" 114*6777b538SAndroid Build Coastguard Worker #include "build/build_config.h" 115*6777b538SAndroid Build Coastguard Worker 116*6777b538SAndroid Build Coastguard Worker // Windows-style drive letter support and pathname separator characters can be 117*6777b538SAndroid Build Coastguard Worker // enabled and disabled independently, to aid testing. These #defines are 118*6777b538SAndroid Build Coastguard Worker // here so that the same setting can be used in both the implementation and 119*6777b538SAndroid Build Coastguard Worker // in the unit test. 120*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 121*6777b538SAndroid Build Coastguard Worker #define FILE_PATH_USES_DRIVE_LETTERS 122*6777b538SAndroid Build Coastguard Worker #define FILE_PATH_USES_WIN_SEPARATORS 123*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_WIN) 124*6777b538SAndroid Build Coastguard Worker 125*6777b538SAndroid Build Coastguard Worker // To print path names portably use PRFilePath (based on PRIuS and friends from 126*6777b538SAndroid Build Coastguard Worker // C99 and format_macros.h) like this: 127*6777b538SAndroid Build Coastguard Worker // base::StringPrintf("Path is %" PRFilePath ".\n", path.value().c_str()); 128*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 129*6777b538SAndroid Build Coastguard Worker #define PRFilePath "ls" 130*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 131*6777b538SAndroid Build Coastguard Worker #define PRFilePath "s" 132*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_WIN) 133*6777b538SAndroid Build Coastguard Worker 134*6777b538SAndroid Build Coastguard Worker // Macros for string literal initialization of FilePath::CharType[]. 135*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 136*6777b538SAndroid Build Coastguard Worker 137*6777b538SAndroid Build Coastguard Worker // The `FILE_PATH_LITERAL_INTERNAL` indirection allows `FILE_PATH_LITERAL` to 138*6777b538SAndroid Build Coastguard Worker // work correctly with macro parameters, for example 139*6777b538SAndroid Build Coastguard Worker // `FILE_PATH_LITERAL(TEST_FILE)` where `TEST_FILE` is a macro #defined as 140*6777b538SAndroid Build Coastguard Worker // "TestFile". 141*6777b538SAndroid Build Coastguard Worker #define FILE_PATH_LITERAL_INTERNAL(x) L##x 142*6777b538SAndroid Build Coastguard Worker #define FILE_PATH_LITERAL(x) FILE_PATH_LITERAL_INTERNAL(x) 143*6777b538SAndroid Build Coastguard Worker 144*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 145*6777b538SAndroid Build Coastguard Worker #define FILE_PATH_LITERAL(x) x 146*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_WIN) 147*6777b538SAndroid Build Coastguard Worker 148*6777b538SAndroid Build Coastguard Worker namespace base { 149*6777b538SAndroid Build Coastguard Worker 150*6777b538SAndroid Build Coastguard Worker class SafeBaseName; 151*6777b538SAndroid Build Coastguard Worker class Pickle; 152*6777b538SAndroid Build Coastguard Worker class PickleIterator; 153*6777b538SAndroid Build Coastguard Worker 154*6777b538SAndroid Build Coastguard Worker // An abstraction to isolate users from the differences between native 155*6777b538SAndroid Build Coastguard Worker // pathnames on different platforms. 156*6777b538SAndroid Build Coastguard Worker class BASE_EXPORT FilePath { 157*6777b538SAndroid Build Coastguard Worker public: 158*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_WIN) 159*6777b538SAndroid Build Coastguard Worker // On Windows, for Unicode-aware applications, native pathnames are wchar_t 160*6777b538SAndroid Build Coastguard Worker // arrays encoded in UTF-16. 161*6777b538SAndroid Build Coastguard Worker typedef std::wstring StringType; 162*6777b538SAndroid Build Coastguard Worker #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) 163*6777b538SAndroid Build Coastguard Worker // On most platforms, native pathnames are char arrays, and the encoding 164*6777b538SAndroid Build Coastguard Worker // may or may not be specified. On Mac OS X, native pathnames are encoded 165*6777b538SAndroid Build Coastguard Worker // in UTF-8. 166*6777b538SAndroid Build Coastguard Worker typedef std::string StringType; 167*6777b538SAndroid Build Coastguard Worker #endif // BUILDFLAG(IS_WIN) 168*6777b538SAndroid Build Coastguard Worker 169*6777b538SAndroid Build Coastguard Worker typedef StringType::value_type CharType; 170*6777b538SAndroid Build Coastguard Worker typedef std::basic_string_view<CharType> StringPieceType; 171*6777b538SAndroid Build Coastguard Worker 172*6777b538SAndroid Build Coastguard Worker // Null-terminated array of separators used to separate components in paths. 173*6777b538SAndroid Build Coastguard Worker // Each character in this array is a valid separator, but kSeparators[0] is 174*6777b538SAndroid Build Coastguard Worker // treated as the canonical separator and is used when composing pathnames. 175*6777b538SAndroid Build Coastguard Worker static constexpr CharType kSeparators[] = 176*6777b538SAndroid Build Coastguard Worker #if defined(FILE_PATH_USES_WIN_SEPARATORS) 177*6777b538SAndroid Build Coastguard Worker FILE_PATH_LITERAL("\\/"); 178*6777b538SAndroid Build Coastguard Worker #else // FILE_PATH_USES_WIN_SEPARATORS 179*6777b538SAndroid Build Coastguard Worker FILE_PATH_LITERAL("/"); 180*6777b538SAndroid Build Coastguard Worker #endif // FILE_PATH_USES_WIN_SEPARATORS 181*6777b538SAndroid Build Coastguard Worker 182*6777b538SAndroid Build Coastguard Worker // std::size(kSeparators), i.e., the number of separators in kSeparators plus 183*6777b538SAndroid Build Coastguard Worker // one (the null terminator at the end of kSeparators). 184*6777b538SAndroid Build Coastguard Worker static constexpr size_t kSeparatorsLength = std::size(kSeparators); 185*6777b538SAndroid Build Coastguard Worker 186*6777b538SAndroid Build Coastguard Worker // The special path component meaning "this directory." 187*6777b538SAndroid Build Coastguard Worker static constexpr CharType kCurrentDirectory[] = FILE_PATH_LITERAL("."); 188*6777b538SAndroid Build Coastguard Worker 189*6777b538SAndroid Build Coastguard Worker // The special path component meaning "the parent directory." 190*6777b538SAndroid Build Coastguard Worker static constexpr CharType kParentDirectory[] = FILE_PATH_LITERAL(".."); 191*6777b538SAndroid Build Coastguard Worker 192*6777b538SAndroid Build Coastguard Worker // The character used to identify a file extension. 193*6777b538SAndroid Build Coastguard Worker static constexpr CharType kExtensionSeparator = FILE_PATH_LITERAL('.'); 194*6777b538SAndroid Build Coastguard Worker 195*6777b538SAndroid Build Coastguard Worker FilePath(); 196*6777b538SAndroid Build Coastguard Worker FilePath(const FilePath& that); 197*6777b538SAndroid Build Coastguard Worker explicit FilePath(StringPieceType path); 198*6777b538SAndroid Build Coastguard Worker ~FilePath(); 199*6777b538SAndroid Build Coastguard Worker FilePath& operator=(const FilePath& that); 200*6777b538SAndroid Build Coastguard Worker 201*6777b538SAndroid Build Coastguard Worker // Constructs FilePath with the contents of |that|, which is left in valid but 202*6777b538SAndroid Build Coastguard Worker // unspecified state. 203*6777b538SAndroid Build Coastguard Worker FilePath(FilePath&& that) noexcept; 204*6777b538SAndroid Build Coastguard Worker // Replaces the contents with those of |that|, which is left in valid but 205*6777b538SAndroid Build Coastguard Worker // unspecified state. 206*6777b538SAndroid Build Coastguard Worker FilePath& operator=(FilePath&& that) noexcept; 207*6777b538SAndroid Build Coastguard Worker 208*6777b538SAndroid Build Coastguard Worker bool operator==(const FilePath& that) const; 209*6777b538SAndroid Build Coastguard Worker 210*6777b538SAndroid Build Coastguard Worker bool operator!=(const FilePath& that) const; 211*6777b538SAndroid Build Coastguard Worker 212*6777b538SAndroid Build Coastguard Worker // Required for some STL containers and operations 213*6777b538SAndroid Build Coastguard Worker bool operator<(const FilePath& that) const { 214*6777b538SAndroid Build Coastguard Worker return path_ < that.path_; 215*6777b538SAndroid Build Coastguard Worker } 216*6777b538SAndroid Build Coastguard Worker value()217*6777b538SAndroid Build Coastguard Worker const StringType& value() const { return path_; } 218*6777b538SAndroid Build Coastguard Worker empty()219*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool empty() const { return path_.empty(); } 220*6777b538SAndroid Build Coastguard Worker clear()221*6777b538SAndroid Build Coastguard Worker void clear() { path_.clear(); } 222*6777b538SAndroid Build Coastguard Worker 223*6777b538SAndroid Build Coastguard Worker // Returns true if |character| is in kSeparators. 224*6777b538SAndroid Build Coastguard Worker static bool IsSeparator(CharType character); 225*6777b538SAndroid Build Coastguard Worker 226*6777b538SAndroid Build Coastguard Worker // Returns a vector of all of the components of the provided path. It is 227*6777b538SAndroid Build Coastguard Worker // equivalent to calling DirName().value() on the path's root component, 228*6777b538SAndroid Build Coastguard Worker // and BaseName().value() on each child component. 229*6777b538SAndroid Build Coastguard Worker // 230*6777b538SAndroid Build Coastguard Worker // To make sure this is lossless so we can differentiate absolute and 231*6777b538SAndroid Build Coastguard Worker // relative paths, the root slash will be included even though no other 232*6777b538SAndroid Build Coastguard Worker // slashes will be. The precise behavior is: 233*6777b538SAndroid Build Coastguard Worker // 234*6777b538SAndroid Build Coastguard Worker // Posix: "/foo/bar" -> [ "/", "foo", "bar" ] 235*6777b538SAndroid Build Coastguard Worker // Windows: "C:\foo\bar" -> [ "C:", "\\", "foo", "bar" ] 236*6777b538SAndroid Build Coastguard Worker std::vector<FilePath::StringType> GetComponents() const; 237*6777b538SAndroid Build Coastguard Worker 238*6777b538SAndroid Build Coastguard Worker // Returns true if this FilePath is a parent or ancestor of the |child|. 239*6777b538SAndroid Build Coastguard Worker // Absolute and relative paths are accepted i.e. /foo is a parent to /foo/bar, 240*6777b538SAndroid Build Coastguard Worker // and foo is a parent to foo/bar. Any ancestor is considered a parent i.e. /a 241*6777b538SAndroid Build Coastguard Worker // is a parent to both /a/b and /a/b/c. Does not convert paths to absolute, 242*6777b538SAndroid Build Coastguard Worker // follow symlinks or directory navigation (e.g. ".."). A path is *NOT* its 243*6777b538SAndroid Build Coastguard Worker // own parent. 244*6777b538SAndroid Build Coastguard Worker bool IsParent(const FilePath& child) const; 245*6777b538SAndroid Build Coastguard Worker 246*6777b538SAndroid Build Coastguard Worker // If IsParent(child) holds, appends to path (if non-NULL) the 247*6777b538SAndroid Build Coastguard Worker // relative path to child and returns true. For example, if parent 248*6777b538SAndroid Build Coastguard Worker // holds "/Users/johndoe/Library/Application Support", child holds 249*6777b538SAndroid Build Coastguard Worker // "/Users/johndoe/Library/Application Support/Google/Chrome/Default", and 250*6777b538SAndroid Build Coastguard Worker // *path holds "/Users/johndoe/Library/Caches", then after 251*6777b538SAndroid Build Coastguard Worker // parent.AppendRelativePath(child, path) is called *path will hold 252*6777b538SAndroid Build Coastguard Worker // "/Users/johndoe/Library/Caches/Google/Chrome/Default". Otherwise, 253*6777b538SAndroid Build Coastguard Worker // returns false. 254*6777b538SAndroid Build Coastguard Worker bool AppendRelativePath(const FilePath& child, FilePath* path) const; 255*6777b538SAndroid Build Coastguard Worker 256*6777b538SAndroid Build Coastguard Worker // Returns a FilePath corresponding to the directory containing the path 257*6777b538SAndroid Build Coastguard Worker // named by this object, stripping away the file component. If this object 258*6777b538SAndroid Build Coastguard Worker // only contains one component, returns a FilePath identifying 259*6777b538SAndroid Build Coastguard Worker // kCurrentDirectory. If this object already refers to the root directory, 260*6777b538SAndroid Build Coastguard Worker // returns a FilePath identifying the root directory. Please note that this 261*6777b538SAndroid Build Coastguard Worker // doesn't resolve directory navigation, e.g. the result for "../a" is "..". 262*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath DirName() const; 263*6777b538SAndroid Build Coastguard Worker 264*6777b538SAndroid Build Coastguard Worker // Returns a FilePath corresponding to the last path component of this 265*6777b538SAndroid Build Coastguard Worker // object, either a file or a directory. If this object already refers to 266*6777b538SAndroid Build Coastguard Worker // the root directory, returns a FilePath identifying the root directory; 267*6777b538SAndroid Build Coastguard Worker // this is the only situation in which BaseName will return an absolute path. 268*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath BaseName() const; 269*6777b538SAndroid Build Coastguard Worker 270*6777b538SAndroid Build Coastguard Worker // Returns the extension of a file path. This method works very similarly to 271*6777b538SAndroid Build Coastguard Worker // FinalExtension(), except when the file path ends with a common 272*6777b538SAndroid Build Coastguard Worker // double-extension. For common double-extensions like ".tar.gz" and 273*6777b538SAndroid Build Coastguard Worker // ".user.js", this method returns the combined extension. 274*6777b538SAndroid Build Coastguard Worker // 275*6777b538SAndroid Build Coastguard Worker // Common means that detecting double-extensions is based on a hard-coded 276*6777b538SAndroid Build Coastguard Worker // allow-list (including but not limited to ".*.gz" and ".user.js") and isn't 277*6777b538SAndroid Build Coastguard Worker // solely dependent on the number of dots. Specifically, even if somebody 278*6777b538SAndroid Build Coastguard Worker // invents a new Blah compression algorithm: 279*6777b538SAndroid Build Coastguard Worker // - calling this function with "foo.tar.bz2" will return ".tar.bz2", but 280*6777b538SAndroid Build Coastguard Worker // - calling this function with "foo.tar.blah" will return just ".blah" 281*6777b538SAndroid Build Coastguard Worker // until ".*.blah" is added to the hard-coded allow-list. 282*6777b538SAndroid Build Coastguard Worker // 283*6777b538SAndroid Build Coastguard Worker // That hard-coded allow-list is case-insensitive: ".GZ" and ".gz" are 284*6777b538SAndroid Build Coastguard Worker // equivalent. However, the StringType returned is not canonicalized for 285*6777b538SAndroid Build Coastguard Worker // case: "foo.TAR.bz2" input will produce ".TAR.bz2", not ".tar.bz2", and 286*6777b538SAndroid Build Coastguard Worker // "bar.EXT", which is not a double-extension, will produce ".EXT". 287*6777b538SAndroid Build Coastguard Worker // 288*6777b538SAndroid Build Coastguard Worker // The following code should always work regardless of the value of path. 289*6777b538SAndroid Build Coastguard Worker // new_path = path.RemoveExtension().value().append(path.Extension()); 290*6777b538SAndroid Build Coastguard Worker // ASSERT(new_path == path.value()); 291*6777b538SAndroid Build Coastguard Worker // 292*6777b538SAndroid Build Coastguard Worker // NOTE: this is different from the original file_util implementation which 293*6777b538SAndroid Build Coastguard Worker // returned the extension without a leading "." ("jpg" instead of ".jpg"). 294*6777b538SAndroid Build Coastguard Worker [[nodiscard]] StringType Extension() const; 295*6777b538SAndroid Build Coastguard Worker 296*6777b538SAndroid Build Coastguard Worker // Returns the final extension of a file path, or an empty string if the file 297*6777b538SAndroid Build Coastguard Worker // path has no extension. In most cases, the final extension of a file path 298*6777b538SAndroid Build Coastguard Worker // refers to the part of the file path from the last dot to the end (including 299*6777b538SAndroid Build Coastguard Worker // the dot itself). For example, this method applied to "/pics/jojo.jpg" 300*6777b538SAndroid Build Coastguard Worker // and "/pics/jojo." returns ".jpg" and ".", respectively. However, if the 301*6777b538SAndroid Build Coastguard Worker // base name of the file path is either "." or "..", this method returns an 302*6777b538SAndroid Build Coastguard Worker // empty string. 303*6777b538SAndroid Build Coastguard Worker // 304*6777b538SAndroid Build Coastguard Worker // TODO(davidben): Check all our extension-sensitive code to see if 305*6777b538SAndroid Build Coastguard Worker // we can rename this to Extension() and the other to something like 306*6777b538SAndroid Build Coastguard Worker // LongExtension(), defaulting to short extensions and leaving the 307*6777b538SAndroid Build Coastguard Worker // long "extensions" to logic like base::GetUniquePathNumber(). 308*6777b538SAndroid Build Coastguard Worker [[nodiscard]] StringType FinalExtension() const; 309*6777b538SAndroid Build Coastguard Worker 310*6777b538SAndroid Build Coastguard Worker // Returns "C:\pics\jojo" for path "C:\pics\jojo.jpg" 311*6777b538SAndroid Build Coastguard Worker // NOTE: this is slightly different from the similar file_util implementation 312*6777b538SAndroid Build Coastguard Worker // which returned simply 'jojo'. 313*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath RemoveExtension() const; 314*6777b538SAndroid Build Coastguard Worker 315*6777b538SAndroid Build Coastguard Worker // Removes the path's file extension, as in RemoveExtension(), but 316*6777b538SAndroid Build Coastguard Worker // ignores double extensions. 317*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath RemoveFinalExtension() const; 318*6777b538SAndroid Build Coastguard Worker 319*6777b538SAndroid Build Coastguard Worker // Inserts |suffix| after the file name portion of |path| but before the 320*6777b538SAndroid Build Coastguard Worker // extension. Returns "" if BaseName() == "." or "..". 321*6777b538SAndroid Build Coastguard Worker // Examples: 322*6777b538SAndroid Build Coastguard Worker // path == "C:\pics\jojo.jpg" suffix == " (1)", returns "C:\pics\jojo (1).jpg" 323*6777b538SAndroid Build Coastguard Worker // path == "jojo.jpg" suffix == " (1)", returns "jojo (1).jpg" 324*6777b538SAndroid Build Coastguard Worker // path == "C:\pics\jojo" suffix == " (1)", returns "C:\pics\jojo (1)" 325*6777b538SAndroid Build Coastguard Worker // path == "C:\pics.old\jojo" suffix == " (1)", returns "C:\pics.old\jojo (1)" 326*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath InsertBeforeExtension(StringPieceType suffix) const; 327*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath InsertBeforeExtensionASCII(StringPiece suffix) const; 328*6777b538SAndroid Build Coastguard Worker 329*6777b538SAndroid Build Coastguard Worker // Adds |extension| to |file_name|. Returns the current FilePath if 330*6777b538SAndroid Build Coastguard Worker // |extension| is empty. Returns "" if BaseName() == "." or "..". 331*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath AddExtension(StringPieceType extension) const; 332*6777b538SAndroid Build Coastguard Worker 333*6777b538SAndroid Build Coastguard Worker // Like above, but takes the extension as an ASCII string. See AppendASCII for 334*6777b538SAndroid Build Coastguard Worker // details on how this is handled. 335*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath AddExtensionASCII(StringPiece extension) const; 336*6777b538SAndroid Build Coastguard Worker 337*6777b538SAndroid Build Coastguard Worker // Replaces the extension of |file_name| with |extension|. If |file_name| 338*6777b538SAndroid Build Coastguard Worker // does not have an extension, then |extension| is added. If |extension| is 339*6777b538SAndroid Build Coastguard Worker // empty, then the extension is removed from |file_name|. 340*6777b538SAndroid Build Coastguard Worker // Returns "" if BaseName() == "." or "..". 341*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath ReplaceExtension(StringPieceType extension) const; 342*6777b538SAndroid Build Coastguard Worker 343*6777b538SAndroid Build Coastguard Worker // Returns true if file path's Extension() matches `extension`. The test is 344*6777b538SAndroid Build Coastguard Worker // case insensitive. Don't forget the leading period if appropriate. 345*6777b538SAndroid Build Coastguard Worker bool MatchesExtension(StringPieceType extension) const; 346*6777b538SAndroid Build Coastguard Worker 347*6777b538SAndroid Build Coastguard Worker // Returns true if file path's FinalExtension() matches `extension`. The 348*6777b538SAndroid Build Coastguard Worker // test is case insensitive. Don't forget the leading period if appropriate. 349*6777b538SAndroid Build Coastguard Worker bool MatchesFinalExtension(StringPieceType extension) const; 350*6777b538SAndroid Build Coastguard Worker 351*6777b538SAndroid Build Coastguard Worker // Returns a FilePath by appending a separator and the supplied path 352*6777b538SAndroid Build Coastguard Worker // component to this object's path. Append takes care to avoid adding 353*6777b538SAndroid Build Coastguard Worker // excessive separators if this object's path already ends with a separator. 354*6777b538SAndroid Build Coastguard Worker // If this object's path is kCurrentDirectory ('.'), a new FilePath 355*6777b538SAndroid Build Coastguard Worker // corresponding only to |component| is returned. |component| must be a 356*6777b538SAndroid Build Coastguard Worker // relative path; it is an error to pass an absolute path. 357*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath Append(StringPieceType component) const; 358*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath Append(const FilePath& component) const; 359*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath Append(const SafeBaseName& component) const; 360*6777b538SAndroid Build Coastguard Worker 361*6777b538SAndroid Build Coastguard Worker // Although Windows StringType is std::wstring, since the encoding it uses for 362*6777b538SAndroid Build Coastguard Worker // paths is well defined, it can handle ASCII path components as well. 363*6777b538SAndroid Build Coastguard Worker // Mac uses UTF8, and since ASCII is a subset of that, it works there as well. 364*6777b538SAndroid Build Coastguard Worker // On Linux, although it can use any 8-bit encoding for paths, we assume that 365*6777b538SAndroid Build Coastguard Worker // ASCII is a valid subset, regardless of the encoding, since many operating 366*6777b538SAndroid Build Coastguard Worker // system paths will always be ASCII. 367*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath AppendASCII(StringPiece component) const; 368*6777b538SAndroid Build Coastguard Worker 369*6777b538SAndroid Build Coastguard Worker // Returns true if this FilePath contains an absolute path. On Windows, an 370*6777b538SAndroid Build Coastguard Worker // absolute path begins with either a drive letter specification followed by 371*6777b538SAndroid Build Coastguard Worker // a separator character, or with two separator characters. On POSIX 372*6777b538SAndroid Build Coastguard Worker // platforms, an absolute path begins with a separator character. 373*6777b538SAndroid Build Coastguard Worker bool IsAbsolute() const; 374*6777b538SAndroid Build Coastguard Worker 375*6777b538SAndroid Build Coastguard Worker // Returns true if this FilePath is a network path which starts with 2 path 376*6777b538SAndroid Build Coastguard Worker // separators. See class documentation for 'Alternate root'. 377*6777b538SAndroid Build Coastguard Worker bool IsNetwork() const; 378*6777b538SAndroid Build Coastguard Worker 379*6777b538SAndroid Build Coastguard Worker // Returns true if the patch ends with a path separator character. 380*6777b538SAndroid Build Coastguard Worker [[nodiscard]] bool EndsWithSeparator() const; 381*6777b538SAndroid Build Coastguard Worker 382*6777b538SAndroid Build Coastguard Worker // Returns a copy of this FilePath that ends with a trailing separator. If 383*6777b538SAndroid Build Coastguard Worker // the input path is empty, an empty FilePath will be returned. 384*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath AsEndingWithSeparator() const; 385*6777b538SAndroid Build Coastguard Worker 386*6777b538SAndroid Build Coastguard Worker // Returns a copy of this FilePath that does not end with a trailing 387*6777b538SAndroid Build Coastguard Worker // separator. 388*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath StripTrailingSeparators() const; 389*6777b538SAndroid Build Coastguard Worker 390*6777b538SAndroid Build Coastguard Worker // Returns true if this FilePath contains an attempt to reference a parent 391*6777b538SAndroid Build Coastguard Worker // directory (e.g. has a path component that is ".."). 392*6777b538SAndroid Build Coastguard Worker bool ReferencesParent() const; 393*6777b538SAndroid Build Coastguard Worker 394*6777b538SAndroid Build Coastguard Worker // Return a Unicode human-readable version of this path. 395*6777b538SAndroid Build Coastguard Worker // Warning: you can *not*, in general, go from a display name back to a real 396*6777b538SAndroid Build Coastguard Worker // path. Only use this when displaying paths to users, not just when you 397*6777b538SAndroid Build Coastguard Worker // want to stuff a std::u16string into some other API. 398*6777b538SAndroid Build Coastguard Worker std::u16string LossyDisplayName() const; 399*6777b538SAndroid Build Coastguard Worker 400*6777b538SAndroid Build Coastguard Worker // Return the path as ASCII, or the empty string if the path is not ASCII. 401*6777b538SAndroid Build Coastguard Worker // This should only be used for cases where the FilePath is representing a 402*6777b538SAndroid Build Coastguard Worker // known-ASCII filename. 403*6777b538SAndroid Build Coastguard Worker std::string MaybeAsASCII() const; 404*6777b538SAndroid Build Coastguard Worker 405*6777b538SAndroid Build Coastguard Worker // Return the path as UTF-8. 406*6777b538SAndroid Build Coastguard Worker // 407*6777b538SAndroid Build Coastguard Worker // This function is *unsafe* as there is no way to tell what encoding is 408*6777b538SAndroid Build Coastguard Worker // used in file names on POSIX systems other than Mac and Chrome OS, 409*6777b538SAndroid Build Coastguard Worker // although UTF-8 is practically used everywhere these days. To mitigate 410*6777b538SAndroid Build Coastguard Worker // the encoding issue, this function internally calls 411*6777b538SAndroid Build Coastguard Worker // SysNativeMBToWide() on POSIX systems other than Mac and Chrome OS, 412*6777b538SAndroid Build Coastguard Worker // per assumption that the current locale's encoding is used in file 413*6777b538SAndroid Build Coastguard Worker // names, but this isn't a perfect solution. 414*6777b538SAndroid Build Coastguard Worker // 415*6777b538SAndroid Build Coastguard Worker // Once it becomes safe to to stop caring about non-UTF-8 file names, 416*6777b538SAndroid Build Coastguard Worker // the SysNativeMBToWide() hack will be removed from the code, along 417*6777b538SAndroid Build Coastguard Worker // with "Unsafe" in the function name. 418*6777b538SAndroid Build Coastguard Worker std::string AsUTF8Unsafe() const; 419*6777b538SAndroid Build Coastguard Worker 420*6777b538SAndroid Build Coastguard Worker // Similar to AsUTF8Unsafe, but returns UTF-16 instead. 421*6777b538SAndroid Build Coastguard Worker std::u16string AsUTF16Unsafe() const; 422*6777b538SAndroid Build Coastguard Worker 423*6777b538SAndroid Build Coastguard Worker // Returns a FilePath object from a path name in ASCII. 424*6777b538SAndroid Build Coastguard Worker static FilePath FromASCII(StringPiece ascii); 425*6777b538SAndroid Build Coastguard Worker 426*6777b538SAndroid Build Coastguard Worker // Returns a FilePath object from a path name in UTF-8. This function 427*6777b538SAndroid Build Coastguard Worker // should only be used for cases where you are sure that the input 428*6777b538SAndroid Build Coastguard Worker // string is UTF-8. 429*6777b538SAndroid Build Coastguard Worker // 430*6777b538SAndroid Build Coastguard Worker // Like AsUTF8Unsafe(), this function is unsafe. This function 431*6777b538SAndroid Build Coastguard Worker // internally calls SysWideToNativeMB() on POSIX systems other than Mac 432*6777b538SAndroid Build Coastguard Worker // and Chrome OS, to mitigate the encoding issue. See the comment at 433*6777b538SAndroid Build Coastguard Worker // AsUTF8Unsafe() for details. 434*6777b538SAndroid Build Coastguard Worker static FilePath FromUTF8Unsafe(StringPiece utf8); 435*6777b538SAndroid Build Coastguard Worker 436*6777b538SAndroid Build Coastguard Worker // Similar to FromUTF8Unsafe, but accepts UTF-16 instead. 437*6777b538SAndroid Build Coastguard Worker static FilePath FromUTF16Unsafe(StringPiece16 utf16); 438*6777b538SAndroid Build Coastguard Worker 439*6777b538SAndroid Build Coastguard Worker void WriteToPickle(Pickle* pickle) const; 440*6777b538SAndroid Build Coastguard Worker bool ReadFromPickle(PickleIterator* iter); 441*6777b538SAndroid Build Coastguard Worker 442*6777b538SAndroid Build Coastguard Worker // Normalize all path separators to backslash on Windows 443*6777b538SAndroid Build Coastguard Worker // (if FILE_PATH_USES_WIN_SEPARATORS is true), or do nothing on POSIX systems. 444*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath NormalizePathSeparators() const; 445*6777b538SAndroid Build Coastguard Worker 446*6777b538SAndroid Build Coastguard Worker // Normalize all path separattors to given type on Windows 447*6777b538SAndroid Build Coastguard Worker // (if FILE_PATH_USES_WIN_SEPARATORS is true), or do nothing on POSIX systems. 448*6777b538SAndroid Build Coastguard Worker [[nodiscard]] FilePath NormalizePathSeparatorsTo(CharType separator) const; 449*6777b538SAndroid Build Coastguard Worker 450*6777b538SAndroid Build Coastguard Worker // Compare two strings in the same way the file system does. 451*6777b538SAndroid Build Coastguard Worker // Note that these always ignore case, even on file systems that are case- 452*6777b538SAndroid Build Coastguard Worker // sensitive. If case-sensitive comparison is ever needed, add corresponding 453*6777b538SAndroid Build Coastguard Worker // methods here. 454*6777b538SAndroid Build Coastguard Worker // The methods are written as a static method so that they can also be used 455*6777b538SAndroid Build Coastguard Worker // on parts of a file path, e.g., just the extension. 456*6777b538SAndroid Build Coastguard Worker // CompareIgnoreCase() returns -1, 0 or 1 for less-than, equal-to and 457*6777b538SAndroid Build Coastguard Worker // greater-than respectively. 458*6777b538SAndroid Build Coastguard Worker static int CompareIgnoreCase(StringPieceType string1, 459*6777b538SAndroid Build Coastguard Worker StringPieceType string2); CompareEqualIgnoreCase(StringPieceType string1,StringPieceType string2)460*6777b538SAndroid Build Coastguard Worker static bool CompareEqualIgnoreCase(StringPieceType string1, 461*6777b538SAndroid Build Coastguard Worker StringPieceType string2) { 462*6777b538SAndroid Build Coastguard Worker return CompareIgnoreCase(string1, string2) == 0; 463*6777b538SAndroid Build Coastguard Worker } CompareLessIgnoreCase(StringPieceType string1,StringPieceType string2)464*6777b538SAndroid Build Coastguard Worker static bool CompareLessIgnoreCase(StringPieceType string1, 465*6777b538SAndroid Build Coastguard Worker StringPieceType string2) { 466*6777b538SAndroid Build Coastguard Worker return CompareIgnoreCase(string1, string2) < 0; 467*6777b538SAndroid Build Coastguard Worker } 468*6777b538SAndroid Build Coastguard Worker 469*6777b538SAndroid Build Coastguard Worker // Serialise this object into a trace. 470*6777b538SAndroid Build Coastguard Worker void WriteIntoTrace(perfetto::TracedValue context) const; 471*6777b538SAndroid Build Coastguard Worker 472*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_APPLE) 473*6777b538SAndroid Build Coastguard Worker // Returns the string in the special canonical decomposed form as defined for 474*6777b538SAndroid Build Coastguard Worker // HFS, which is close to, but not quite, decomposition form D. See 475*6777b538SAndroid Build Coastguard Worker // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#UnicodeSubtleties 476*6777b538SAndroid Build Coastguard Worker // for further comments. 477*6777b538SAndroid Build Coastguard Worker // Returns the epmty string if the conversion failed. 478*6777b538SAndroid Build Coastguard Worker static StringType GetHFSDecomposedForm(StringPieceType string); 479*6777b538SAndroid Build Coastguard Worker 480*6777b538SAndroid Build Coastguard Worker // Special UTF-8 version of FastUnicodeCompare. Cf: 481*6777b538SAndroid Build Coastguard Worker // http://developer.apple.com/mac/library/technotes/tn/tn1150.html#StringComparisonAlgorithm 482*6777b538SAndroid Build Coastguard Worker // IMPORTANT: The input strings must be in the special HFS decomposed form! 483*6777b538SAndroid Build Coastguard Worker // (cf. above GetHFSDecomposedForm method) 484*6777b538SAndroid Build Coastguard Worker static int HFSFastUnicodeCompare(StringPieceType string1, 485*6777b538SAndroid Build Coastguard Worker StringPieceType string2); 486*6777b538SAndroid Build Coastguard Worker #endif 487*6777b538SAndroid Build Coastguard Worker 488*6777b538SAndroid Build Coastguard Worker #if BUILDFLAG(IS_ANDROID) 489*6777b538SAndroid Build Coastguard Worker // On android, file selection dialog can return a file with content uri 490*6777b538SAndroid Build Coastguard Worker // scheme(starting with content://). Content uri needs to be opened with 491*6777b538SAndroid Build Coastguard Worker // ContentResolver to guarantee that the app has appropriate permissions 492*6777b538SAndroid Build Coastguard Worker // to access it. 493*6777b538SAndroid Build Coastguard Worker // Returns true if the path is a content uri, or false otherwise. 494*6777b538SAndroid Build Coastguard Worker bool IsContentUri() const; 495*6777b538SAndroid Build Coastguard Worker #endif 496*6777b538SAndroid Build Coastguard Worker 497*6777b538SAndroid Build Coastguard Worker // NOTE: When adding a new public method, consider adding it to 498*6777b538SAndroid Build Coastguard Worker // file_path_fuzzer.cc as well. 499*6777b538SAndroid Build Coastguard Worker 500*6777b538SAndroid Build Coastguard Worker private: 501*6777b538SAndroid Build Coastguard Worker // Remove trailing separators from this object. If the path is absolute, it 502*6777b538SAndroid Build Coastguard Worker // will never be stripped any more than to refer to the absolute root 503*6777b538SAndroid Build Coastguard Worker // directory, so "////" will become "/", not "". A leading pair of 504*6777b538SAndroid Build Coastguard Worker // separators is never stripped, to support alternate roots. This is used to 505*6777b538SAndroid Build Coastguard Worker // support UNC paths on Windows. 506*6777b538SAndroid Build Coastguard Worker void StripTrailingSeparatorsInternal(); 507*6777b538SAndroid Build Coastguard Worker 508*6777b538SAndroid Build Coastguard Worker StringType path_; 509*6777b538SAndroid Build Coastguard Worker }; 510*6777b538SAndroid Build Coastguard Worker 511*6777b538SAndroid Build Coastguard Worker BASE_EXPORT std::ostream& operator<<(std::ostream& out, 512*6777b538SAndroid Build Coastguard Worker const FilePath& file_path); 513*6777b538SAndroid Build Coastguard Worker 514*6777b538SAndroid Build Coastguard Worker } // namespace base 515*6777b538SAndroid Build Coastguard Worker 516*6777b538SAndroid Build Coastguard Worker namespace std { 517*6777b538SAndroid Build Coastguard Worker 518*6777b538SAndroid Build Coastguard Worker template <> 519*6777b538SAndroid Build Coastguard Worker struct hash<base::FilePath> { 520*6777b538SAndroid Build Coastguard Worker typedef base::FilePath argument_type; 521*6777b538SAndroid Build Coastguard Worker typedef std::size_t result_type; 522*6777b538SAndroid Build Coastguard Worker result_type operator()(argument_type const& f) const { 523*6777b538SAndroid Build Coastguard Worker return hash<base::FilePath::StringType>()(f.value()); 524*6777b538SAndroid Build Coastguard Worker } 525*6777b538SAndroid Build Coastguard Worker }; 526*6777b538SAndroid Build Coastguard Worker 527*6777b538SAndroid Build Coastguard Worker } // namespace std 528*6777b538SAndroid Build Coastguard Worker 529*6777b538SAndroid Build Coastguard Worker #endif // BASE_FILES_FILE_PATH_H_ 530