Commit bbd9d231 authored by Sandrine Avakian's avatar Sandrine Avakian

Adding truncate function support in VFS.

So far, choice was made not to implement the truncate function whose pointer was set to NULL.
parent 8ed19292
......@@ -91,6 +91,7 @@ struct bctbx_io_methods_t {
int (*pFuncClose)(bctbx_vfs_file_t *pFile);
ssize_t (*pFuncRead)(bctbx_vfs_file_t *pFile, void* buf, size_t count, off_t offset);
ssize_t (*pFuncWrite)(bctbx_vfs_file_t *pFile, const void* buf, size_t count, off_t offset);
int (*pFuncTruncate)(bctbx_vfs_file_t *pFile, int64_t size);
int64_t (*pFuncFileSize)(bctbx_vfs_file_t *pFile);
int (*pFuncGetLineFromFd)(bctbx_vfs_file_t *pFile, char* s, int count);
off_t (*pFuncSeek)(bctbx_vfs_file_t *pFile, off_t offset, int whence);
......@@ -162,6 +163,14 @@ BCTBX_PUBLIC bctbx_vfs_file_t* bctbx_file_open2(bctbx_vfs_t *pVfs, const char *f
*/
BCTBX_PUBLIC int64_t bctbx_file_size(bctbx_vfs_file_t *pFile);
/**
* Truncates/ Extends a file.
* @param pFile bctbx_vfs_file_t File handle pointer.
* @param size New size of the file.
* @return BCTBX_VFS_ERROR if an error occured, 0 otherwise.
*/
BCTBX_PUBLIC int bctbx_file_truncate(bctbx_vfs_file_t *pFile, int64_t size);
/**
* Write count bytes contained in buf to a file associated with pFile at the position
* offset. Calls pFuncWrite (set to bc_Write by default).
......
......@@ -155,6 +155,29 @@ static int64_t bcFileSize(bctbx_vfs_file_t *pFile) {
return sStat.st_size;
}
/*
** Truncate a file
* @param pFile File handle pointer.
* @param new_size Extends the file with null bytes if it is superiori to the file's size
* truncates the file otherwise.
* @return -errno if an error occurred, 0 otherwise.
*/
static int bcTruncate(bctbx_vfs_file_t *pFile, int64_t new_size){
int ret;
#if _WIN32
ret = _chsize(pFile->fd, (long)new_size);
#else
ret = ftruncate(pFile->fd, new_size);
#endif
if (ret < 0) {
return -errno;
}
return 0;
}
/**
* Gets a line of max_len length and stores it to the allocaed buffer s.
* Reads at most max_len characters from the file descriptor associated with the argument pFile
......@@ -234,6 +257,7 @@ static const bctbx_io_methods_t bcio = {
bcClose, /* pFuncClose */
bcRead, /* pFuncRead */
bcWrite, /* pFuncWrite */
bcTruncate, /* pFuncTruncate */
bcFileSize, /* pFuncFileSize */
bcGetLine,
bcSeek,
......@@ -346,6 +370,7 @@ int bctbx_file_close(bctbx_vfs_file_t *pFile) {
}
int64_t bctbx_file_size(bctbx_vfs_file_t *pFile) {
int64_t ret = BCTBX_VFS_ERROR;
if (pFile){
......@@ -356,6 +381,15 @@ int64_t bctbx_file_size(bctbx_vfs_file_t *pFile) {
}
int bctbx_file_truncate(bctbx_vfs_file_t *pFile, int64_t size) {
int ret = BCTBX_VFS_ERROR;
if (pFile){
ret = pFile->pMethods->pFuncTruncate(pFile, size);
if (ret < 0) bctbx_error("bctbx_file_truncate: Error truncate %s", strerror((int)-(ret)));
}
return ret;
}
ssize_t bctbx_file_fprintf(bctbx_vfs_file_t *pFile, off_t offset, const char *fmt, ...) {
char *ret = NULL;
va_list args;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment