nxffs_truncate.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /****************************************************************************
  2. * fs/nxffs/nxffs_truncate.c
  3. *
  4. * Copyright (C) 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * References: Linux/Documentation/filesystems/romfs.txt
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. * 3. Neither the name NuttX nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software
  21. * without specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  24. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  25. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  26. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  28. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  29. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  30. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  31. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  32. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  33. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. *
  36. ****************************************************************************/
  37. /****************************************************************************
  38. * Included Files
  39. ****************************************************************************/
  40. #include <nuttx/config.h>
  41. #include <fcntl.h>
  42. #include <assert.h>
  43. #include <errno.h>
  44. #include <debug.h>
  45. #include <nuttx/semaphore.h>
  46. #include "nxffs.h"
  47. #ifdef __NO_TRUNCATE_SUPPORT__
  48. /****************************************************************************
  49. * Public Functions
  50. ****************************************************************************/
  51. /****************************************************************************
  52. * Name: nxffs_truncate
  53. *
  54. * Description:
  55. * Set the length of the open, regular file associated with the file
  56. * structure 'filep' to 'length'.
  57. *
  58. ****************************************************************************/
  59. int nxffs_truncate(FAR struct file *filep, off_t length)
  60. {
  61. FAR struct nxffs_volume_s *volume;
  62. FAR struct nxffs_wrfile_s *wrfile;
  63. off_t oldsize;
  64. int ret;
  65. finfo("Write %d bytes to offset %d\n", buflen, filep->f_pos);
  66. /* Sanity checks */
  67. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  68. /* Recover the open file state from the struct file instance */
  69. wrfile = (FAR struct nxffs_wrfile_s *)filep->f_priv;
  70. /* Recover the volume state from the open file */
  71. volume = (FAR struct nxffs_volume_s *)filep->f_inode->i_private;
  72. DEBUGASSERT(volume != NULL);
  73. /* Get exclusive access to the volume. Note that the volume exclsem
  74. * protects the open file list.
  75. */
  76. ret = nxsem_wait(&volume->exclsem);
  77. if (ret < 0)
  78. {
  79. ferr("ERROR: nxsem_wait failed: %d\n", ret);
  80. goto errout;
  81. }
  82. /* Check if the file was opened with write access */
  83. if ((wrfile->ofile.oflags & O_WROK) == 0)
  84. {
  85. ferr("ERROR: File not open for write access\n");
  86. ret = -EACCES;
  87. goto errout_with_semaphore;
  88. }
  89. /* Are we shrinking the file? Or extending it? */
  90. oldsize = wrfile->ofile.entry.datlen;
  91. if (oldsize == length)
  92. {
  93. ret = OK;
  94. goto errout_with_semaphore;
  95. }
  96. else if (oldsize > length)
  97. {
  98. /* We are shrinking the file */
  99. /* REVISIT: Logic to shrink the file has not yet been implemented */
  100. ret = -ENOSYS;
  101. }
  102. else
  103. {
  104. /* We are zero-extending the file. This essential amount to a write-
  105. * append operation with zero data.
  106. */
  107. ret = nxffs_wrextend(volume, wrfile, length);
  108. }
  109. errout_with_semaphore:
  110. nxsem_post(&volume->exclsem);
  111. errout:
  112. return ret;
  113. }
  114. #endif /* __NO_TRUNCATE_SUPPORT__ */