Flutter Engine
filesystem Namespace Reference

Classes

class  Descriptor
 

Functions

std::pair< uint8_t *, intptr_t > ReadFileDescriptorToBytes (int fd)
 
bool ReadFileToString (const std::string &path, std::string *result)
 
bool ReadFileDescriptorToString (int fd, std::string *result)
 
std::pair< uint8_t *, intptr_t > ReadFileToBytes (const std::string &path)
 
std::string SimplifyPath (std::string path)
 
std::string AbsolutePath (const std::string &path)
 
std::string GetDirectoryName (const std::string &path)
 
std::string GetBaseName (const std::string &path)
 
std::string GetAbsoluteFilePath (const std::string &path)
 
 TEST (Directory, CreateDirectory)
 
 TEST (FileDescriptor, WriteAndRead)
 
void ExpectPlatformPath (std::string expected, std::string actual)
 
 TEST (Path, SimplifyPath)
 
 TEST (Path, AbsolutePath)
 
 TEST (Path, GetDirectoryName)
 
 TEST (Path, GetBaseName)
 
 TEST (Path, DeletePath)
 
 TEST (Path, DeletePathRecursively)
 
 TEST (ScopedTempDir, Creation)
 
 TEST (ScopedTempDir, Deletion)
 
 TEST (ScopedTempDir, NewTempFile)
 
 TEST (ScopedTempDir, CustomParent)
 

Function Documentation

◆ AbsolutePath()

std::string filesystem::AbsolutePath ( const std::string &  path)

Definition at line 147 of file path_posix.cc.

References flutter::path.

Referenced by GetAbsoluteFilePath(), tonic::FileLoader::LoadPackagesMap(), and TEST().

