file_path.h 24 KB

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