10e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org/*
20e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * libjingle
30e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Copyright 2004--2006, Google Inc.
40e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
50e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * Redistribution and use in source and binary forms, with or without
60e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * modification, are permitted provided that the following conditions are met:
70e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
80e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
90e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer.
100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     this list of conditions and the following disclaimer in the documentation
120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     and/or other materials provided with the distribution.
130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *  3. The name of the author may not be used to endorse or promote products
140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *     derived from this software without specific prior written permission.
150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org *
160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org */
270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#ifndef TALK_BASE_UNIXFILESYSTEM_H_
290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#define TALK_BASE_UNIXFILESYSTEM_H_
300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include <sys/types.h>
320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#include "talk/base/fileutils.h"
340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgnamespace talk_base {
360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.orgclass UnixFilesystem : public FilesystemInterface {
380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org public:
39aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  UnixFilesystem();
40aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  virtual ~UnixFilesystem();
410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#if defined(ANDROID) || defined(IOS)
43aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  // Android does not have a native code API to fetch the app data or temp
44aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  // folders. That needs to be passed into this class from Java. Similarly, iOS
45aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  // only supports an Objective-C API for fetching the folder locations, so that
46aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  // needs to be passed in here from Objective-C.  Or at least that used to be
47aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  // the case; now the ctor will do the work if necessary and possible.
48aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  // TODO(fischman): add an Android version that uses JNI and drop the
49aa06b85f89c9e3d388b67c7c0b4c5e9347809a5bfischman@webrtc.org  // SetApp*Folder() APIs once external users stop using them.
500e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static void SetAppDataFolder(const std::string& folder);
510e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static void SetAppTempFolder(const std::string& folder);
520e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif
530e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
540e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Opens a file. Returns an open StreamInterface if function succeeds.
550e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Otherwise, returns NULL.
560e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual FileStream *OpenFile(const Pathname &filename,
570e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                               const std::string &mode);
580e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
590e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Atomically creates an empty file accessible only to the current user if one
600e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // does not already exist at the given path, otherwise fails.
610e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool CreatePrivateFile(const Pathname &filename);
620e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
630e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This will attempt to delete the file located at filename.
640e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // It will fail with VERIY if you pass it a non-existant file, or a directory.
650e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool DeleteFile(const Pathname &filename);
660e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
670e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This will attempt to delete the folder located at 'folder'
680e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // It ASSERTs and returns false if you pass it a non-existant folder or a
690e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // plain file.
700e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool DeleteEmptyFolder(const Pathname &folder);
710e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
720e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Creates a directory. This will call itself recursively to create /foo/bar
730e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // even if /foo does not exist. All created directories are created with the
740e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // given mode.
750e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns TRUE if function succeeds
760e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool CreateFolder(const Pathname &pathname, mode_t mode);
770e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
780e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // As above, with mode = 0755.
790e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool CreateFolder(const Pathname &pathname);
800e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
810e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This moves a file from old_path to new_path, where "file" can be a plain
820e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // file or directory, which will be moved recursively.
830e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true if function succeeds.
840e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path);
850e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool MoveFolder(const Pathname &old_path, const Pathname &new_path);
860e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
870e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // This copies a file from old_path to _new_path where "file" can be a plain
880e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // file or directory, which will be copied recursively.
890e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true if function succeeds
900e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path);
910e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
920e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true if a pathname is a directory
930e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsFolder(const Pathname& pathname);
940e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
950e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true if pathname represents a temporary location on the system.
960e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsTemporaryPath(const Pathname& pathname);
970e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
980e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true of pathname represents an existing file
990e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsFile(const Pathname& pathname);
1000e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1010e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns true if pathname refers to no filesystem object, every parent
1020e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // directory either exists, or is also absent.
1030e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool IsAbsent(const Pathname& pathname);
1040e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1050e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual std::string TempFilename(const Pathname &dir,
1060e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                   const std::string &prefix);
1070e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1080e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // A folder appropriate for storing temporary files (Contents are
1090e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // automatically deleted when the program exists)
1100e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetTemporaryFolder(Pathname &path, bool create,
1110e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                                 const std::string *append);
1120e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1130e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetFileSize(const Pathname& path, size_t* size);
1140e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetFileTime(const Pathname& path, FileTimeType which,
1150e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org                           time_t* time);
1160e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1170e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the path to the running application.
1180e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetAppPathname(Pathname* path);
1190e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1200e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetAppDataFolder(Pathname* path, bool per_user);
1210e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1220e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Get a temporary folder that is unique to the current user and application.
1230e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetAppTempFolder(Pathname* path);
1240e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1250e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual bool GetDiskFreeSpace(const Pathname& path, int64 *freebytes);
1260e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1270e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  // Returns the absolute path of the current directory.
1280e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  virtual Pathname GetCurrentDirectory();
1290e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1300e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org private:
1310e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#if defined(ANDROID) || defined(IOS)
1320e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static char* provided_app_data_folder_;
1330e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static char* provided_app_temp_folder_;
1340e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#else
1350e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static char* app_temp_path_;
1360e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif
1370e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1380e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org  static char* CopyString(const std::string& str);
1390e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org};
1400e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1410e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org}  // namespace talk_base
1420e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org
1430e118e7129884fbea117e78d6f2068139a414dbhenrike@webrtc.org#endif  // TALK_BASE_UNIXFILESYSTEM_H_
144