blob: b1eec8bc004a0097fad426fcd99888b2d4380617 [file] [log] [blame]
kjellander@webrtc.org7951e812011-10-13 12:24:41 +00001/*
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
pbos@webrtc.org12dc1a32013-08-05 16:22:53 +000011#include <stdio.h>
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +000012
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020013#ifndef TEST_TESTSUPPORT_FILEUTILS_H_
14#define TEST_TESTSUPPORT_FILEUTILS_H_
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000015
16#include <string>
alessiob00b16f42017-06-01 03:29:40 -070017#include <vector>
18
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "api/optional.h"
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000020
21namespace webrtc {
22namespace test {
23
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000024// This is the "directory" returned if the ProjectPath() function fails
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000025// to find the project root.
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +000026extern const char* kCannotFindProjectRootDir;
kjellander@webrtc.org7951e812011-10-13 12:24:41 +000027
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000028// Creates and returns the absolute path to the output directory where log files
phoglund@webrtc.org4aa57b42012-03-22 12:56:54 +000029// and other test artifacts should be put. The output directory is generally a
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000030// directory named "out" at the top-level of the project, i.e. a subfolder to
phoglund@webrtc.org4aa57b42012-03-22 12:56:54 +000031// the path returned by ProjectRootPath(). The exception is Android where we use
32// /sdcard/ instead.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000033//
ehmaldonadod661e9c2016-11-22 10:42:59 -080034// If symbolic links occur in the path they will be resolved and the actual
35// directory will be returned.
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000036//
andrew@webrtc.org0db7dc62011-11-13 01:34:05 +000037// Returns the path WITH a trailing path delimiter. If the project root is not
38// found, the current working directory ("./") is returned as a fallback.
39std::string OutputPath();
kjellander@webrtc.org4d8cd9d2011-11-09 11:24:14 +000040
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000041// Generates an empty file with a unique name in the specified directory and
42// returns the file name and path.
Artem Titove62f6002018-03-19 15:40:00 +010043// TODO(titovartem) rename to TempFile and next method to TempFilename
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000044std::string TempFilename(const std::string &dir, const std::string &prefix);
45
Artem Titove62f6002018-03-19 15:40:00 +010046// Generates a unique file name that can be used for file creation. Doesn't
47// create any files.
48std::string GenerateTempFilename(const std::string& dir,
49 const std::string& prefix);
50
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000051// Returns a path to a resource file for the currently executing platform.
52// Adapts to what filenames are currently present in the
53// [project-root]/resources/ dir.
54// Returns an absolute path according to this priority list (the directory
55// part of the path is left out for readability):
56// 1. [name]_[platform]_[architecture].[extension]
57// 2. [name]_[platform].[extension]
58// 3. [name]_[architecture].[extension]
59// 4. [name].[extension]
60// Where
61// * platform is either of "win", "mac" or "linux".
62// * architecture is either of "32" or "64".
63//
64// Arguments:
65// name - Name of the resource file. If a plain filename (no directory path)
66// is supplied, the file is assumed to be located in resources/
67// If a directory path is prepended to the filename, a subdirectory
68// hierarchy reflecting that path is assumed to be present.
69// extension - File extension, without the dot, i.e. "bmp" or "yuv".
Niels Möller392f8d02018-06-01 10:14:44 +020070std::string ResourcePath(const std::string& name, const std::string& extension);
71
72// Joins directory name and file name, separated by the path delimiter.
73std::string JoinFilename(const std::string& dir, const std::string& name);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000074
75// Gets the current working directory for the executing program.
76// Returns "./" if for some reason it is not possible to find the working
77// directory.
78std::string WorkingDir();
79
alessiob00b16f42017-06-01 03:29:40 -070080// Reads the content of a directory and, in case of success, returns a vector
81// of strings with one element for each found file or directory. Each element is
82// a path created by prepending |dir| to the file/directory name. "." and ".."
83// are never added in the returned vector.
84rtc::Optional<std::vector<std::string>> ReadDirectory(std::string path);
85
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000086// Creates a directory if it not already exists.
87// Returns true if successful. Will print an error message to stderr and return
88// false if a file with the same name already exists.
ehmaldonado88df0bc2017-02-10 09:27:14 -080089bool CreateDir(const std::string& directory_name);
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000090
nisse57efb032017-05-18 03:55:59 -070091// Removes a directory, which must already be empty.
92bool RemoveDir(const std::string& directory_name);
93
94// Removes a file.
95bool RemoveFile(const std::string& file_name);
96
kjellander@webrtc.org7de47bc2014-04-16 08:04:26 +000097// Checks if a file exists.
ehmaldonado88df0bc2017-02-10 09:27:14 -080098bool FileExists(const std::string& file_name);
kjellander@webrtc.org4ed4f242011-12-05 16:31:12 +000099
alessiobe49fede2017-03-15 06:04:59 -0700100// Checks if a directory exists.
101bool DirExists(const std::string& directory_name);
102
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000103// File size of the supplied file in bytes. Will return 0 if the file is
104// empty or if the file does not exist/is readable.
ehmaldonado88df0bc2017-02-10 09:27:14 -0800105size_t GetFileSize(const std::string& filename);
kjellander@webrtc.org5b97b122011-12-08 07:42:18 +0000106
kjellander@webrtc.org193600b2012-10-17 04:39:44 +0000107// Sets the executable path, i.e. the path to the executable that is being used
108// when launching it. This is usually the path relative to the working directory
109// but can also be an absolute path. The intention with this function is to pass
110// the argv[0] being sent into the main function to make it possible for
111// fileutils.h to find the correct project paths even when the working directory
112// is outside the project tree (which happens in some cases).
113void SetExecutablePath(const std::string& path_to_executable);
kjellander@webrtc.org83b767b2012-10-15 18:14:12 +0000114
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000115} // namespace test
andrew@webrtc.org1e10bb32011-10-31 20:22:02 +0000116} // namespace webrtc
kjellander@webrtc.org7951e812011-10-13 12:24:41 +0000117
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200118#endif // TEST_TESTSUPPORT_FILEUTILS_H_