You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by je...@apache.org on 2020/12/14 08:23:00 UTC

[incubator-nuttx] branch master updated: libc: Implement posix_fallocate

This is an automated email from the ASF dual-hosted git repository.

jerpelea pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-nuttx.git


The following commit(s) were added to refs/heads/master by this push:
     new 98be50a  libc: Implement posix_fallocate
98be50a is described below

commit 98be50a9bc1dd8077428431960c6574c9e95075a
Author: Xiang Xiao <xi...@xiaomi.com>
AuthorDate: Mon Dec 14 10:56:23 2020 +0800

    libc: Implement posix_fallocate
    
    as specified here:
    https://pubs.opengroup.org/onlinepubs/007904875/functions/posix_fallocate.html
    
    Signed-off-by: Xiang Xiao <xi...@xiaomi.com>
---
 include/fcntl.h                        |  2 +
 libs/libc/unistd/Make.defs             |  2 +-
 libs/libc/unistd/lib_posix_fallocate.c | 96 ++++++++++++++++++++++++++++++++++
 3 files changed, 99 insertions(+), 1 deletion(-)

diff --git a/include/fcntl.h b/include/fcntl.h
index a12071c..9d7c97b 100644
--- a/include/fcntl.h
+++ b/include/fcntl.h
@@ -174,6 +174,8 @@ extern "C"
 int open(FAR const char *path, int oflag, ...);
 int fcntl(int fd, int cmd, ...);
 
+int posix_fallocate(int fd, off_t offset, off_t len);
+
 #undef EXTERN
 #if defined(__cplusplus)
 }
diff --git a/libs/libc/unistd/Make.defs b/libs/libc/unistd/Make.defs
index 3955fe8..cf481b2 100644
--- a/libs/libc/unistd/Make.defs
+++ b/libs/libc/unistd/Make.defs
@@ -59,7 +59,7 @@ CSRCS += lib_execl.c
 endif
 
 ifneq ($(CONFIG_DISABLE_MOUNTPOINTS),y)
-CSRCS += lib_truncate.c
+CSRCS += lib_truncate.c lib_posix_fallocate.c
 endif
 
 ifeq ($(CONFIG_PIPES),y)
diff --git a/libs/libc/unistd/lib_posix_fallocate.c b/libs/libc/unistd/lib_posix_fallocate.c
new file mode 100644
index 0000000..d432f49
--- /dev/null
+++ b/libs/libc/unistd/lib_posix_fallocate.c
@@ -0,0 +1,96 @@
+/****************************************************************************
+ * libs/libc/unistd/lib_posix_fallocate.c
+ *
+ * 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
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+#include <sys/stat.h>
+
+#ifndef CONFIG_DISABLE_MOUNTPOINT
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: posix_fallocate
+ *
+ * Description:
+ *  The posix_fallocate() function shall ensure that any required storage for
+ *  regular file data starting at offset and continuing for len bytes is
+ *  allocated on the file system storage media. If posix_fallocate() returns
+ *  successfully, subsequent writes to the specified file data shall not fail
+ *  due to the lack of free space on the file system storage media.
+ *
+ *  If the offset+len is beyond the current file size, then posix_fallocate()
+ *  shall adjust the file size to offset+len. Otherwise, the file size shall
+ *  not be changed.
+ *
+ *  It is implementation-defined whether a previous posix_fadvise() call
+ *  influences allocation strategy.
+ *
+ *  Space allocated via posix_fallocate() shall be freed by a successful call
+ *  to creat() or open() that truncates the size of the file. Space allocated
+ *  via posix_fallocate() may be freed by a successful call to ftruncate()
+ *  that reduces the file size to a size smaller than offset+ len.
+ *
+ * Returned Value:
+ *   Upon successful completion, posix_fallocate() shall return zero;
+ *   otherwise, an error number shall be returned to indicate the error.
+ *
+ ****************************************************************************/
+
+int posix_fallocate(int fd, off_t offset, off_t len)
+{
+  struct stat st;
+
+  if (offset < 0 || len < 0)
+    {
+      return EINVAL;
+    }
+
+  len += offset;
+  if (len < 0)
+    {
+      return EFBIG;
+    }
+
+  if (fstat(fd, &st) != 0)
+    {
+      return errno;
+    }
+
+  if (st.st_size < len)
+    {
+      if (ftruncate(fd, len) != 0)
+        {
+          return errno;
+        }
+    }
+
+  return 0;
+}
+
+#endif /* !CONFIG_DISABLE_MOUNTPOINT */