blocxx
FileSystem.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2005, Quest Software, Inc. All rights reserved.
3 * Copyright (C) 2006, Novell, Inc. All rights reserved.
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 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * * Neither the name of
14 * Quest Software, Inc.,
15 * nor Novell, Inc.,
16 * nor the names of its contributors or employees may be used to
17 * endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *******************************************************************************/
32 
33 
40 #ifndef BLOCXX_FILESYSTEM_HPP_INCLUDE_GUARD_
41 #define BLOCXX_FILESYSTEM_HPP_INCLUDE_GUARD_
42 #include "blocxx/BLOCXX_config.h"
43 #include "blocxx/Types.hpp"
44 #include "blocxx/ArrayFwd.hpp"
45 #include "blocxx/Exception.hpp"
46 #include "blocxx/CommonFwd.hpp"
47 #include "blocxx/String.hpp"
48 #ifdef BLOCXX_ENABLE_TEST_HOOKS
49 #include "blocxx/GlobalPtr.hpp"
50 #endif
51 
52 #include <utility>
53 
54 #ifdef BLOCXX_HAVE_SYS_PARAM_H
55 #include <sys/param.h>
56 #endif
57 #ifndef MAXPATHLEN
58 #ifdef PATH_MAX
59 #define MAXPATHLEN PATH_MAX
60 #else
61 #define MAXPATHLEN 1024
62 #endif
63 #endif
64 
65 namespace BLOCXX_NAMESPACE
66 {
67 
68 BLOCXX_DECLARE_APIEXCEPTION(FileSystem, BLOCXX_COMMON_API)
69 
70 
74 namespace FileSystem
75 {
80  BLOCXX_COMMON_API File openFile(const String& path);
88  BLOCXX_COMMON_API File createFile(const String& path);
95  BLOCXX_COMMON_API File openOrCreateFile(const String& path);
102  BLOCXX_COMMON_API File openForAppendOrCreateFile(const String& path);
111  BLOCXX_COMMON_API File createAutoDeleteTempFile(const String& dir=String());
123  BLOCXX_COMMON_API File createTempFile(String& filePath,
124  const String& dir=String());
131  BLOCXX_COMMON_API int changeFileOwner(const String& filename,
132  const UserId& userId);
136  BLOCXX_COMMON_API bool exists(const String& path);
137 #ifndef BLOCXX_WIN32
138 
146  BLOCXX_COMMON_API bool isExecutable(const String& path);
147 #endif
148 
151  BLOCXX_COMMON_API bool canRead(const String& path);
155  BLOCXX_COMMON_API bool canWrite(const String& path);
156 #ifndef BLOCXX_WIN32
157 
165  BLOCXX_COMMON_API bool isLink(const String& path);
166 #endif
167 
170  BLOCXX_COMMON_API bool isDirectory(const String& path);
176  BLOCXX_COMMON_API bool changeDirectory(const String& path);
183 #ifndef BLOCXX_WIN32
184  BLOCXX_COMMON_API bool makeDirectory(const String& path, int mode=0777);
185 #else
186  BLOCXX_COMMON_API bool makeDirectory(const String& path, int mode=-1);
187 #endif
188 
194  BLOCXX_COMMON_API bool getFileSize(const String& path, Int64& size);
200  BLOCXX_COMMON_API UInt64 fileSize(FileHandle fh);
206  BLOCXX_COMMON_API bool removeDirectory(const String& path);
212  BLOCXX_COMMON_API bool removeFile(const String& path);
219  BLOCXX_COMMON_API bool getDirectoryContents(const String& path,
220  StringArray& dirEntries);
227  BLOCXX_COMMON_API bool renameFile(const String& oldFileName,
228  const String& newFileName);
240  BLOCXX_COMMON_API size_t read(const FileHandle& hdl, void* bfr, size_t numberOfBytes,
241  Int64 offset=-1L);
252  BLOCXX_COMMON_API size_t write(FileHandle hdl, const void* bfr,
253  size_t numberOfBytes, Int64 offset=-1L);
265  BLOCXX_COMMON_API Int64 seek(const FileHandle& hdl, Int64 offset, int whence);
271  BLOCXX_COMMON_API Int64 tell(const FileHandle& hdl);
277  BLOCXX_COMMON_API void rewind(const FileHandle& hdl);
283  BLOCXX_COMMON_API int close(const FileHandle& hdl);
288  BLOCXX_COMMON_API int flush(FileHandle& hdl);
296  BLOCXX_COMMON_API String getFileContents(const String& filename);
297 
305  BLOCXX_COMMON_API StringArray getFileLines(const String& filename);
306 
314  BLOCXX_COMMON_API String readSymbolicLink(const String& path);
315 
316  namespace Path
317  {
339  BLOCXX_COMMON_API String realPath(const String& path);
340 
342  {
344  };
345 
367  BLOCXX_COMMON_API std::pair<ESecurity, String>
368  security(String const & path, UserId uid);
369 
374  BLOCXX_COMMON_API std::pair<ESecurity, String> security(String const & path);
375 
388  BLOCXX_COMMON_API std::pair<ESecurity, String>
389  security(String const & base_dir, String const & rel_path, UserId uid);
390 
395  BLOCXX_COMMON_API std::pair<ESecurity, String>
396  security(String const & base_dir, String const & rel_path);
397 
408  BLOCXX_COMMON_API String dirname(const String& filename);
409 
416  BLOCXX_COMMON_API String basename(const String& filename);
417 
426  BLOCXX_COMMON_API String getCurrentWorkingDirectory();
427 
428 
429  } // end namespace Path
430 
431  struct NullFactory
432  {
433  static void* create()
434  {
435  return 0;
436  }
437  };
438 #ifdef BLOCXX_ENABLE_TEST_HOOKS
449  extern FileSystemMockObject_t g_fileSystemMockObject;
450 #endif
451 
452 } // end namespace FileSystem
453 
454 } // end namespace BLOCXX_NAMESPACE
455 
456 #endif
BLOCXX_COMMON_API bool canWrite(const String &path)
Taken from RFC 1321.
BLOCXX_COMMON_API bool getFileSize(const String &path, Int64 &size)
Get the size of the file in bytes.
BLOCXX_COMMON_API Int64 tell(const FileHandle &hdl)
BLOCXX_COMMON_API File openForAppendOrCreateFile(const String &path)
Opens the file for the given name to append data or create if it does not exist.
BLOCXX_COMMON_API String realPath(const String &path)
BLOCXX_COMMON_API StringArray getFileLines(const String &filename)
Read and return the lines of a test file.
BLOCXX_COMMON_API int close(const FileHandle &hdl)
Close file handle.
BLOCXX_COMMON_API int flush(FileHandle &hdl)
Flush any buffered data to the file if buffering supported.
BLOCXX_COMMON_API File openOrCreateFile(const String &path)
Opens or creates the file for the given name.
This String class is an abstract data type that represents as NULL terminated string of characters...
Definition: String.hpp:66
BLOCXX_COMMON_API std::pair< ESecurity, String > security(String const &base_dir, String const &rel_path)
Equivalent to security(base_dir, rel_path, uid), where uid is the effective user ID of the process...
BLOCXX_COMMON_API UInt64 fileSize(FileHandle fh)
Get the size of a file from the file handle.
BLOCXX_COMMON_API bool isDirectory(const String &path)
BLOCXX_COMMON_API bool renameFile(const String &oldFileName, const String &newFileName)
Rename the given file to the new name.
BLOCXX_COMMON_API bool removeFile(const String &path)
Remove the given file.
GlobalPtr< FileSystemMockObject, NullFactory > FileSystemMockObject_t
BLOCXX_COMMON_API size_t read(const FileHandle &hdl, void *bfr, size_t numberOfBytes, Int64 offset=-1L)
Read data from file.
BLOCXX_COMMON_API String getFileContents(const String &filename)
Read and return the contents of a text file.
BLOCXX_COMMON_API bool removeDirectory(const String &path)
Remove the given directory.
BLOCXX_COMMON_API bool exists(const String &path)
BLOCXX_COMMON_API bool canRead(const String &path)
BLOCXX_COMMON_API bool changeDirectory(const String &path)
Change to the given directory.
BLOCXX_COMMON_API bool makeDirectory(const String &path, int mode=0777)
Create a directory.
BLOCXX_COMMON_API size_t write(FileHandle hdl, const void *bfr, size_t numberOfBytes, Int64 offset=-1L)
Write data to a file.
BLOCXX_COMMON_API int changeFileOwner(const String &filename, const UserId &userId)
Change the given file ownership.
BLOCXX_COMMON_API File createFile(const String &path)
Create the file for the given name.
BLOCXX_COMMON_API String readSymbolicLink(const String &path)
Read the value of a symbolic link.
The purpose of the File class is to provide an abstraction layer over the platform dependant function...
Definition: File.hpp:54
BLOCXX_COMMON_API bool getDirectoryContents(const String &path, StringArray &dirEntries)
Get the names of the files (and directories) in the given directory.
BLOCXX_COMMON_API Int64 seek(const FileHandle &hdl, Int64 offset, int whence)
Seek to a given offset within the file.
BLOCXX_COMMON_API String basename(const String &filename)
Take a string that contains a pathname, and return a string that is the filename with the path remove...
BLOCXX_COMMON_API File createTempFile(String &filePath, const String &dir=String())
Create a tempororary file in an optional directory.
FileSystemMockObject_t g_fileSystemMockObject
BLOCXX_COMMON_API bool isExecutable(const String &path)
Tests if a file is executable.
#define BLOCXX_DECLARE_APIEXCEPTION(NAME, LINKAGE_SPEC)
Declare a new exception class named Exception that derives from Exception This macro is typical...
Definition: Exception.hpp:396
BLOCXX_COMMON_API String getCurrentWorkingDirectory()
Get the process's current working directory.
BLOCXX_COMMON_API File createAutoDeleteTempFile(const String &dir=String())
Create a tempororary file that will be removed when the returned File object is closed.
BLOCXX_COMMON_API void rewind(const FileHandle &hdl)
Position the file pointer associated with the given file handle to the beginning of the file...
BLOCXX_COMMON_API String dirname(const String &filename)
Take a string that contains a pathname, and return a string that is a pathname of the parent director...
BLOCXX_COMMON_API File openFile(const String &path)
Open a file for read/write and return an File object that can be used for reading and writing...
BLOCXX_COMMON_API bool isLink(const String &path)
Tests if a file is a symbolic link.
This class can be used to store a global pointer.
Definition: GlobalPtr.hpp:83