blob: a047918901e440818aafb04d1534eaed26c1d738 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/*
* Copyright 2004 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "webrtc/base/fileutils.h"
#include "webrtc/base/arraysize.h"
#include "webrtc/base/checks.h"
#include "webrtc/base/pathutils.h"
#include "webrtc/base/stringutils.h"
#if defined(WEBRTC_WIN)
#include "webrtc/base/win32filesystem.h"
#else
#include "webrtc/base/unixfilesystem.h"
#endif
#if !defined(WEBRTC_WIN)
#define MAX_PATH 260
#endif
namespace rtc {
//////////////////////////
// Directory Iterator //
//////////////////////////
// A DirectoryIterator is created with a given directory. It originally points
// to the first file in the directory, and can be advanecd with Next(). This
// allows you to get information about each file.
// Constructor
DirectoryIterator::DirectoryIterator()
#ifdef WEBRTC_WIN
: handle_(INVALID_HANDLE_VALUE) {
#else
: dir_(nullptr),
dirent_(nullptr){
#endif
}
// Destructor
DirectoryIterator::~DirectoryIterator() {
#if defined(WEBRTC_WIN)
if (handle_ != INVALID_HANDLE_VALUE)
::FindClose(handle_);
#else
if (dir_)
closedir(dir_);
#endif
}
// Starts traversing a directory.
// dir is the directory to traverse
// returns true if the directory exists and is valid
bool DirectoryIterator::Iterate(const Pathname &dir) {
directory_ = dir.pathname();
#if defined(WEBRTC_WIN)
if (handle_ != INVALID_HANDLE_VALUE)
::FindClose(handle_);
std::string d = dir.pathname() + '*';
handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
if (handle_ == INVALID_HANDLE_VALUE)
return false;
#else
if (dir_ != nullptr)
closedir(dir_);
dir_ = ::opendir(directory_.c_str());
if (dir_ == nullptr)
return false;
dirent_ = readdir(dir_);
if (dirent_ == nullptr)
return false;
if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
return false;
#endif
return true;
}
// Advances to the next file
// returns true if there were more files in the directory.
bool DirectoryIterator::Next() {
#if defined(WEBRTC_WIN)
return ::FindNextFile(handle_, &data_) == TRUE;
#else
dirent_ = ::readdir(dir_);
if (dirent_ == nullptr)
return false;
return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
#endif
}
// returns true if the file currently pointed to is a directory
bool DirectoryIterator::IsDirectory() const {
#if defined(WEBRTC_WIN)
return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
#else
return S_ISDIR(stat_.st_mode);
#endif
}
// returns the name of the file currently pointed to
std::string DirectoryIterator::Name() const {
#if defined(WEBRTC_WIN)
return ToUtf8(data_.cFileName);
#else
RTC_DCHECK(dirent_);
return dirent_->d_name;
#endif
}
FilesystemInterface* Filesystem::default_filesystem_ = nullptr;
FilesystemInterface *Filesystem::EnsureDefaultFilesystem() {
if (!default_filesystem_) {
#if defined(WEBRTC_WIN)
default_filesystem_ = new Win32Filesystem();
#else
default_filesystem_ = new UnixFilesystem();
#endif
}
return default_filesystem_;
}
DirectoryIterator* FilesystemInterface::IterateDirectory() {
return new DirectoryIterator();
}
} // namespace rtc
|