147  {
148  if (path.size() > 0) {
149  if (path[0] == '/') {
150  // Path is already absolute.
151  return path;
152  }
153  return GetCurrentDirectory() + "/" + path;
154  } else {
155  // Path is empty.
156  return GetCurrentDirectory();
157  }
158 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32

◆ ExpectPlatformPath()

void filesystem::ExpectPlatformPath ( std::string  expected,
std::string  actual 
)

Definition at line 13 of file path_unittest.cc.

Referenced by TEST().

13  {
14 #if defined(OS_WIN)
15  std::replace(expected.begin(), expected.end(), '/', '\\');
16 #endif
17  EXPECT_EQ(expected, actual);
18 }

◆ GetAbsoluteFilePath()

std::string filesystem::GetAbsoluteFilePath ( const std::string &  path)

Definition at line 176 of file path_posix.cc.

References AbsolutePath(), and SimplifyPath().

Referenced by tonic::FileLoader::FetchBytes().

176  {
177 #if defined(OS_FUCHSIA)
178  // realpath() isn't supported by Fuchsia. See MG-425.
179  return SimplifyPath(AbsolutePath(path));
180 #else
181  char buffer[PATH_MAX];
182  if (realpath(path.c_str(), buffer) == nullptr)
183  return std::string();
184  return buffer;
185 #endif // defined(OS_FUCHSIA)
186 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
std::string AbsolutePath(const std::string &path)
Definition: paths_posix.cc:29
std::string SimplifyPath(std::string path)
Definition: path_posix.cc:48

◆ GetBaseName()

std::string filesystem::GetBaseName ( const std::string &  path)

Definition at line 169 of file path_posix.cc.

References flutter::path.

Referenced by TEST().

169  {
170  size_t separator = path.rfind('/');
171  if (separator == std::string::npos)
172  return path;
173  return path.substr(separator + 1);
174 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32

◆ GetDirectoryName()

std::string filesystem::GetDirectoryName ( const std::string &  path)

Definition at line 160 of file path_posix.cc.

Referenced by tonic::FileLoader::CanonicalizeURL(), tonic::FileLoader::LoadPackagesMap(), and TEST().

160  {
161  size_t separator = path.rfind('/');
162  if (separator == 0u)
163  return "/";
164  if (separator == std::string::npos)
165  return std::string();
166  return path.substr(0, separator);
167 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32

◆ ReadFileDescriptorToBytes()

std::pair< uint8_t *, intptr_t > filesystem::ReadFileDescriptorToBytes ( int  fd)

Definition at line 63 of file file.cc.

References HANDLE_EINTR.

Referenced by filesystem::Descriptor::get(), and ReadFileToBytes().

63  {
64  std::pair<uint8_t*, intptr_t> failure_pair{nullptr, -1};
65  struct stat st;
66  if (fstat(fd, &st) != 0) {
67  return failure_pair;
68  }
69  intptr_t file_size = st.st_size;
70  uint8_t* ptr = (uint8_t*)malloc(file_size);
71 
72  size_t bytes_left = file_size;
73  size_t offset = 0;
74  while (bytes_left > 0) {
75  ssize_t bytes_read = HANDLE_EINTR(read(fd, &ptr[offset], bytes_left));
76  if (bytes_read < 0) {
77  return failure_pair;
78  }
79  offset += bytes_read;
80  bytes_left -= bytes_read;
81  }
82  return std::pair<uint8_t*, intptr_t>(ptr, file_size);
83 }
#define HANDLE_EINTR(x)
Definition: eintr_wrapper.h:33

◆ ReadFileDescriptorToString()

bool filesystem::ReadFileDescriptorToString ( int  fd,
std::string *  result 
)

Definition at line 90 of file file.cc.

Referenced by filesystem::Descriptor::get().

90  {
91  return ReadFileDescriptor(fd, result);
92 }

◆ ReadFileToBytes()

std::pair< uint8_t *, intptr_t > filesystem::ReadFileToBytes ( const std::string &  path)

Definition at line 94 of file file.cc.

References BINARY_MODE, filesystem::Descriptor::get(), filesystem::Descriptor::is_valid(), and ReadFileDescriptorToBytes().

Referenced by tonic::FileLoader::FetchBytes(), and filesystem::Descriptor::get().

94  {
95  std::pair<uint8_t*, intptr_t> failure_pair{nullptr, -1};
96  Descriptor fd(open(path.c_str(), O_RDONLY | BINARY_MODE));
97  if (!fd.is_valid())
98  return failure_pair;
99  return ReadFileDescriptorToBytes(fd.get());
100 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
#define BINARY_MODE
Definition: file.cc:18
std::pair< uint8_t *, intptr_t > ReadFileDescriptorToBytes(int fd)
Definition: file.cc:63

◆ ReadFileToString()

bool filesystem::ReadFileToString ( const std::string &  path,
std::string *  result 
)

Definition at line 85 of file file.cc.

References filesystem::Descriptor::get().

Referenced by filesystem::Descriptor::get().

85  {
86  Descriptor fd(open(path.c_str(), O_RDONLY));
87  return ReadFileDescriptor(fd.get(), result);
88 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32

◆ SimplifyPath()

std::string filesystem::SimplifyPath ( std::string  path)

Definition at line 48 of file path_posix.cc.

References flutter::path.

Referenced by tonic::FileLoader::CanonicalizeURL(), tonic::FileLoader::FetchBytes(), GetAbsoluteFilePath(), and TEST().

48  {
49  if (path.empty())
50  return ".";
51 
52  size_t put = 0;
53  size_t get = 0;
54  size_t traversal_root = 0;
55  size_t component_start = 0;
56 
57  if (path[0] == '/') {
58  put = 1;
59  get = 1;
60  component_start = 1;
61  }
62 
63  while (get < path.size()) {
64  char c = path[get];
65 
66  if (c == '.' && (get == component_start || get == component_start + 1)) {
67  // We've seen "." or ".." so far in this component. We need to continue
68  // searching.
69  ++get;
70  continue;
71  }
72 
73  if (c == '/') {
74  if (get == component_start || get == component_start + 1) {
75  // We've found a "/" or a "./", which we can elide.
76  ++get;
77  component_start = get;
78  continue;
79  }
80  if (get == component_start + 2) {
81  // We've found a "../", which means we need to remove the previous
82  // component.
83  if (put == traversal_root) {
84  path[put++] = '.';
85  path[put++] = '.';
86  path[put++] = '/';
87  traversal_root = put;
88  } else {
89  put = ResolveParentDirectoryTraversal(path, put);
90  }
91  ++get;
92  component_start = get;
93  continue;
94  }
95  }
96 
97  size_t next_separator = path.find('/', get);
98  if (next_separator == std::string::npos) {
99  // We've reached the last component.
100  break;
101  }
102  size_t next_component_start = next_separator + 1;
103  ++next_separator;
104  size_t component_size = next_component_start - component_start;
105  if (put != component_start && component_size > 0) {
106  path.replace(put, component_size,
107  path.substr(component_start, component_size));
108  }
109  put += component_size;
110  get = next_component_start;
111  component_start = next_component_start;
112  }
113 
114  size_t last_component_size = path.size() - component_start;
115  if (last_component_size == 1 && path[component_start] == '.') {
116  // The last component is ".", which we can elide.
117  } else if (last_component_size == 2 && path[component_start] == '.' &&
118  path[component_start + 1] == '.') {
119  // The last component is "..", which means we need to remove the previous
120  // component.
121  if (put == traversal_root) {
122  path[put++] = '.';
123  path[put++] = '.';
124  path[put++] = '/';
125  traversal_root = put;
126  } else {
127  put = ResolveParentDirectoryTraversal(path, put);
128  }
129  } else {
130  // Otherwise, we need to copy over the last component.
131  if (put != component_start && last_component_size > 0) {
132  path.replace(put, last_component_size,
133  path.substr(component_start, last_component_size));
134  }
135  put += last_component_size;
136  }
137 
138  if (put >= 2 && path[put - 1] == '/')
139  --put; // Trim trailing /
140  else if (put == 0)
141  return "."; // Use . for otherwise empty paths to treat them as relative.
142 
143  path.resize(put);
144  return path;
145 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32

◆ TEST() [1/12]

filesystem::TEST ( ScopedTempDir  ,
Creation   
)

Definition at line 12 of file scoped_temp_dir_unittest.cc.

References fml::IsDirectory().

12  {
13  ScopedTempDir dir;
14 
15  EXPECT_TRUE(IsDirectory(dir.path()));
16 }
bool IsDirectory(const fml::UniqueFD &directory)
Definition: file_posix.cc:124

◆ TEST() [2/12]

filesystem::TEST ( Directory  ,
CreateDirectory   
)

Definition at line 12 of file directory_unittest.cc.

References fml::CreateDirectory(), and fml::IsDirectory().

12  {
13  std::string cwd = GetCurrentDirectory();
14 
15  ScopedTempDir dir;
16  EXPECT_TRUE(IsDirectory(dir.path()));
17  EXPECT_EQ(0, chdir(dir.path().c_str()));
18 
19  EXPECT_TRUE(CreateDirectory("foo/bar"));
20  EXPECT_TRUE(IsDirectory("foo"));
21  EXPECT_TRUE(IsDirectory("foo/bar"));
22  EXPECT_FALSE(IsDirectory("foo/bar/baz"));
23 
24  EXPECT_TRUE(CreateDirectory("foo/bar/baz"));
25  EXPECT_TRUE(IsDirectory("foo/bar/baz"));
26 
27  EXPECT_TRUE(CreateDirectory("qux"));
28  EXPECT_TRUE(IsDirectory("qux"));
29 
30  EXPECT_EQ(0, chdir(cwd.c_str()));
31 
32  std::string abs_path = dir.path() + "/another/one";
33  EXPECT_TRUE(CreateDirectory(abs_path));
34  EXPECT_TRUE(IsDirectory(abs_path));
35 }
bool IsDirectory(const fml::UniqueFD &directory)
Definition: file_posix.cc:124
static fml::UniqueFD CreateDirectory(const fml::UniqueFD &base_directory, const std::vector< std::string > &components, FilePermission permission, size_t index)
Definition: file.cc:12

◆ TEST() [3/12]

filesystem::TEST ( FileDescriptor  ,
WriteAndRead   
)

Definition at line 17 of file file_descriptor_unittest.cc.

References flutter::path, and fml::size().

17  {
18  files::ScopedTempDir temp_dir;
19  std::string path;
20  ASSERT_TRUE(temp_dir.NewTempFile(&path));
21 
22  fxl::UniqueFD fd(open(path.c_str(), O_RDWR));
23  ASSERT_TRUE(fd.is_valid());
24 
25  std::string string = "one, two, three";
26  EXPECT_TRUE(WriteFileDescriptor(fd.get(), string.data(), string.size()));
27  EXPECT_EQ(0, lseek(fd.get(), 0, SEEK_SET));
28 
29  std::vector<char> buffer;
30  buffer.resize(1024);
31  ssize_t read = ReadFileDescriptor(fd.get(), buffer.data(), 1024);
32  EXPECT_EQ(static_cast<ssize_t>(string.size()), read);
33  EXPECT_EQ(string, buffer.data());
34 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
constexpr std::size_t size(T(&array)[N])
Definition: size.h:13
UniqueObject< int, internal::os_unix::UniqueFDTraits > UniqueFD
Definition: unique_fd.h:61

◆ TEST() [4/12]

filesystem::TEST ( ScopedTempDir  ,
Deletion   
)

Definition at line 18 of file scoped_temp_dir_unittest.cc.

References fml::IsDirectory(), and flutter::path.

18  {
19  std::string path;
20  {
21  ScopedTempDir dir;
22  path = dir.path();
23  }
24 
25  EXPECT_FALSE(IsDirectory(path));
26 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
bool IsDirectory(const fml::UniqueFD &directory)
Definition: file_posix.cc:124

◆ TEST() [5/12]

filesystem::TEST ( Path  ,
SimplifyPath   
)

Definition at line 20 of file path_unittest.cc.

References ExpectPlatformPath(), and SimplifyPath().

20  {
23  ExpectPlatformPath("..", SimplifyPath(".."));
24  ExpectPlatformPath("...", SimplifyPath("..."));
25 
27  ExpectPlatformPath("/", SimplifyPath("/."));
28  ExpectPlatformPath("/", SimplifyPath("/.."));
29  ExpectPlatformPath("/...", SimplifyPath("/..."));
30 
31  ExpectPlatformPath("foo", SimplifyPath("foo"));
32  ExpectPlatformPath("foo", SimplifyPath("foo/"));
33  ExpectPlatformPath("foo", SimplifyPath("foo/."));
34  ExpectPlatformPath("foo", SimplifyPath("foo/./"));
35  ExpectPlatformPath(".", SimplifyPath("foo/.."));
36  ExpectPlatformPath(".", SimplifyPath("foo/../"));
37  ExpectPlatformPath("foo/...", SimplifyPath("foo/..."));
38  ExpectPlatformPath("foo/...", SimplifyPath("foo/.../"));
39  ExpectPlatformPath("foo/.b", SimplifyPath("foo/.b"));
40  ExpectPlatformPath("foo/.b", SimplifyPath("foo/.b/"));
41 
42  ExpectPlatformPath("/foo", SimplifyPath("/foo"));
43  ExpectPlatformPath("/foo", SimplifyPath("/foo/"));
44  ExpectPlatformPath("/foo", SimplifyPath("/foo/."));
45  ExpectPlatformPath("/foo", SimplifyPath("/foo/./"));
46  ExpectPlatformPath("/", SimplifyPath("/foo/.."));
47  ExpectPlatformPath("/", SimplifyPath("/foo/../"));
48  ExpectPlatformPath("/foo/...", SimplifyPath("/foo/..."));
49  ExpectPlatformPath("/foo/...", SimplifyPath("/foo/.../"));
50  ExpectPlatformPath("/foo/.b", SimplifyPath("/foo/.b"));
51  ExpectPlatformPath("/foo/.b", SimplifyPath("/foo/.b/"));
52 
53  ExpectPlatformPath("foo/bar", SimplifyPath("foo/bar"));
54  ExpectPlatformPath("foo/bar", SimplifyPath("foo/bar/"));
55  ExpectPlatformPath("foo/bar", SimplifyPath("foo/./bar"));
56  ExpectPlatformPath("foo/bar", SimplifyPath("foo/./bar/"));
57  ExpectPlatformPath("bar", SimplifyPath("foo/../bar"));
58  ExpectPlatformPath("bar", SimplifyPath("foo/baz/../../bar"));
59  ExpectPlatformPath("bar", SimplifyPath("foo/../bar/"));
60  ExpectPlatformPath("foo/.../bar", SimplifyPath("foo/.../bar"));
61  ExpectPlatformPath("foo/.../bar", SimplifyPath("foo/.../bar/"));
62  ExpectPlatformPath("foo/.b/bar", SimplifyPath("foo/.b/bar"));
63  ExpectPlatformPath("foo/.b/bar", SimplifyPath("foo/.b/bar/"));
64 
65  ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/bar"));
66  ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/bar/"));
67  ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/./bar"));
68  ExpectPlatformPath("/foo/bar", SimplifyPath("/foo/./bar/"));
69  ExpectPlatformPath("/bar", SimplifyPath("/foo/../bar"));
70  ExpectPlatformPath("/bar", SimplifyPath("/foo/../bar/"));
71  ExpectPlatformPath("/foo/.../bar", SimplifyPath("/foo/.../bar"));
72  ExpectPlatformPath("/foo/.../bar", SimplifyPath("/foo/.../bar/"));
73  ExpectPlatformPath("/foo/.b/bar", SimplifyPath("/foo/.b/bar"));
74  ExpectPlatformPath("/foo/.b/bar", SimplifyPath("/foo/.b/bar/"));
75 
76  ExpectPlatformPath("../foo", SimplifyPath("../foo"));
77  ExpectPlatformPath("../../bar", SimplifyPath("../foo/../../bar"));
78  ExpectPlatformPath("/bar", SimplifyPath("/foo/../../bar"));
79 
80  // Already clean
82  ExpectPlatformPath("abc", SimplifyPath("abc"));
83  ExpectPlatformPath("abc/def", SimplifyPath("abc/def"));
84  ExpectPlatformPath("a/b/c", SimplifyPath("a/b/c"));
86  ExpectPlatformPath("..", SimplifyPath(".."));
87  ExpectPlatformPath("../..", SimplifyPath("../.."));
88  ExpectPlatformPath("../../abc", SimplifyPath("../../abc"));
89  ExpectPlatformPath("/abc", SimplifyPath("/abc"));
91 
92  // Remove trailing slash
93  ExpectPlatformPath("abc", SimplifyPath("abc/"));
94  ExpectPlatformPath("abc/def", SimplifyPath("abc/def/"));
95  ExpectPlatformPath("a/b/c", SimplifyPath("a/b/c/"));
96  ExpectPlatformPath(".", SimplifyPath("./"));
97  ExpectPlatformPath("..", SimplifyPath("../"));
98  ExpectPlatformPath("../..", SimplifyPath("../../"));
99  ExpectPlatformPath("/abc", SimplifyPath("/abc/"));
100 
101  // Remove doubled slash
102  ExpectPlatformPath("abc/def/ghi", SimplifyPath("abc//def//ghi"));
103  ExpectPlatformPath("/abc", SimplifyPath("//abc"));
104  ExpectPlatformPath("/abc", SimplifyPath("///abc"));
105  ExpectPlatformPath("/abc", SimplifyPath("//abc//"));
106  ExpectPlatformPath("abc", SimplifyPath("abc//"));
107 
108  // Remove . elements
109  ExpectPlatformPath("abc/def", SimplifyPath("abc/./def"));
110  ExpectPlatformPath("/abc/def", SimplifyPath("/./abc/def"));
111  ExpectPlatformPath("abc", SimplifyPath("abc/."));
112 
113  // Remove .. elements
114  ExpectPlatformPath("abc/def/jkl", SimplifyPath("abc/def/ghi/../jkl"));
115  ExpectPlatformPath("abc/jkl", SimplifyPath("abc/def/../ghi/../jkl"));
116  ExpectPlatformPath("abc", SimplifyPath("abc/def/.."));
117  ExpectPlatformPath(".", SimplifyPath("abc/def/../.."));
118  ExpectPlatformPath("/", SimplifyPath("/abc/def/../.."));
119  ExpectPlatformPath("..", SimplifyPath("abc/def/../../.."));
120  ExpectPlatformPath("/", SimplifyPath("/abc/def/../../.."));
121  ExpectPlatformPath("../../mno",
122  SimplifyPath("abc/def/../../../ghi/jkl/../../../mno"));
123  ExpectPlatformPath("/mno", SimplifyPath("/../mno"));
124 
125  // Combinations
126  ExpectPlatformPath("def", SimplifyPath("abc/./../def"));
127  ExpectPlatformPath("def", SimplifyPath("abc//./../def"));
128  ExpectPlatformPath("../../def", SimplifyPath("abc/../../././../def"));
129 
130 #if defined(OS_WIN)
131  ExpectPlatformPath("a\\c", SimplifyPath("a\\b\\..\\c"));
132  ExpectPlatformPath("X:\\a\\c", SimplifyPath("X:/a/b/../c"));
133  ExpectPlatformPath("X:\\a\\b\\c", SimplifyPath("X:/a/b/./c"));
134  ExpectPlatformPath("X:\\c", SimplifyPath("X:/../../c"));
135 #endif
136 }
void ExpectPlatformPath(std::string expected, std::string actual)
std::string SimplifyPath(std::string path)
Definition: path_posix.cc:48

◆ TEST() [6/12]

filesystem::TEST ( ScopedTempDir  ,
NewTempFile   
)

Definition at line 28 of file scoped_temp_dir_unittest.cc.

References flutter::path.

28  {
29  ScopedTempDir dir;
30  std::string path;
31  EXPECT_TRUE(dir.NewTempFile(&path));
32  EXPECT_FALSE(path.empty());
33 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32

◆ TEST() [7/12]

filesystem::TEST ( ScopedTempDir  ,
CustomParent   
)

Definition at line 35 of file scoped_temp_dir_unittest.cc.

References GetBaseName(), GetDirectoryName(), fml::IsDirectory(), and flutter::path.

35  {
36  ScopedTempDir root_dir;
37  std::string parent = root_dir.path() + "/a/b/c";
38  std::string path;
39  {
40  ScopedTempDir dir(parent);
41  path = dir.path();
42  EXPECT_TRUE(IsDirectory(path));
43  EXPECT_EQ(path.substr(0, parent.size()), parent);
44  EXPECT_NE("temp_dir_XXXXXX", GetBaseName(path));
45 
46  // Regression test - don't create temp_dir_XXXXXX dir next to the temp one.
47  EXPECT_FALSE(
48  files::IsDirectory(GetDirectoryName(path) + "/temp_dir_XXXXXX"));
49  }
50 
51  // Verify that the tmp directory itself was deleted, but not the parent.
52  EXPECT_FALSE(IsDirectory(path));
53  EXPECT_TRUE(IsDirectory(parent));
54 }
DEF_SWITCHES_START snapshot asset path
Definition: switches.h:32
std::string GetDirectoryName(const std::string &path)
Definition: paths_posix.cc:42
std::string GetBaseName(const std::string &path)
Definition: path_posix.cc:169
bool IsDirectory(const fml::UniqueFD &directory)
Definition: file_posix.cc:124

◆ TEST() [8/12]

filesystem::TEST ( Path  ,
AbsolutePath   
)

Definition at line 138 of file path_unittest.cc.

References AbsolutePath().

138  {
139 #if defined(OS_WIN)
140  // We cut out the drive letter as it can be different on every system.
141  EXPECT_EQ(":\\foo\\bar", AbsolutePath("\\foo\\bar").substr(1));
142  EXPECT_EQ(":\\foo\\bar", AbsolutePath("/foo/bar").substr(1));
143  EXPECT_EQ(":\\foo\\bar\\", AbsolutePath("\\foo\\bar\\").substr(1));
144  EXPECT_EQ(":\\foo\\bar\\", AbsolutePath("/foo/bar/").substr(1));
145  EXPECT_EQ("C:\\foo\\bar\\", AbsolutePath("C:\\foo\\bar\\"));
146  EXPECT_EQ(GetCurrentDirectory() + "\\foo", AbsolutePath("foo"));
147 #else
148  EXPECT_EQ("/foo/bar", AbsolutePath("/foo/bar"));
149  EXPECT_EQ("/foo/bar/", AbsolutePath("/foo/bar/"));
150  EXPECT_EQ(GetCurrentDirectory() + "/foo", AbsolutePath("foo"));
151 #endif
152  EXPECT_EQ(GetCurrentDirectory(), AbsolutePath(""));
153 }
std::string AbsolutePath(const std::string &path)
Definition: paths_posix.cc:29

◆ TEST() [9/12]

filesystem::TEST ( Path  ,
GetDirectoryName   
)

Definition at line 155 of file path_unittest.cc.

References GetDirectoryName().

155  {
156  EXPECT_EQ("foo", GetDirectoryName("foo/"));
157  EXPECT_EQ("foo/bar", GetDirectoryName("foo/bar/"));
158  EXPECT_EQ("foo", GetDirectoryName("foo/bar"));
159  EXPECT_EQ("foo/bar", GetDirectoryName("foo/bar/.."));
160  EXPECT_EQ("foo/bar/..", GetDirectoryName("foo/bar/../.."));
161  EXPECT_EQ("", GetDirectoryName("foo"));
162  EXPECT_EQ("/", GetDirectoryName("/"));
163  EXPECT_EQ("", GetDirectoryName("a"));
164  EXPECT_EQ("/", GetDirectoryName("/a"));
165  EXPECT_EQ("/a", GetDirectoryName("/a/"));
166  EXPECT_EQ("a", GetDirectoryName("a/"));
167 #if defined(OS_WIN)
168  EXPECT_EQ("C:\\", GetDirectoryName("C:\\"));
169  EXPECT_EQ("C:\\foo", GetDirectoryName("C:\\foo\\"));
170  EXPECT_EQ("C:\\foo", GetDirectoryName("C:\\foo\\bar"));
171  EXPECT_EQ("foo\\bar", GetDirectoryName("foo\\bar\\"));
172  EXPECT_EQ("foo", GetDirectoryName("foo\\bar"));
173  EXPECT_EQ("\\", GetDirectoryName("\\"));
174  EXPECT_EQ("\\", GetDirectoryName("\\a"));
175 #endif
176 }
std::string GetDirectoryName(const std::string &path)
Definition: paths_posix.cc:42

◆ TEST() [10/12]

filesystem::TEST ( Path  ,
GetBaseName   
)

Definition at line 178 of file path_unittest.cc.

References GetBaseName().

178  {
179  EXPECT_EQ("", GetBaseName("foo/"));
180  EXPECT_EQ("", GetBaseName("foo/bar/"));
181  EXPECT_EQ("bar", GetBaseName("foo/bar"));
182  EXPECT_EQ("..", GetBaseName("foo/bar/.."));
183  EXPECT_EQ("..", GetBaseName("foo/bar/../.."));
184  EXPECT_EQ("foo", GetBaseName("foo"));
185  EXPECT_EQ("", GetBaseName("/"));
186  EXPECT_EQ("a", GetBaseName("a"));
187  EXPECT_EQ("a", GetBaseName("/a"));
188  EXPECT_EQ("", GetBaseName("/a/"));
189  EXPECT_EQ("", GetBaseName("a/"));
190 #if defined(OS_WIN)
191  EXPECT_EQ("", GetBaseName("C:\\"));
192  EXPECT_EQ("", GetBaseName("C:\\foo\\"));
193  EXPECT_EQ("bar", GetBaseName("C:\\foo\\bar"));
194  EXPECT_EQ("", GetBaseName("foo\\bar\\"));
195  EXPECT_EQ("bar", GetBaseName("foo\\bar"));
196  EXPECT_EQ("", GetBaseName("\\"));
197  EXPECT_EQ("a", GetBaseName("\\a"));
198 #endif
199 }
std::string GetBaseName(const std::string &path)
Definition: path_posix.cc:169

◆ TEST() [11/12]

filesystem::TEST ( Path  ,
DeletePath   
)

Definition at line 201 of file path_unittest.cc.

References fml::CreateDirectory(), and fml::IsDirectory().

201  {
202  ScopedTempDir dir;
203 
204  std::string sub_dir = dir.path() + "/dir";
205  CreateDirectory(sub_dir);
206  EXPECT_TRUE(IsDirectory(sub_dir));
207  EXPECT_TRUE(DeletePath(sub_dir, false));
208  EXPECT_FALSE(IsDirectory(sub_dir));
209 }
bool IsDirectory(const fml::UniqueFD &directory)
Definition: file_posix.cc:124
static fml::UniqueFD CreateDirectory(const fml::UniqueFD &base_directory, const std::vector< std::string > &components, FilePermission permission, size_t index)
Definition: file.cc:12

◆ TEST() [12/12]

filesystem::TEST ( Path  ,
DeletePathRecursively   
)

Definition at line 211 of file path_unittest.cc.

References fml::CreateDirectory(), and fml::IsDirectory().

211  {
212  ScopedTempDir dir;
213 
214  std::string sub_dir = dir.path() + "/dir";
215  CreateDirectory(sub_dir);
216  EXPECT_TRUE(IsDirectory(sub_dir));
217 
218  std::string sub_sub_dir1 = sub_dir + "/dir1";
219  CreateDirectory(sub_sub_dir1);
220  EXPECT_TRUE(IsDirectory(sub_sub_dir1));
221  std::string sub_sub_dir2 = sub_dir + "/dir2";
222  CreateDirectory(sub_sub_dir2);
223  EXPECT_TRUE(IsDirectory(sub_sub_dir2));
224 
225  EXPECT_FALSE(DeletePath(sub_dir, false));
226  EXPECT_TRUE(IsDirectory(sub_dir));
227  EXPECT_TRUE(IsDirectory(sub_sub_dir1));
228 
229  EXPECT_TRUE(DeletePath(sub_dir, true));
230  EXPECT_FALSE(IsDirectory(sub_dir));
231  EXPECT_FALSE(IsDirectory(sub_sub_dir1));
232 }
bool IsDirectory(const fml::UniqueFD &directory)
Definition: file_posix.cc:124
static fml::UniqueFD CreateDirectory(const fml::UniqueFD &base_directory, const std::vector< std::string > &components, FilePermission permission, size_t index)
Definition: file.cc:12