Class File.

Inherits Garbage

Represents a file.

A file is read once on object construction and never read later, or opened and truncated, ready for later writing.

Once read, a file's contents() returns the entire contents of the file. To write to a file, write() is available. The file remains open until the destructor is called.

modificationTime() returns an integer that's bigger for more recently modified/created files, name() returns the file's name and valid() returns true or false depending on whether everything is okay.

File::File( const EString & name, File::Access a, uint mode )

Creates a new File object representing name. If a is Read, the contents of the file are read. If a is Write, the file is opened for writing and truncated. If a is Append, the file is opened for append. In the latter cases, the file is created with the specified mode if it does not exist.

File::File( const EString & name, uint maxLength )

Creates a new File object representing name, and tries to open it and read up to maxLength bytes, or the whole file if maxLength is 0.

File::File( int fd )

Creates a new unnamed File representing the specified fd. This is used only by the LogServer to write to stdout.

static EString File::chrooted( const EString & filename )

Returns the current name of filename based on the root() in effect, if filename starts with a '/'.

If filename is a relative name, this function does nothing.

The current root() is assumed to match the chroot directory used by the operating system. If filename isn't within root(), chrooted() logs an error. Thus, when a server runs inside chroot jail, attempts to open files or unix sockets cause sensible error messages.

EString File::contents() const

Returns the contents of the file as read by the constructor. If this file is being written to, not read, then contents() returns an empty string.

void File::init( const EString & name, File::Access a, uint mode, uint maxLength )

Initialises a file object to represent name, which is opened for the specified mode. If a is Read, maxLength specifies the number of bytes to read; and if a is Write or Append, the mode is used if the file is to be created.

EStringList * File::lines()

Returns a non-zero pointer to a EStringList containing the lines of text read from this File. The lines are returned unmodified, with trailing CRLF, if any, intact.

uint File::modificationTime() const

Returns the modification time of this file as it was at time of construction. The modification time is an opaque integer; its meaning is not specified except that more recently created/modified files have larger values of modificationTime().

EString File::name() const

Returns the name of the file, as specified to the constructor.

static EString File::root()

Returns the currently recorded root directory, which always starts and ends with a '/' character. This is just the argument of the last setRoot() call.

static void File::setRoot( const EString & d )

Records that the root directory is now d. The initial value is "/". This value is used by chrooted().

If d does not end with a '/', setRoot() appends one.

static void File::unlink( EString s )

Removes the file with name s. All operating system errors are blithely ignored.

bool File::valid() const

Returns true if this file was opened and read correctly, and false if the object is somehow invalid.

void File::write( const EString & s )

Writes s to the end of the file if this file is open for writing, and does nothing else.

OS errors are disregarded.


Destroyes the file and closes it if it still is open.

This web page based on source code belonging to The Archiveopteryx Developers. All rights reserved.