blob: a88399505469753dd9734a36d09df1d5898cacf7 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
2 * Copyright 2004 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
kwiberg22487b22016-09-13 01:17:10 -070011#include "webrtc/base/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
tfarina5237aaf2015-11-10 23:44:30 -080013#include "webrtc/base/arraysize.h"
kwiberg22487b22016-09-13 01:17:10 -070014#include "webrtc/base/checks.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015#include "webrtc/base/pathutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000016#include "webrtc/base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
18#if defined(WEBRTC_WIN)
19#include "webrtc/base/win32filesystem.h"
20#else
21#include "webrtc/base/unixfilesystem.h"
22#endif
23
24#if !defined(WEBRTC_WIN)
25#define MAX_PATH 260
26#endif
27
28namespace rtc {
29
30//////////////////////////
31// Directory Iterator //
32//////////////////////////
33
34// A DirectoryIterator is created with a given directory. It originally points
35// to the first file in the directory, and can be advanecd with Next(). This
36// allows you to get information about each file.
37
38 // Constructor
39DirectoryIterator::DirectoryIterator()
40#ifdef WEBRTC_WIN
41 : handle_(INVALID_HANDLE_VALUE) {
42#else
43 : dir_(NULL), dirent_(NULL) {
44#endif
45}
46
47 // Destructor
48DirectoryIterator::~DirectoryIterator() {
49#if defined(WEBRTC_WIN)
50 if (handle_ != INVALID_HANDLE_VALUE)
51 ::FindClose(handle_);
52#else
53 if (dir_)
54 closedir(dir_);
55#endif
56}
57
58 // Starts traversing a directory.
59 // dir is the directory to traverse
60 // returns true if the directory exists and is valid
61bool DirectoryIterator::Iterate(const Pathname &dir) {
62 directory_ = dir.pathname();
63#if defined(WEBRTC_WIN)
64 if (handle_ != INVALID_HANDLE_VALUE)
65 ::FindClose(handle_);
66 std::string d = dir.pathname() + '*';
67 handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
68 if (handle_ == INVALID_HANDLE_VALUE)
69 return false;
70#else
71 if (dir_ != NULL)
72 closedir(dir_);
73 dir_ = ::opendir(directory_.c_str());
74 if (dir_ == NULL)
75 return false;
76 dirent_ = readdir(dir_);
77 if (dirent_ == NULL)
78 return false;
79
80 if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
81 return false;
82#endif
83 return true;
84}
85
86 // Advances to the next file
87 // returns true if there were more files in the directory.
88bool DirectoryIterator::Next() {
89#if defined(WEBRTC_WIN)
90 return ::FindNextFile(handle_, &data_) == TRUE;
91#else
92 dirent_ = ::readdir(dir_);
93 if (dirent_ == NULL)
94 return false;
95
96 return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
97#endif
98}
99
100 // returns true if the file currently pointed to is a directory
101bool DirectoryIterator::IsDirectory() const {
102#if defined(WEBRTC_WIN)
103 return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
104#else
105 return S_ISDIR(stat_.st_mode);
106#endif
107}
108
109 // returns the name of the file currently pointed to
110std::string DirectoryIterator::Name() const {
111#if defined(WEBRTC_WIN)
112 return ToUtf8(data_.cFileName);
113#else
kwiberg22487b22016-09-13 01:17:10 -0700114 RTC_DCHECK(dirent_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000115 return dirent_->d_name;
116#endif
117}
118
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119FilesystemInterface* Filesystem::default_filesystem_ = NULL;
120
121FilesystemInterface *Filesystem::EnsureDefaultFilesystem() {
122 if (!default_filesystem_) {
123#if defined(WEBRTC_WIN)
124 default_filesystem_ = new Win32Filesystem();
125#else
126 default_filesystem_ = new UnixFilesystem();
127#endif
128 }
129 return default_filesystem_;
130}
131
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000132DirectoryIterator* FilesystemInterface::IterateDirectory() {
133 return new DirectoryIterator();
134}
135
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000136bool FilesystemInterface::DeleteFolderContents(const Pathname &folder) {
137 bool success = true;
nisse989ec092017-01-10 03:44:41 -0800138 RTC_CHECK(IsFolder(folder));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000139 DirectoryIterator *di = IterateDirectory();
140 if (!di)
141 return false;
142 if (di->Iterate(folder)) {
143 do {
144 if (di->Name() == "." || di->Name() == "..")
145 continue;
146 Pathname subdir;
147 subdir.SetFolder(folder.pathname());
148 if (di->IsDirectory()) {
149 subdir.AppendFolder(di->Name());
150 if (!DeleteFolderAndContents(subdir)) {
151 success = false;
152 }
153 } else {
154 subdir.SetFilename(di->Name());
155 if (!DeleteFile(subdir)) {
156 success = false;
157 }
158 }
159 } while (di->Next());
160 }
161 delete di;
162 return success;
163}
164
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +0000165bool FilesystemInterface::DeleteFolderAndContents(const Pathname& folder) {
166 return DeleteFolderContents(folder) && DeleteEmptyFolder(folder);
167}
168
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000169} // namespace rtc