blob: 7d83f97fae4c1ca06914745509bc047178ae93fa [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
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/fileutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012
Oleh Prypin96a0f612018-10-05 13:13:38 +000013#include "rtc_base/arraysize.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020014#include "rtc_base/checks.h"
15#include "rtc_base/pathutils.h"
Oleh Prypin96a0f612018-10-05 13:13:38 +000016#include "rtc_base/stringutils.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000017
18#if defined(WEBRTC_WIN)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020019#include "rtc_base/win32filesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000020#else
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/unixfilesystem.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000022#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
Yves Gerey665174f2018-06-19 15:03:05 +020038// Constructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000039DirectoryIterator::DirectoryIterator()
40#ifdef WEBRTC_WIN
41 : handle_(INVALID_HANDLE_VALUE) {
42#else
deadbeef37f5ecf2017-02-27 14:06:41 -080043 : dir_(nullptr),
44 dirent_(nullptr){
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045#endif
46}
47
Yves Gerey665174f2018-06-19 15:03:05 +020048// Destructor
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000049DirectoryIterator::~DirectoryIterator() {
50#if defined(WEBRTC_WIN)
51 if (handle_ != INVALID_HANDLE_VALUE)
52 ::FindClose(handle_);
53#else
54 if (dir_)
55 closedir(dir_);
56#endif
57}
58
Yves Gerey665174f2018-06-19 15:03:05 +020059// Starts traversing a directory.
60// dir is the directory to traverse
61// returns true if the directory exists and is valid
62bool DirectoryIterator::Iterate(const Pathname& dir) {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000063 directory_ = dir.pathname();
64#if defined(WEBRTC_WIN)
65 if (handle_ != INVALID_HANDLE_VALUE)
66 ::FindClose(handle_);
67 std::string d = dir.pathname() + '*';
68 handle_ = ::FindFirstFile(ToUtf16(d).c_str(), &data_);
69 if (handle_ == INVALID_HANDLE_VALUE)
70 return false;
71#else
deadbeef37f5ecf2017-02-27 14:06:41 -080072 if (dir_ != nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000073 closedir(dir_);
74 dir_ = ::opendir(directory_.c_str());
deadbeef37f5ecf2017-02-27 14:06:41 -080075 if (dir_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 return false;
77 dirent_ = readdir(dir_);
deadbeef37f5ecf2017-02-27 14:06:41 -080078 if (dirent_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000079 return false;
80
81 if (::stat(std::string(directory_ + Name()).c_str(), &stat_) != 0)
82 return false;
83#endif
84 return true;
85}
86
Yves Gerey665174f2018-06-19 15:03:05 +020087// Advances to the next file
88// returns true if there were more files in the directory.
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000089bool DirectoryIterator::Next() {
90#if defined(WEBRTC_WIN)
91 return ::FindNextFile(handle_, &data_) == TRUE;
92#else
93 dirent_ = ::readdir(dir_);
deadbeef37f5ecf2017-02-27 14:06:41 -080094 if (dirent_ == nullptr)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000095 return false;
96
97 return ::stat(std::string(directory_ + Name()).c_str(), &stat_) == 0;
98#endif
99}
100
Yves Gerey665174f2018-06-19 15:03:05 +0200101// returns true if the file currently pointed to is a directory
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102bool DirectoryIterator::IsDirectory() const {
103#if defined(WEBRTC_WIN)
104 return (data_.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) != FALSE;
105#else
106 return S_ISDIR(stat_.st_mode);
107#endif
108}
109
Yves Gerey665174f2018-06-19 15:03:05 +0200110// returns the name of the file currently pointed to
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000111std::string DirectoryIterator::Name() const {
112#if defined(WEBRTC_WIN)
113 return ToUtf8(data_.cFileName);
114#else
kwiberg22487b22016-09-13 01:17:10 -0700115 RTC_DCHECK(dirent_);
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000116 return dirent_->d_name;
117#endif
118}
119
Niels Möllerae82ffa2018-06-25 09:52:21 +0200120FilesystemInterface* Filesystem::GetFilesystem() {
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000121#if defined(WEBRTC_WIN)
Niels Möllerae82ffa2018-06-25 09:52:21 +0200122 static FilesystemInterface* const filesystem = new Win32Filesystem();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000123#else
Niels Möllerae82ffa2018-06-25 09:52:21 +0200124 static FilesystemInterface* const filesystem = new UnixFilesystem();
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000125#endif
Niels Möllerae82ffa2018-06-25 09:52:21 +0200126
127 return filesystem;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000128}
129
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000130} // namespace rtc