PTLib  Version 2.18.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
file.h
Go to the documentation of this file.
1 /*
2  * file.h
3  *
4  * Operating System file I/O channel class.
5  *
6  * Portable Windows Library
7  *
8  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
9  *
10  * The contents of this file are subject to the Mozilla Public License
11  * Version 1.0 (the "License"); you may not use this file except in
12  * compliance with the License. You may obtain a copy of the License at
13  * http://www.mozilla.org/MPL/
14  *
15  * Software distributed under the License is distributed on an "AS IS"
16  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
17  * the License for the specific language governing rights and limitations
18  * under the License.
19  *
20  * The Original Code is Portable Windows Library.
21  *
22  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
23  *
24  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
25  * All Rights Reserved.
26  *
27  * Contributor(s): ______________________________________.
28  */
29 
30 
31 #ifndef PTLIB_FILE_H
32 #define PTLIB_FILE_H
33 
34 #ifdef P_USE_PRAGMA
35 #pragma interface
36 #endif
37 
38 #ifndef _WIN32
39 #include <sys/stat.h>
40 #endif
41 
42 #include <ptlib/bitwise_enum.h>
43 
44 
46 // Binary Files
47 
57 class PFile : public PChannel
58 {
59  PCLASSINFO(PFile, PChannel);
60 
61  public:
68  PFile();
69 
74  enum OpenMode {
78  };
79 
90  P_DECLARE_BITWISE_ENUM_EX(OpenOptions, 7,
91  (NoOptions,
92  MustExist,
93  Create,
94  Truncate,
95  Exclusive,
96  Temporary,
97  DenySharedRead,
98  DenySharedWrite
99  ),
100  ModeDefault = -1
101  );
102 
111  PFile(
112  OpenMode mode,
113  OpenOptions opts = ModeDefault
114  );
115 
122  PFile(
123  const PFilePath & name,
124  OpenMode mode = ReadWrite,
125  OpenOptions opts = ModeDefault
126  );
127 
129  ~PFile();
131 
132 
142  const PObject & obj
143  ) const;
145 
146 
155  virtual PString GetName() const;
156 
168  virtual PBoolean Read(
169  void * buf,
170  PINDEX len
171  );
172 
182  virtual PBoolean Write(
183  const void * buf,
184  PINDEX len
185  );
186 
190  virtual PBoolean Close();
192 
193 
203  static bool Exists(
204  const PFilePath & name
205  );
206 
214  bool Exists() const;
215 
225  static bool Access(
226  const PFilePath & name,
227  OpenMode mode
228  );
229 
240  bool Access(
241  OpenMode mode
242  );
243 
251  static bool Touch(
252  const PFilePath & name,
253  const PTime & accessTime = 0
254  );
255  static bool Touch(
256  const PFilePath & name,
257  const PTime & accessTime,
258  const PTime & modTime
259  );
260 
266  bool Touch(
267  const PTime & accessTime = 0
268  );
269  bool Touch(
270  const PTime & accessTime,
271  const PTime & modTime
272  );
273 
286  static bool Remove(
287  const PFilePath & name,
288  bool force = false
289  );
290  static bool Remove(
291  const PString & name,
292  bool force = false
293  );
294 
307  bool Remove(
308  bool force = false
309  );
310 
326  static bool Rename(
327  const PFilePath & oldname,
328  const PString & newname,
329  bool force = false
330  );
331 
348  bool Rename(
349  const PString & newname,
350  bool force = false
351  );
352 
358  static bool Copy(
359  const PFilePath & oldname,
360  const PFilePath & newname,
361  bool force = false,
362  bool recurse = false
363  );
364 
370  bool Copy(
371  const PFilePath & newname,
372  bool force = false,
373  bool recurse = false
374  );
375 
385  static bool Move(
386  const PFilePath & oldname,
387  const PFilePath & newname,
388  bool force = false,
389  bool recurse = false
390  );
391 
401  bool Move(
402  const PFilePath & newname,
403  bool force = false,
404  bool recurse = false
405  );
407 
416  const PFilePath & GetFilePath() const;
417 
421  void SetFilePath(
422  const PString & path
423  );
424 
425 
437  bool Open(
438  OpenMode mode = ReadWrite,
439  OpenOptions opts = ModeDefault
440  );
441 
449  bool Open(
450  OpenMode mode,
451  OpenOptions opts,
452  PFileInfo::Permissions permissions
453  );
454 
462  bool Open(
463  const PFilePath & name,
464  OpenMode mode = ReadWrite,
465  OpenOptions opts = ModeDefault
466  );
467 
475  bool Open(
476  const PFilePath & name,
477  OpenMode mode,
478  OpenOptions opts,
479  PFileInfo::Permissions permissions
480  );
481 
487  virtual off_t GetLength() const;
488 
495  virtual PBoolean SetLength(
496  off_t len // New length of file.
497  );
498 
501  Start = SEEK_SET,
502  Current = SEEK_CUR,
503  End = SEEK_END
504  };
505 
516  virtual PBoolean SetPosition(
517  off_t pos,
518  FilePositionOrigin origin = Start
519  );
520 
527  virtual off_t GetPosition() const;
528 
535  bool IsEndOfFile() const;
536 
542  static bool GetInfo(
543  const PFilePath & name,
544  PFileInfo & info
545  );
546 
552  bool GetInfo(
553  PFileInfo & info
554  );
555 
561  static bool SetPermissions(
562  const PFilePath & name,
563  PFileInfo::Permissions permissions
564  );
570  bool SetPermissions(
571  PFileInfo::Permissions permissions
572  );
573 
587  struct RotateInfo
588  {
589  static const PString & DefaultTimestamp();
590  explicit RotateInfo(
591  const PDirectory & dir = PDirectory(),
592  const PString & prefix = PString::Empty(), // If empty, uses PProcess::Current().GetName()
593  const PString & suffix = PString::Empty(),
594  const PString & timestamp = DefaultTimestamp(),
595  off_t maxSize = 1000000000, // A gigabyte
596  off_t minFree = -10 // Percent
597  );
598  RotateInfo(const RotateInfo & other);
599  RotateInfo & operator=(const RotateInfo & other);
600  virtual ~RotateInfo() { }
601 
605  bool CanRotate() const;
606 
618  bool Rotate(
619  PFile & activeFile,
620  bool force = false,
621  const PTime & now = PTime()
622  );
623 
627  virtual void OnCloseFile(
628  PFile & file,
629  const PFilePath & rotatedTo
630  );
631 
635  virtual bool OnOpenFile(
636  PFile & file
637  );
638 
641  virtual void OnMessage(
642  bool error,
643  const PString & msg
644  );
645 
647  SizeOnly,
648  Hourly,
649  Daily,
650  Weekly,
651  Monthly
652  );
653 
659  off_t m_maxSize;
660  off_t m_freeDisk;
661  Period m_period;
662  unsigned m_maxFileCount;
664 
666  };
668 
669  protected:
670  virtual bool InternalOpen(OpenMode mode, OpenOptions opts, PFileInfo::Permissions permissions);
671 
672  // Member variables
673 
676 
677 
678 // Include platform dependent part of class
679 #ifdef _WIN32
680 #include "msos/ptlib/file.h"
681 #else
682 #include "unix/ptlib/file.h"
683 #endif
684 };
685 
686 
687 #endif // PTLIB_FILE_H
688 
689 
690 // End Of File ///////////////////////////////////////////////////////////////
off_t m_maxSize
Size in bytes which triggers a rotation, default zero disables.
Definition: file.h:659
virtual PBoolean Close()
Close the file channel.
virtual PBoolean Read(void *buf, PINDEX len)
Low level read from the file channel.
const PFilePath & GetFilePath() const
Get the full path name of the file.
static bool Copy(const PFilePath &oldname, const PFilePath &newname, bool force=false, bool recurse=false)
Make a copy of the specified file.
This class represents a disk file.
Definition: file.h:57
This class defines an arbitrary time interval to millisecond accuracy.
Definition: timeint.h:51
static bool Move(const PFilePath &oldname, const PFilePath &newname, bool force=false, bool recurse=false)
Move the specified file.
bool IsEndOfFile() const
Determine if the current file position is at the end of the file.
static bool Rename(const PFilePath &oldname, const PString &newname, bool force=false)
Change the specified files name.
bool Rotate(PFile &activeFile, bool force=false, const PTime &now=PTime())
Execute a rotation.
This class defines an absolute time and date.
Definition: ptime.h:49
PDirectory m_directory
Destination directory for rotated file, default to same s log file.
Definition: file.h:654
virtual off_t GetLength() const
Get the current size of the file.
Information on how to rotate files.
Definition: file.h:587
This class describes a full description for a file on the particular platform.
Definition: filepath.h:61
static const PString & DefaultTimestamp()
virtual void OnCloseFile(PFile &file, const PFilePath &rotatedTo)
Callback when a rotation of an open file is about to be performed.
File can be both read and written.
Definition: file.h:77
Comparison
Result of the comparison operation performed by the Compare() function.
Definition: object.h:2251
P_DECLARE_STREAMABLE_ENUM(Period, SizeOnly, Hourly, Daily, Weekly, Monthly)
virtual void OnMessage(bool error, const PString &msg)
Callback when have a message on thte rotation.
virtual PBoolean SetPosition(off_t pos, FilePositionOrigin origin=Start)
Set the current active position in the file for the next read or write operation. ...
virtual bool OnOpenFile(PFile &file)
Callback to open the file.
File can be read but not written.
Definition: file.h:75
unsigned m_maxFileCount
When this many files have been rotated, oldest is deleted.
Definition: file.h:662
PString m_timestamp
Time template for rotated file, default &quot;_yyyy_MM_dd_hh_mm&quot;.
Definition: file.h:656
PFilePath m_path
The fully qualified path name for the file.
Definition: file.h:674
bool Exists() const
Check for file existance.
static bool Touch(const PFilePath &name, const PTime &accessTime=0)
Set access &amp; modification times for file.
Class to represent a directory in the operating system file system.
Definition: pdirect.h:173
Set position relative to current file position.
Definition: file.h:502
virtual bool InternalOpen(OpenMode mode, OpenOptions opts, PFileInfo::Permissions permissions)
P_DECLARE_BITWISE_ENUM_EX(OpenOptions, 7,(NoOptions, MustExist, Create, Truncate, Exclusive, Temporary, DenySharedRead, DenySharedWrite), ModeDefault=-1)
When a file is opened, a number of options may be associated with the open file.
off_t m_freeDisk
Minimum free space for the disk. Negative is percentage, positive is bytes.
Definition: file.h:660
PFilePathString m_suffix
File name suffix, default &quot;.log&quot;.
Definition: file.h:658
virtual PBoolean Write(const void *buf, PINDEX len)
Low level write to the file channel.
PTime m_lastTime
Definition: file.h:665
Abstract class defining I/O channel semantics.
Definition: channel.h:103
FilePositionOrigin
Options for the origin in setting the file position.
Definition: file.h:500
bool m_removeOnClose
File is to be removed when closed.
Definition: file.h:675
File can be written but not read.
Definition: file.h:76
virtual off_t GetPosition() const
Get the current active position in the file for the next read or write operation. ...
bool PBoolean
Definition: object.h:174
bool CanRotate() const
Inidcate that the RotateInfo is configured so that rotations can be made via the Rotate() function...
PFile()
Create a file object but do not open it.
The character string class.
Definition: pstring.h:108
void SetFilePath(const PString &path)
Set the full path name of the file.
PTimeInterval m_maxFileAge
Rotated files older than this are deleted.
Definition: file.h:663
PFilePathString m_prefix
File name prefix, default PProcess::GetName()
Definition: file.h:655
Base string type for a file path.
Definition: filepath.h:44
Period m_period
Rotate on the peroid regardless of size.
Definition: file.h:661
int m_timeZone
TIme zone for output and rotated file names.
Definition: file.h:657
Set position relative to end of file.
Definition: file.h:503
static bool GetInfo(const PFilePath &name, PFileInfo &info)
Get information (eg protection, timestamps) on the specified file.
static const PString & Empty()
Return an empty string.
static bool Remove(const PFilePath &name, bool force=false)
Delete the specified file.
static bool SetPermissions(const PFilePath &name, PFileInfo::Permissions permissions)
Set permissions on the specified file.
RotateInfo & operator=(const RotateInfo &other)
static bool Access(const PFilePath &name, OpenMode mode)
Check for file access modes.
RotateInfo(const PDirectory &dir=PDirectory(), const PString &prefix=PString::Empty(), const PString &suffix=PString::Empty(), const PString &timestamp=DefaultTimestamp(), off_t maxSize=1000000000, off_t minFree=-10)
~PFile()
Close the file on destruction.
virtual PString GetName() const
Get the platform and I/O channel type name of the channel.
Set position relative to start of file.
Definition: file.h:501
virtual PBoolean SetLength(off_t len)
Set the size of the file, padding with 0 bytes if it would require expanding the file, or truncating it if being made shorter.
virtual ~RotateInfo()
Definition: file.h:600
Class containing the system information on a file path.
Definition: pdirect.h:65
Ultimate parent class for all objects in the class library.
Definition: object.h:2204
Comparison Compare(const PObject &obj) const
Determine the relative rank of the two objects.
OpenMode
When a file is opened, it may restrict the access available to operations on the object instance...
Definition: file.h:74
bool Open(OpenMode mode=ReadWrite, OpenOptions opts=ModeDefault)
Open the current file in the specified mode and with the specified options.