2012-12-22 23:59:46 +04:00
|
|
|
/*
|
2014-06-19 03:10:45 +04:00
|
|
|
* Copyright 2009-2014 Haiku, Inc. All rights reserved.
|
2012-12-22 23:59:46 +04:00
|
|
|
* Distributed under the terms of the MIT License.
|
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Tyler Dauwalder
|
|
|
|
* John Scipione, jscipione@gmail.com
|
|
|
|
* Ingo Weinhold, bonefish@users.sf.net
|
|
|
|
*
|
|
|
|
* Corresponds to:
|
2014-06-19 03:10:45 +04:00
|
|
|
* headers/os/storage/File.h hrev47402
|
|
|
|
* src/kits/storage/File.cpp hrev47402
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\file File.h
|
2013-02-07 06:05:00 +04:00
|
|
|
\ingroup storage
|
|
|
|
\ingroup libbe
|
2013-01-15 06:11:15 +04:00
|
|
|
\brief Provides the BFile class.
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class BFile
|
|
|
|
\ingroup storage
|
|
|
|
\ingroup libbe
|
|
|
|
\brief Provides the ability to read and write the data of a file.
|
|
|
|
|
|
|
|
The file is automatically opened when you initialize a BFile and is
|
|
|
|
automatically closed when you re-initialize or destroy the object.
|
|
|
|
|
|
|
|
Symbolic links are automatically transversed by opening a BFile.
|
|
|
|
The node that the BFile ends up opening will be the file or directory
|
|
|
|
that the link points to, not the symbolic link file itself.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile::BFile()
|
|
|
|
\brief Creates an uninitialized BFile object.
|
|
|
|
|
|
|
|
Should be followed by a call to one of the SetTo() methods, or an
|
|
|
|
assignment:
|
|
|
|
- SetTo(const entry_ref* ref, uint32 openMode)
|
|
|
|
- SetTo(const BEntry* entry, uint32 openMode)
|
|
|
|
- SetTo(const char* path, uint32 openMode)
|
|
|
|
- SetTo(const BDirectory* dir, const char* path, uint32 openMode)
|
|
|
|
- operator=(const BFile &file)
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile::BFile(const BFile& file)
|
|
|
|
\brief Creates a copy of the supplied BFile.
|
|
|
|
|
|
|
|
If \a file is uninitialized, the newly constructed BFile will be too.
|
|
|
|
|
|
|
|
\param file The BFile object to be copied.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile::BFile(const entry_ref* ref, uint32 openMode)
|
|
|
|
\brief Creates a BFile and initializes it to the file referred to by
|
2014-06-19 03:10:45 +04:00
|
|
|
the supplied entry_ref and according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param ref The entry_ref referring to the file.
|
|
|
|
\param openMode The mode in which the file should be opened.
|
|
|
|
|
|
|
|
\see SetTo(const entry_ref* ref, uint32 openMode)
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile::BFile(const BEntry* entry, uint32 openMode)
|
|
|
|
\brief Creates a BFile and initializes it to the file referred to by
|
2014-06-19 03:10:45 +04:00
|
|
|
the supplied BEntry and according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param entry The BEntry referring to the file.
|
|
|
|
\param openMode The mode in which the file should be opened.
|
|
|
|
|
|
|
|
\see SetTo(const BEntry* entry, uint32 openMode)
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile::BFile(const char* path, uint32 openMode)
|
|
|
|
\brief Creates a BFile and initializes it to the file referred to by
|
2014-06-19 03:10:45 +04:00
|
|
|
the supplied path name and according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param path The file's path name.
|
|
|
|
\param openMode The mode in which the file should be opened.
|
|
|
|
|
|
|
|
\see SetTo(const char* path, uint32 openMode)
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile::BFile(const BDirectory *dir, const char* path, uint32 openMode)
|
|
|
|
\brief Creates a BFile and initializes it to the file referred to by
|
2014-06-19 03:10:45 +04:00
|
|
|
the supplied path name relative to the specified BDirectory and
|
|
|
|
according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param dir The BDirectory, relative to which the file's path name is
|
2014-06-19 03:10:45 +04:00
|
|
|
given.
|
2012-12-22 23:59:46 +04:00
|
|
|
\param path The file's path name relative to \a dir.
|
|
|
|
\param openMode The mode in which the file should be opened.
|
|
|
|
|
|
|
|
\see SetTo(const BDirectory* dir, const char* path, uint32 openMode)
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile::~BFile()
|
|
|
|
\brief Destroys the BFile object and frees all allocated resources.
|
|
|
|
|
|
|
|
If the file is properly initialized, the file descriptor is closed.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn status_t BFile::SetTo(const entry_ref* ref, uint32 openMode)
|
|
|
|
\brief Re-initializes the BFile to the file referred to by the
|
2014-06-19 03:10:45 +04:00
|
|
|
supplied entry_ref and according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param ref The entry_ref referring to the file.
|
|
|
|
\param openMode The mode in which the file should be opened
|
2014-06-19 03:10:45 +04:00
|
|
|
\a openMode must be a bitwise or of exactly one of the flags.
|
|
|
|
- \c B_READ_ONLY: The file is opened read only.
|
|
|
|
- \c B_WRITE_ONLY: The file is opened write only.
|
|
|
|
- \c B_READ_WRITE: The file is opened for random read/write access.
|
|
|
|
and any number of the flags
|
|
|
|
- \c B_CREATE_FILE: A new file will be created, if it does not
|
|
|
|
already exist.
|
|
|
|
- \c B_FAIL_IF_EXISTS: If the file does already exist and
|
|
|
|
\c B_CREATE_FILE is set, SetTo() fails.
|
|
|
|
- \c B_ERASE_FILE: An already existing file is truncated to zero
|
|
|
|
size.
|
|
|
|
- \c B_OPEN_AT_END: Seek() to the end of the file after opening.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\returns A status code.
|
|
|
|
\retval B_OK Everything went fine.
|
|
|
|
\retval B_BAD_VALUE \c NULL \a ref or bad \a openMode.
|
|
|
|
\retval B_ENTRY_NOT_FOUND File not found or failed to create file.
|
|
|
|
\retval B_FILE_EXISTS File exists and \c B_FAIL_IF_EXISTS was passed.
|
|
|
|
\retval B_PERMISSION_DENIED File permissions didn't allow operation.
|
|
|
|
\retval B_NO_MEMORY Insufficient memory for operation.
|
|
|
|
\retval B_LINK_LIMIT Indicates a cyclic loop within the file system.
|
|
|
|
\retval B_BUSY A node was busy.
|
|
|
|
\retval B_FILE_ERROR A general file error.
|
|
|
|
\retval B_NO_MORE_FDS The application has run out of file descriptors.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn status_t BFile::SetTo(const BEntry* entry, uint32 openMode)
|
|
|
|
\brief Re-initializes the BFile to the file referred to by the
|
2014-06-19 03:10:45 +04:00
|
|
|
supplied BEntry and according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param entry the BEntry referring to the file
|
|
|
|
\param openMode the mode in which the file should be opened
|
|
|
|
|
|
|
|
\returns A status code.
|
|
|
|
\retval B_OK Everything went fine.
|
|
|
|
\retval B_BAD_VALUE \c NULL \a entry or bad \a openMode.
|
|
|
|
\retval B_ENTRY_NOT_FOUND File not found or failed to create file.
|
|
|
|
\retval B_FILE_EXISTS File exists and \c B_FAIL_IF_EXISTS was passed.
|
|
|
|
\retval B_PERMISSION_DENIED File permissions didn't allow operation.
|
|
|
|
\retval B_NO_MEMORY Insufficient memory for operation.
|
|
|
|
\retval B_LINK_LIMIT Indicates a cyclic loop within the file system.
|
|
|
|
\retval B_BUSY A node was busy.
|
|
|
|
\retval B_FILE_ERROR A general file error.
|
|
|
|
\retval B_NO_MORE_FDS The application has run out of file descriptors.
|
|
|
|
|
|
|
|
\todo Implemented using SetTo(entry_ref*, uint32). Check, if necessary
|
2014-06-19 03:10:45 +04:00
|
|
|
to re-implement!
|
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn status_t BFile::SetTo(const char* path, uint32 openMode)
|
|
|
|
\brief Re-initializes the BFile to the file referred to by the
|
2014-06-19 03:10:45 +04:00
|
|
|
supplied path name and according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param path The file's path name.
|
|
|
|
\param openMode The mode in which the file should be opened.
|
|
|
|
|
|
|
|
\returns A status code.
|
|
|
|
\retval B_OK Everything went fine.
|
|
|
|
\retval B_BAD_VALUE \c NULL \a path or bad \a openMode.
|
|
|
|
\retval B_ENTRY_NOT_FOUND File not found or failed to create file.
|
|
|
|
\retval B_FILE_EXISTS File exists and \c B_FAIL_IF_EXISTS was passed.
|
|
|
|
\retval B_PERMISSION_DENIED File permissions didn't allow operation.
|
|
|
|
\retval B_NO_MEMORY Insufficient memory for operation.
|
|
|
|
\retval B_LINK_LIMIT Indicates a cyclic loop within the file system.
|
|
|
|
\retval B_BUSY A node was busy.
|
|
|
|
\retval B_FILE_ERROR A general file error.
|
|
|
|
\retval B_NO_MORE_FDS The application has run out of file descriptors.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn status_t BFile::SetTo(const BDirectory* dir, const char* path,
|
2014-06-19 03:10:45 +04:00
|
|
|
uint32 openMode)
|
2012-12-22 23:59:46 +04:00
|
|
|
\brief Re-initializes the BFile to the file referred to by the
|
2014-06-19 03:10:45 +04:00
|
|
|
supplied path name relative to the specified BDirectory and
|
|
|
|
according to the specified open mode.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param dir The BDirectory, relative to which the file's path name is
|
2014-06-19 03:10:45 +04:00
|
|
|
given.
|
2012-12-22 23:59:46 +04:00
|
|
|
\param path The file's path name relative to \a dir.
|
|
|
|
\param openMode The mode in which the file should be opened.
|
|
|
|
|
|
|
|
\returns A status code.
|
|
|
|
\retval B_OK Everything went fine.
|
|
|
|
\retval B_BAD_VALUE \c NULL \a dir or \a path or bad \a openMode.
|
|
|
|
\retval B_ENTRY_NOT_FOUND File not found or failed to create file.
|
|
|
|
\retval B_FILE_EXISTS File exists and \c B_FAIL_IF_EXISTS was passed.
|
|
|
|
\retval B_PERMISSION_DENIED File permissions didn't allow operation.
|
|
|
|
\retval B_NO_MEMORY Insufficient memory for operation.
|
|
|
|
\retval B_LINK_LIMIT Indicates a cyclic loop within the file system.
|
|
|
|
\retval B_BUSY A node was busy.
|
|
|
|
\retval B_FILE_ERROR A general file error.
|
|
|
|
\retval B_NO_MORE_FDS The application has run out of file descriptors.
|
|
|
|
|
|
|
|
\todo Implemented using SetTo(BEntry*, uint32). Check, if necessary
|
2014-06-19 03:10:45 +04:00
|
|
|
to re-implement!
|
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool BFile::IsReadable() const
|
|
|
|
\brief Reports whether or not the file is readable.
|
|
|
|
|
|
|
|
\return
|
|
|
|
- \c true, if the BFile has been initialized properly and the file has
|
|
|
|
been been opened for reading,
|
|
|
|
- \c false, otherwise.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn bool BFile::IsWritable() const
|
|
|
|
\brief Reports whether or not the file is writable.
|
|
|
|
|
|
|
|
\return
|
|
|
|
- \c true, if the BFile has been initialized properly and the file has
|
|
|
|
been opened for writing,
|
|
|
|
- \c false, otherwise.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
ssize_t BFile::Read(void* buffer, size_t size)
|
|
|
|
\brief Reads a number of bytes from the file into a buffer.
|
|
|
|
|
|
|
|
\param buffer The buffer the data from the file shall be written to.
|
|
|
|
\param size The number of bytes that shall be read.
|
|
|
|
|
|
|
|
\returns The number of bytes read or an error code.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn ssize_t BFile::ReadAt(off_t location, void* buffer, size_t size)
|
|
|
|
\brief Reads a number of bytes from a certain position within the file
|
2014-06-19 03:10:45 +04:00
|
|
|
into a buffer.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param location The position (in bytes) within the file from which the
|
2014-06-19 03:10:45 +04:00
|
|
|
data shall be read.
|
2012-12-22 23:59:46 +04:00
|
|
|
\param buffer The buffer the data from the file shall be written to.
|
|
|
|
\param size The number of bytes that shall be read.
|
|
|
|
|
|
|
|
\returns The number of bytes read or an error code.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn ssize_t BFile::Write(const void* buffer, size_t size)
|
|
|
|
\brief Writes a number of bytes from a buffer into the file.
|
|
|
|
|
|
|
|
\param buffer The buffer containing the data to be written to the file.
|
|
|
|
\param size The number of bytes that shall be written.
|
|
|
|
|
|
|
|
\returns The number of bytes actually written or an error code.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn ssize_t BFile::WriteAt(off_t location, const void* buffer, size_t size)
|
2014-06-19 03:10:45 +04:00
|
|
|
\brief Writes a number of bytes from a buffer at a certain position
|
|
|
|
into the file.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param location The position (in bytes) within the file at which the data
|
2014-06-19 03:10:45 +04:00
|
|
|
shall be written.
|
2012-12-22 23:59:46 +04:00
|
|
|
\param buffer The buffer containing the data to be written to the file.
|
|
|
|
\param size The number of bytes that shall be written.
|
|
|
|
|
|
|
|
\returns The number of bytes actually written or an error code.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn off_t BFile::Seek(off_t offset, uint32 seekMode)
|
|
|
|
\brief Seeks to another read/write position within the file.
|
|
|
|
|
|
|
|
It is allowed to seek past the end of the file. A subsequent call to
|
|
|
|
Write() will pad the file with undefined data. Seeking before the
|
|
|
|
beginning of the file will fail and the behavior of subsequent Read()
|
|
|
|
or Write() invocations will be undefined.
|
|
|
|
|
|
|
|
\param offset New read/write position, depending on \a seekMode relative
|
2014-06-19 03:10:45 +04:00
|
|
|
to the beginning or the end of the file or the current position.
|
2012-12-22 23:59:46 +04:00
|
|
|
\param seekMode
|
2014-06-19 03:10:45 +04:00
|
|
|
- \c SEEK_SET: move relative to the beginning of the file.
|
|
|
|
- \c SEEK_CUR: move relative to the current position.
|
|
|
|
- \c SEEK_END: move relative to the end of the file.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\returns The new read/write position relative to the beginning of the
|
2014-06-19 03:10:45 +04:00
|
|
|
file or an error code.
|
2012-12-22 23:59:46 +04:00
|
|
|
\retval B_ERROR Trying to seek before the beginning of the file.
|
|
|
|
\retval B_FILE_ERROR The file is not properly initialized.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn off_t BFile::Position() const
|
|
|
|
\brief Gets the current read/write position within the file.
|
|
|
|
|
|
|
|
\returns The current read/write position relative to the beginning of the
|
2014-06-19 03:10:45 +04:00
|
|
|
file or an error code.
|
2012-12-22 23:59:46 +04:00
|
|
|
\retval B_ERROR After a Seek() before the beginning of the file.
|
|
|
|
\retval B_FILE_ERROR The file has not been initialized.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn status_t BFile::SetSize(off_t size)
|
|
|
|
\brief Sets the size of the file.
|
|
|
|
|
|
|
|
If the file is shorter than \a size bytes it will be padded with
|
|
|
|
unspecified data to the requested size. If it is larger, it will be
|
|
|
|
truncated.
|
|
|
|
|
|
|
|
\note There's no problem with setting the size of a BFile opened in
|
2014-06-19 03:10:45 +04:00
|
|
|
\c B_READ_ONLY mode, unless the file resides on a read only volume.
|
2012-12-22 23:59:46 +04:00
|
|
|
|
|
|
|
\param size The new file size.
|
|
|
|
|
|
|
|
\returns A status code.
|
|
|
|
\retval B_OK Everything went fine.
|
|
|
|
\retval B_NOT_ALLOWED Trying to set the size of a file on a read only
|
2014-06-19 03:10:45 +04:00
|
|
|
volume.
|
2012-12-22 23:59:46 +04:00
|
|
|
\retval B_DEVICE_FULL There's not enough space left on the volume.
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn status_t BFile::GetSize(off_t* size) const
|
|
|
|
\brief Gets the size of the file.
|
|
|
|
|
|
|
|
\param size The file size to fill out.
|
|
|
|
|
|
|
|
\returns A status code.
|
|
|
|
|
|
|
|
\see BStatable::GetSize()
|
2014-06-19 03:10:45 +04:00
|
|
|
|
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\fn BFile& BFile::operator=(const BFile &file)
|
|
|
|
\brief Assigns another BFile to this BFile.
|
|
|
|
|
|
|
|
If the other BFile is uninitialized, this one will be too. Otherwise it
|
|
|
|
will refer to the same file using the same mode, unless an error occurs.
|
|
|
|
|
|
|
|
\param file The original BFile to assign from.
|
|
|
|
|
|
|
|
\returns A reference to the assigned BFile.
|
|
|
|
|
2014-06-19 03:10:45 +04:00
|
|
|
\since BeOS R3
|
2012-12-22 23:59:46 +04:00
|
|
|
*/
|