blob: d709115fe9f0757bf1d14716259e08e47f3da1c6 [file] [log] [blame]
henrike@webrtc.org0e118e72013-07-10 00:45:36 +00001/*
2 * libjingle
3 * Copyright 2004--2006, Google Inc.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#ifndef TALK_BASE_UNIXFILESYSTEM_H_
29#define TALK_BASE_UNIXFILESYSTEM_H_
30
31#include <sys/types.h>
32
33#include "talk/base/fileutils.h"
34
35namespace talk_base {
36
37class UnixFilesystem : public FilesystemInterface {
38 public:
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000039 UnixFilesystem();
40 virtual ~UnixFilesystem();
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000041
42#if defined(ANDROID) || defined(IOS)
fischman@webrtc.orgaa06b852014-04-29 18:37:29 +000043 // Android does not have a native code API to fetch the app data or temp
44 // folders. That needs to be passed into this class from Java. Similarly, iOS
45 // only supports an Objective-C API for fetching the folder locations, so that
46 // needs to be passed in here from Objective-C. Or at least that used to be
47 // the case; now the ctor will do the work if necessary and possible.
48 // TODO(fischman): add an Android version that uses JNI and drop the
49 // SetApp*Folder() APIs once external users stop using them.
henrike@webrtc.org0e118e72013-07-10 00:45:36 +000050 static void SetAppDataFolder(const std::string& folder);
51 static void SetAppTempFolder(const std::string& folder);
52#endif
53
54 // Opens a file. Returns an open StreamInterface if function succeeds.
55 // Otherwise, returns NULL.
56 virtual FileStream *OpenFile(const Pathname &filename,
57 const std::string &mode);
58
59 // Atomically creates an empty file accessible only to the current user if one
60 // does not already exist at the given path, otherwise fails.
61 virtual bool CreatePrivateFile(const Pathname &filename);
62
63 // This will attempt to delete the file located at filename.
64 // It will fail with VERIY if you pass it a non-existant file, or a directory.
65 virtual bool DeleteFile(const Pathname &filename);
66
67 // This will attempt to delete the folder located at 'folder'
68 // It ASSERTs and returns false if you pass it a non-existant folder or a
69 // plain file.
70 virtual bool DeleteEmptyFolder(const Pathname &folder);
71
72 // Creates a directory. This will call itself recursively to create /foo/bar
73 // even if /foo does not exist. All created directories are created with the
74 // given mode.
75 // Returns TRUE if function succeeds
76 virtual bool CreateFolder(const Pathname &pathname, mode_t mode);
77
78 // As above, with mode = 0755.
79 virtual bool CreateFolder(const Pathname &pathname);
80
81 // This moves a file from old_path to new_path, where "file" can be a plain
82 // file or directory, which will be moved recursively.
83 // Returns true if function succeeds.
84 virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path);
85 virtual bool MoveFolder(const Pathname &old_path, const Pathname &new_path);
86
87 // This copies a file from old_path to _new_path where "file" can be a plain
88 // file or directory, which will be copied recursively.
89 // Returns true if function succeeds
90 virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path);
91
92 // Returns true if a pathname is a directory
93 virtual bool IsFolder(const Pathname& pathname);
94
95 // Returns true if pathname represents a temporary location on the system.
96 virtual bool IsTemporaryPath(const Pathname& pathname);
97
98 // Returns true of pathname represents an existing file
99 virtual bool IsFile(const Pathname& pathname);
100
101 // Returns true if pathname refers to no filesystem object, every parent
102 // directory either exists, or is also absent.
103 virtual bool IsAbsent(const Pathname& pathname);
104
105 virtual std::string TempFilename(const Pathname &dir,
106 const std::string &prefix);
107
108 // A folder appropriate for storing temporary files (Contents are
109 // automatically deleted when the program exists)
110 virtual bool GetTemporaryFolder(Pathname &path, bool create,
111 const std::string *append);
112
113 virtual bool GetFileSize(const Pathname& path, size_t* size);
114 virtual bool GetFileTime(const Pathname& path, FileTimeType which,
115 time_t* time);
116
117 // Returns the path to the running application.
118 virtual bool GetAppPathname(Pathname* path);
119
120 virtual bool GetAppDataFolder(Pathname* path, bool per_user);
121
122 // Get a temporary folder that is unique to the current user and application.
123 virtual bool GetAppTempFolder(Pathname* path);
124
125 virtual bool GetDiskFreeSpace(const Pathname& path, int64 *freebytes);
126
127 // Returns the absolute path of the current directory.
128 virtual Pathname GetCurrentDirectory();
129
130 private:
131#if defined(ANDROID) || defined(IOS)
132 static char* provided_app_data_folder_;
133 static char* provided_app_temp_folder_;
134#else
135 static char* app_temp_path_;
136#endif
137
138 static char* CopyString(const std::string& str);
139};
140
141} // namespace talk_base
142
143#endif // TALK_BASE_UNIXFILESYSTEM_H_