X-Git-Url: http://git.tuebingen.mpg.de/?a=blobdiff_plain;f=fd.c;h=7d69a2f35b6fb3fe08ce487b6c02d3ebe557c9f2;hb=dfd3368c03d72660b1e5dba55b23395e234d0963;hp=b889d9d2144a4ad78cf6442997d80fec643aad87;hpb=da40398fa79dc25801e7d276fd86da7439bd3e41;p=osl.git diff --git a/fd.c b/fd.c index b889d9d..7d69a2f 100644 --- 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; +}