]> git.tuebingen.mpg.de Git - osl.git/blobdiff - fd.c
fsck.c depends on errtab.h.
[osl.git] / fd.c
diff --git a/fd.c b/fd.c
index b889d9d2144a4ad78cf6442997d80fec643aad87..7d69a2f35b6fb3fe08ce487b6c02d3ebe557c9f2 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -15,6 +15,7 @@
 #include "log.h"
 #include "osl.h"
 #include "error.h"
+#include "fd.h"
 
 /**
  * Wrapper for the write system call.
@@ -322,3 +323,30 @@ __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
        return p;
 }
 
+/**
+ * A wrapper for truncate(2)
+ *
+ * \param path Name of the regular file to truncate
+ * \param size Number of bytes to \b shave \b off
+ *
+ * Truncate the regular file named by \a path by \a size bytes.
+ *
+ * \return Standard.
+ *
+ * \sa truncate(2)
+ */
+int truncate_file(const char *path, off_t size)
+{
+       int ret;
+       struct stat statbuf;
+
+       ret = osl_stat(path, &statbuf);
+       if (ret < 0)
+               return ret;
+       ret = -E_OSL_BAD_SIZE;
+       if (statbuf.st_size < size)
+               return ret;
+       if (truncate(path, statbuf.st_size - size) < 0)
+               return -ERRNO_TO_ERROR(errno);
+       return 1;
+}