15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/*
25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle
35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2004--2006, Google Inc.
45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without
65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met:
75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  1. Redistributions of source code must retain the above copyright notice,
95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer.
105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  2. Redistributions in binary form must reproduce the above copyright notice,
115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     this list of conditions and the following disclaimer in the documentation
125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     and/or other materials provided with the distribution.
135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *  3. The name of the author may not be used to endorse or promote products
145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *     derived from this software without specific prior written permission.
155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org *
165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */
275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#ifndef TALK_BASE_UNIXFILESYSTEM_H_
295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#define TALK_BASE_UNIXFILESYSTEM_H_
305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include <sys/types.h>
325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/fileutils.h"
345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgnamespace talk_base {
365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgclass UnixFilesystem : public FilesystemInterface {
385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org public:
395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#if defined(ANDROID) || defined(IOS)
415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Android does not have a native code API to fetch the app data or temp
425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// folders. That needs to be passed into this class from Java. Similarly, iOS
435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// only supports an Objective-C API for fetching the folder locations, so that
445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// needs to be passed in here from Objective-C.
455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static void SetAppDataFolder(const std::string& folder);
475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static void SetAppTempFolder(const std::string& folder);
485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif
495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Opens a file. Returns an open StreamInterface if function succeeds.
515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Otherwise, returns NULL.
525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual FileStream *OpenFile(const Pathname &filename,
535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                               const std::string &mode);
545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Atomically creates an empty file accessible only to the current user if one
565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // does not already exist at the given path, otherwise fails.
575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool CreatePrivateFile(const Pathname &filename);
585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // This will attempt to delete the file located at filename.
605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // It will fail with VERIY if you pass it a non-existant file, or a directory.
615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool DeleteFile(const Pathname &filename);
625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // This will attempt to delete the folder located at 'folder'
645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // It ASSERTs and returns false if you pass it a non-existant folder or a
655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // plain file.
665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool DeleteEmptyFolder(const Pathname &folder);
675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Creates a directory. This will call itself recursively to create /foo/bar
695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // even if /foo does not exist. All created directories are created with the
705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // given mode.
715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns TRUE if function succeeds
725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool CreateFolder(const Pathname &pathname, mode_t mode);
735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // As above, with mode = 0755.
755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool CreateFolder(const Pathname &pathname);
765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // This moves a file from old_path to new_path, where "file" can be a plain
785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // file or directory, which will be moved recursively.
795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns true if function succeeds.
805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool MoveFile(const Pathname &old_path, const Pathname &new_path);
815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool MoveFolder(const Pathname &old_path, const Pathname &new_path);
825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // This copies a file from old_path to _new_path where "file" can be a plain
845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // file or directory, which will be copied recursively.
855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns true if function succeeds
865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool CopyFile(const Pathname &old_path, const Pathname &new_path);
875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns true if a pathname is a directory
895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool IsFolder(const Pathname& pathname);
905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns true if pathname represents a temporary location on the system.
925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool IsTemporaryPath(const Pathname& pathname);
935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns true of pathname represents an existing file
955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool IsFile(const Pathname& pathname);
965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns true if pathname refers to no filesystem object, every parent
985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // directory either exists, or is also absent.
995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool IsAbsent(const Pathname& pathname);
1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual std::string TempFilename(const Pathname &dir,
1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                   const std::string &prefix);
1035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // A folder appropriate for storing temporary files (Contents are
1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // automatically deleted when the program exists)
1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetTemporaryFolder(Pathname &path, bool create,
1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                                 const std::string *append);
1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetFileSize(const Pathname& path, size_t* size);
1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetFileTime(const Pathname& path, FileTimeType which,
1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org                           time_t* time);
1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns the path to the running application.
1145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetAppPathname(Pathname* path);
1155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetAppDataFolder(Pathname* path, bool per_user);
1175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Get a temporary folder that is unique to the current user and application.
1195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetAppTempFolder(Pathname* path);
1205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual bool GetDiskFreeSpace(const Pathname& path, int64 *freebytes);
1225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  // Returns the absolute path of the current directory.
1245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  virtual Pathname GetCurrentDirectory();
1255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org private:
1275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#if defined(ANDROID) || defined(IOS)
1285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static char* provided_app_data_folder_;
1295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static char* provided_app_temp_folder_;
1305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#else
1315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static char* app_temp_path_;
1325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif
1335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org  static char* CopyString(const std::string& str);
1355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org};
1365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org}  // namespace talk_base
1385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org
1395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#endif  // TALK_BASE_UNIXFILESYSTEM_H_
140