Planeshift

fileutil.h

Go to the documentation of this file.
00001 /*
00002 * fileutil.h by Matthias Braun <[email protected]>
00003 *
00004 * Copyright (C) 2002 Atomic Blue ([email protected], http://www.atomicblue.org) 
00005 *
00006 *
00007 * This program is free software; you can redistribute it and/or
00008 * modify it under the terms of the GNU General Public License
00009 * as published by the Free Software Foundation (version 2 of the License)
00010 * This program is distributed in the hope that it will be useful,
00011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 * GNU General Public License for more details.
00014 * You should have received a copy of the GNU General Public License
00015 * along with this program; if not, write to the Free Software
00016 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00017 *
00018 */
00019 
00020 /* This file contains
00021 * Utility functions to access the filesystem in a platform
00022 * independant way. It would be nicer to use vfs here, but vfs is too limited
00023 * for our needs (testing for file/directory, file permissions) */
00024 
00025 #ifndef __FILEUTIL_H__
00026 #define __FILEUTIL_H__
00027 
00028 #include <psstdint.h>
00029 #include <iutil/vfs.h>
00030 #include <csutil/refcount.h>
00031 
00032 struct iVFS;
00033 
00038 class FileStat : public csRefCount
00039 {
00040 public:
00041     enum Type
00042     {
00043         TYPE_FILE,
00044         TYPE_DIRECTORY
00045     };
00046 
00047     Type type;
00048     bool link;
00049     bool executable;
00050     uint32_t size;
00051     char* target;
00052     bool readonly;
00053     unsigned short mode;
00054     short uid;
00055     short gid;
00056 
00057     FileStat ()
00058     { target = NULL; }
00059     ~FileStat ()
00060     { delete[] target; }
00061 };
00062 
00063 
00064 class FileUtil
00065 {
00066 private:
00067     csRef<iVFS> vfs;
00068 public:
00069     FileUtil(iVFS* vfs);
00070     ~FileUtil();
00071     /* Tests if the file exists and returns data about the file. */
00072     csPtr<FileStat> StatFile(const char* path);
00073 
00074     bool RemoveFile(const char* filename, bool silent = false);
00075 
00076     /* Creates a directory, given a vfs path (/this/). */
00077     void MakeDirectory(const char* directory);
00078 
00079     /* Copies a file. */
00080     bool CopyFile(csString from, csString to, bool vfsPath, bool executable, bool silent = false, bool copyPermissions = true);
00081 
00082     /* Moves a file */
00083     inline void MoveFile(csString from, csString to, bool vfsPath, bool executable, bool silent = false)
00084     {
00085         CopyFile(from, to, vfsPath, executable, silent);
00086         RemoveFile(from, silent);
00087     }
00088 
00089     /* Returns true is the file at the specified path is executable. */
00090     bool isExecutable(const char* path);
00091 
00092     /* Sets all permissions on a file. */
00093     void SetPermissions(const char* path, FileStat* fs);
00094 };
00095 
00098 #endif // __FILEUTIL_H__