2018-01-03 23:03:56 +01:00
|
|
|
/****************************************************************************
|
|
|
|
* fs/vfs/fs_truncate.c
|
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2018-01-03 23:03:56 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2018-01-03 23:03:56 +01:00
|
|
|
*
|
2021-02-03 14:47:23 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2018-01-03 23:03:56 +01:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <assert.h>
|
2021-05-14 04:45:57 +02:00
|
|
|
#include <debug.h>
|
2018-01-03 23:03:56 +01:00
|
|
|
|
|
|
|
#include <nuttx/fs/fs.h>
|
|
|
|
|
|
|
|
#include "inode/inode.h"
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: file_truncate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* Equivalent to the standard ftruncate() function except that is accepts
|
|
|
|
* a struct file instance instead of a file descriptor and it does not set
|
|
|
|
* the errno variable.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int file_truncate(FAR struct file *filep, off_t length)
|
|
|
|
{
|
|
|
|
struct inode *inode;
|
|
|
|
|
|
|
|
/* Was this file opened for write access? */
|
|
|
|
|
|
|
|
if ((filep->f_oflags & O_WROK) == 0)
|
|
|
|
{
|
|
|
|
fwarn("WARNING: Cannot truncate a file opened read-only\n");
|
2023-06-09 15:13:21 +02:00
|
|
|
return -EINVAL;
|
2018-01-03 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Is this inode a registered mountpoint? Does it support the
|
|
|
|
* truncate operations may be relevant to device drivers but only
|
|
|
|
* the mountpoint operations vtable contains a truncate method.
|
|
|
|
*/
|
|
|
|
|
|
|
|
inode = filep->f_inode;
|
2022-12-22 11:51:59 +01:00
|
|
|
if (inode == NULL)
|
2018-01-03 23:03:56 +01:00
|
|
|
{
|
2022-12-22 11:51:59 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (inode->u.i_ops == NULL)
|
|
|
|
{
|
|
|
|
fwarn("WARNING: Not a file\n");
|
2018-01-03 23:03:56 +01:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* A NULL write() method is an indicator of a read-only file system (but
|
2018-01-04 17:54:54 +01:00
|
|
|
* possible not the only indicator -- sufficient, but not necessary")
|
2018-01-03 23:03:56 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-22 11:51:59 +01:00
|
|
|
if (inode->u.i_ops->write == NULL)
|
2018-01-03 23:03:56 +01:00
|
|
|
{
|
|
|
|
fwarn("WARNING: File system is read-only\n");
|
|
|
|
return -EROFS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Does the file system support the truncate method? It should if it is
|
|
|
|
* a write-able file system.
|
|
|
|
*/
|
|
|
|
|
2022-12-22 11:51:59 +01:00
|
|
|
if (inode->u.i_ops->truncate == NULL)
|
2018-01-03 23:03:56 +01:00
|
|
|
{
|
|
|
|
fwarn("WARNING: File system does not support the truncate() method\n");
|
|
|
|
return -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Yes, then tell the file system to truncate this file */
|
|
|
|
|
2022-12-22 11:51:59 +01:00
|
|
|
return inode->u.i_ops->truncate(filep, length);
|
2018-01-03 23:03:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: ftruncate
|
|
|
|
*
|
|
|
|
* Description:
|
|
|
|
* The ftruncate() function causes the regular file referenced by fd to
|
|
|
|
* have a size of length bytes.
|
|
|
|
*
|
|
|
|
* If the file previously was larger than length, the extra data is
|
|
|
|
* discarded. If it was previously shorter than length, it is unspecified
|
|
|
|
* whether the file is changed or its size increased. If the file is
|
|
|
|
* extended, the extended area appears as if it were zero-filled. If fd
|
|
|
|
* references a shared memory object, ftruncate() sets the size of the
|
|
|
|
* shared memory object to length. If the file is not a regular file or
|
|
|
|
* a shared memory object, the result is unspecified.
|
|
|
|
|
|
|
|
* With ftruncate(), the file must be open for writing; for truncate(),
|
|
|
|
* the process must have write permission for the file.
|
|
|
|
*
|
|
|
|
* ftruncate() does not modify the file offset for any open file
|
|
|
|
* descriptions associated with the file.
|
|
|
|
*
|
|
|
|
* Input Parameters:
|
|
|
|
* fd - A reference to an open, regular file or shared memory object
|
|
|
|
* to be truncated.
|
|
|
|
* length - The new length of the file or shared memory object.
|
|
|
|
*
|
|
|
|
* Returned Value:
|
|
|
|
* Upon successful completion, ftruncate() return 0s. Otherwise a -1 is
|
|
|
|
* returned, and errno is set to indicate the error.
|
|
|
|
*
|
|
|
|
* EINTR
|
|
|
|
* - A signal was caught during execution.
|
|
|
|
* EINVAL
|
|
|
|
* - The length argument was less than 0.
|
|
|
|
* EFBIG or EINVAL
|
|
|
|
* - The length argument was greater than the maximum file size.
|
|
|
|
* EIO
|
|
|
|
* - An I/O error occurred while reading from or writing to a file
|
|
|
|
* system.
|
|
|
|
* EBADF or EINVAL
|
|
|
|
* - the fd argument is not a file descriptor open for writing.
|
|
|
|
* EFBIG
|
|
|
|
* - The file is a regular file and length is greater than the offset
|
|
|
|
* maximum established in the open file description associated with
|
|
|
|
* fd.
|
|
|
|
* EINVAL
|
|
|
|
* - The fd argument references a file that was opened without write
|
|
|
|
* permission.
|
|
|
|
* EROFS
|
|
|
|
* - The named file resides on a read-only file system.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int ftruncate(int fd, off_t length)
|
|
|
|
{
|
|
|
|
FAR struct file *filep;
|
2021-10-31 05:29:59 +01:00
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
if (length < 0)
|
|
|
|
{
|
|
|
|
goto errout;
|
|
|
|
}
|
2018-01-03 23:03:56 +01:00
|
|
|
|
|
|
|
/* Get the file structure corresponding to the file descriptor. */
|
|
|
|
|
|
|
|
ret = fs_getfilep(fd, &filep);
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
ferr("ERROR: Could no get file structure: %d\n", ret);
|
|
|
|
goto errout;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Perform the truncate operation */
|
|
|
|
|
|
|
|
ret = file_truncate(filep, length);
|
2023-12-25 08:04:01 +01:00
|
|
|
fs_putfilep(filep);
|
2018-01-03 23:03:56 +01:00
|
|
|
if (ret >= 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
fwarn("WARNING: file_truncate() failed: %d\n", ret);
|
|
|
|
|
|
|
|
errout:
|
|
|
|
set_errno(-ret);
|
|
|
|
return ERROR;
|
|
|
|
}
|