blob: 6f9095910193cc8f07e7cfb65e108fe5caed58bb [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
11#ifndef WEBRTC_BASE_UNIXFILESYSTEM_H_
12#define WEBRTC_BASE_UNIXFILESYSTEM_H_
13
14#include <sys/types.h>
15
16#include "webrtc/base/fileutils.h"
17
18namespace rtc {
19
20class UnixFilesystem : public FilesystemInterface {
21 public:
22 UnixFilesystem();
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000023 ~UnixFilesystem() override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024
erikchen2ca8d5c2016-10-05 16:04:33 -070025#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000026 // Android does not have a native code API to fetch the app data or temp
27 // folders. That needs to be passed into this class from Java. Similarly, iOS
28 // only supports an Objective-C API for fetching the folder locations, so that
29 // needs to be passed in here from Objective-C. Or at least that used to be
30 // the case; now the ctor will do the work if necessary and possible.
31 // TODO(fischman): add an Android version that uses JNI and drop the
32 // SetApp*Folder() APIs once external users stop using them.
33 static void SetAppDataFolder(const std::string& folder);
34 static void SetAppTempFolder(const std::string& folder);
35#endif
36
37 // Opens a file. Returns an open StreamInterface if function succeeds.
deadbeef37f5ecf2017-02-27 14:06:41 -080038 // Otherwise, returns null.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000039 FileStream* OpenFile(const Pathname& filename,
40 const std::string& mode) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000041
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000042 // This will attempt to delete the file located at filename.
43 // It will fail with VERIY if you pass it a non-existant file, or a directory.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000044 bool DeleteFile(const Pathname& filename) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045
46 // This will attempt to delete the folder located at 'folder'
nisse0ebdf272017-01-23 07:43:05 -080047 // It DCHECKs and returns false if you pass it a non-existant folder or a
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000048 // plain file.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000049 bool DeleteEmptyFolder(const Pathname& folder) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000050
51 // Creates a directory. This will call itself recursively to create /foo/bar
52 // even if /foo does not exist. All created directories are created with the
53 // given mode.
54 // Returns TRUE if function succeeds
55 virtual bool CreateFolder(const Pathname &pathname, mode_t mode);
56
57 // As above, with mode = 0755.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000058 bool CreateFolder(const Pathname& pathname) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000059
60 // This moves a file from old_path to new_path, where "file" can be a plain
61 // file or directory, which will be moved recursively.
62 // Returns true if function succeeds.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000063 bool MoveFile(const Pathname& old_path, const Pathname& new_path) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000064
65 // This copies a file from old_path to _new_path where "file" can be a plain
66 // file or directory, which will be copied recursively.
67 // Returns true if function succeeds
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000068 bool CopyFile(const Pathname& old_path, const Pathname& new_path) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069
70 // Returns true if a pathname is a directory
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000071 bool IsFolder(const Pathname& pathname) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000072
73 // Returns true if pathname represents a temporary location on the system.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000074 bool IsTemporaryPath(const Pathname& pathname) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075
76 // Returns true of pathname represents an existing file
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000077 bool IsFile(const Pathname& pathname) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000078
79 // Returns true if pathname refers to no filesystem object, every parent
80 // directory either exists, or is also absent.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000081 bool IsAbsent(const Pathname& pathname) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000083 std::string TempFilename(const Pathname& dir,
84 const std::string& prefix) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000085
86 // A folder appropriate for storing temporary files (Contents are
87 // automatically deleted when the program exists)
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000088 bool GetTemporaryFolder(Pathname& path,
89 bool create,
90 const std::string* append) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000091
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000092 bool GetFileSize(const Pathname& path, size_t* size) override;
93 bool GetFileTime(const Pathname& path,
94 FileTimeType which,
95 time_t* time) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000096
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000097 // Get a temporary folder that is unique to the current user and application.
kwiberg@webrtc.org67186fe2015-03-09 22:21:53 +000098 bool GetAppTempFolder(Pathname* path) override;
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000099
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000100 private:
erikchen2ca8d5c2016-10-05 16:04:33 -0700101#if defined(WEBRTC_ANDROID) || defined(WEBRTC_MAC)
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000102 static char* provided_app_data_folder_;
103 static char* provided_app_temp_folder_;
104#else
105 static char* app_temp_path_;
106#endif
107
108 static char* CopyString(const std::string& str);
109};
110
111} // namespace rtc
112
113#endif // WEBRTC_BASE_UNIXFILESYSTEM_H_