1 /*
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 "fileutils.h"
32 
33 namespace talk_base {
34 
35 class UnixFilesystem : public FilesystemInterface {
36  public:
37 
38 #if defined(ANDROID) || defined(IOS)
39 // Android does not have a native code API to fetch the app data or temp
40 // folders. That needs to be passed into this class from Java. Similarly, iOS
41 // only supports an Objective-C API for fetching the folder locations, so that
42 // needs to be passed in here from Objective-C.
43 
44   static void SetAppDataFolder(const std::string& folder);
45   static void SetAppTempFolder(const std::string& folder);
46 #endif
47 
48   // Opens a file. Returns an open StreamInterface if function succeeds. Otherwise,
49   // returns NULL.
50   virtual FileStream *OpenFile(const Pathname &filename,
51                                const std::string &mode);
52 
53   // Atomically creates an empty file accessible only to the current user if one
54   // does not already exist at the given path, otherwise fails.
55   virtual bool CreatePrivateFile(const Pathname &filename);
56 
57   // This will attempt to delete the file located at filename.
58   // It will fail with VERIY if you pass it a non-existant file, or a directory.
59   virtual bool DeleteFile(const Pathname &filename);
60 
61   // This will attempt to delete the folder located at 'folder'
62   // It ASSERTs and returns false if you pass it a non-existant folder or a plain file.
63   virtual bool DeleteEmptyFolder(const Pathname &folder);
64 
65   // Creates a directory. This will call itself recursively to create /foo/bar even if
66   // /foo does not exist.
67   // Returns TRUE if function succeeds
68    virtual bool CreateFolder(const Pathname &pathname);
69 
70   // This moves a file from old_path to new_path, where "file" can be a plain file
71   // or directory, which will be moved recursively.
72   // Returns true if function succeeds.
73   virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path);
74   virtual bool MoveFolder(const Pathname &old_path, const Pathname &new_path);
75 
76   // This copies a file from old_path to _new_path where "file" can be a plain file
77   // or directory, which will be copied recursively.
78   // Returns true if function succeeds
79   virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path);
80 
81   // Returns true if a pathname is a directory
82   virtual bool IsFolder(const Pathname& pathname);
83 
84   // Returns true if pathname represents a temporary location on the system.
85   virtual bool IsTemporaryPath(const Pathname& pathname);
86 
87   // Returns true of pathname represents an existing file
88   virtual bool IsFile(const Pathname& pathname);
89 
90   // Returns true if pathname refers to no filesystem object, every parent
91   // directory either exists, or is also absent.
92   virtual bool IsAbsent(const Pathname& pathname);
93 
94   virtual std::string TempFilename(const Pathname &dir, const std::string &prefix);
95 
96   // A folder appropriate for storing temporary files (Contents are
97   // automatically deleted when the program exists)
98   virtual bool GetTemporaryFolder(Pathname &path, bool create,
99                                  const std::string *append);
100 
101   virtual bool GetFileSize(const Pathname& path, size_t* size);
102   virtual bool GetFileTime(const Pathname& path, FileTimeType which,
103                            time_t* time);
104 
105   // Returns the path to the running application.
106   virtual bool GetAppPathname(Pathname* path);
107 
108   virtual bool GetAppDataFolder(Pathname* path, bool per_user);
109 
110   // Get a temporary folder that is unique to the current user and application.
111   virtual bool GetAppTempFolder(Pathname* path);
112 
113   virtual bool GetDiskFreeSpace(const Pathname& path, int64 *freebytes);
114 
115   // Returns the absolute path of the current directory.
116   virtual Pathname GetCurrentDirectory();
117 
118  private:
119 #if defined(ANDROID) || defined(IOS)
120   static char* provided_app_data_folder_;
121   static char* provided_app_temp_folder_;
122 #else
123   static char* app_temp_path_;
124 #endif
125 
126   static char* CopyString(const std::string& str);
127 };
128 
129 }  // namespace talk_base
130 
131 #endif  // _UNIXFILESYSTEM_H__
132