Add FileHandle::truncate and ftruncate

Add support for file truncation (or extension) to the abstract API.

No hooks to actual implementations in this commit.
pull/8972/head
Kevin Bracey 2018-06-07 14:23:38 +03:00
parent ff7a316a32
commit ae17f6ebba
3 changed files with 33 additions and 0 deletions

View File

@ -138,6 +138,21 @@ public:
*/
virtual off_t size();
/** Truncate or extend a file.
*
* The file's length is set to the specified value. The seek pointer is
* not changed. If the file is extended, the extended area appears as if
* it were zero-filled.
*
* @param length The requested new length for the file
*
* @return Zero on success, negative error code on failure
*/
virtual int truncate(off_t length)
{
return -EINVAL;
}
/** Move the file position to a given offset from a given location.
*
* @param offset The offset from whence to move to

View File

@ -842,6 +842,23 @@ extern "C" off_t lseek(int fildes, off_t offset, int whence)
return off;
}
extern "C" int ftruncate(int fildes, off_t length)
{
FileHandle *fhc = get_fhc(fildes);
if (fhc == NULL) {
errno = EBADF;
return -1;
}
int err = fhc->truncate(length);
if (err < 0) {
errno = -err;
return -1;
} else {
return 0;
}
}
#ifdef __ARMCC_VERSION
extern "C" int PREFIX(_ensure)(FILEHANDLE fh)
{

View File

@ -524,6 +524,7 @@ extern "C" {
ssize_t write(int fildes, const void *buf, size_t nbyte);
ssize_t read(int fildes, void *buf, size_t nbyte);
off_t lseek(int fildes, off_t offset, int whence);
int ftruncate(int fildes, off_t length);
int isatty(int fildes);
int fsync(int fildes);
int fstat(int fildes, struct stat *st);