file_utils.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <stdio.h>
  11. #ifndef TEST_TESTSUPPORT_FILE_UTILS_H_
  12. #define TEST_TESTSUPPORT_FILE_UTILS_H_
  13. #include <string>
  14. #include <vector>
  15. #include "absl/types/optional.h"
  16. namespace webrtc {
  17. namespace test {
  18. // This is the "directory" returned if the ProjectPath() function fails
  19. // to find the project root.
  20. extern const char* kCannotFindProjectRootDir;
  21. // Slash or backslash, depending on platform. NUL-terminated string.
  22. extern const char* kPathDelimiter;
  23. // Returns the absolute path to the output directory where log files and other
  24. // test artifacts should be put. The output directory is generally a directory
  25. // named "out" at the project root. This root is assumed to be two levels above
  26. // where the test binary is located; this is because tests execute in a dir
  27. // out/Whatever relative to the project root. This convention is also followed
  28. // in Chromium.
  29. //
  30. // The exception is Android where we use /sdcard/ instead.
  31. //
  32. // If symbolic links occur in the path they will be resolved and the actual
  33. // directory will be returned.
  34. //
  35. // Returns the path WITH a trailing path delimiter. If the project root is not
  36. // found, the current working directory ("./") is returned as a fallback.
  37. std::string OutputPath();
  38. // Generates an empty file with a unique name in the specified directory and
  39. // returns the file name and path.
  40. // TODO(titovartem) rename to TempFile and next method to TempFilename
  41. std::string TempFilename(const std::string& dir, const std::string& prefix);
  42. // Generates a unique file name that can be used for file creation. Doesn't
  43. // create any files.
  44. std::string GenerateTempFilename(const std::string& dir,
  45. const std::string& prefix);
  46. // Returns a path to a resource file in [project-root]/resources/ dir.
  47. // Returns an absolute path
  48. //
  49. // Arguments:
  50. // name - Name of the resource file. If a plain filename (no directory path)
  51. // is supplied, the file is assumed to be located in resources/
  52. // If a directory path is prepended to the filename, a subdirectory
  53. // hierarchy reflecting that path is assumed to be present.
  54. // extension - File extension, without the dot, i.e. "bmp" or "yuv".
  55. std::string ResourcePath(const std::string& name, const std::string& extension);
  56. // Joins directory name and file name, separated by the path delimiter.
  57. std::string JoinFilename(const std::string& dir, const std::string& name);
  58. // Gets the current working directory for the executing program.
  59. // Returns "./" if for some reason it is not possible to find the working
  60. // directory.
  61. std::string WorkingDir();
  62. // Reads the content of a directory and, in case of success, returns a vector
  63. // of strings with one element for each found file or directory. Each element is
  64. // a path created by prepending |dir| to the file/directory name. "." and ".."
  65. // are never added in the returned vector.
  66. absl::optional<std::vector<std::string>> ReadDirectory(std::string path);
  67. // Creates a directory if it not already exists.
  68. // Returns true if successful. Will print an error message to stderr and return
  69. // false if a file with the same name already exists.
  70. bool CreateDir(const std::string& directory_name);
  71. // Removes a directory, which must already be empty.
  72. bool RemoveDir(const std::string& directory_name);
  73. // Removes a file.
  74. bool RemoveFile(const std::string& file_name);
  75. // Checks if a file exists.
  76. bool FileExists(const std::string& file_name);
  77. // Checks if a directory exists.
  78. bool DirExists(const std::string& directory_name);
  79. // Strips the rightmost path segment from a path.
  80. std::string DirName(const std::string& path);
  81. // File size of the supplied file in bytes. Will return 0 if the file is
  82. // empty or if the file does not exist/is readable.
  83. size_t GetFileSize(const std::string& filename);
  84. } // namespace test
  85. } // namespace webrtc
  86. #endif // TEST_TESTSUPPORT_FILE_UTILS_H_