]> git.tuebingen.mpg.de Git - osl.git/blobdiff - fd.c
Rename para_truncate() to truncate_file().
[osl.git] / fd.c
diff --git a/fd.c b/fd.c
index b889d9d2144a4ad78cf6442997d80fec643aad87..1519ac5552dbf106d7edbec5faf2776066c7e0f1 100644 (file)
--- a/fd.c
+++ b/fd.c
@@ -322,3 +322,34 @@ __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 = -E_OSL_STAT;
+       if (stat(path, &statbuf) < 0)
+               goto out;
+       ret = -E_OSL_BAD_SIZE;
+       if (statbuf.st_size < size)
+               goto out;
+       ret = -E_OSL_TRUNC;
+       if (truncate(path, statbuf.st_size - size) < 0)
+               goto out;
+       ret = 1;
+out:
+       return ret;
+}
+