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