#include "lint.h"
#include <fcntl.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
static int
fallocate_errno(int fd)
{
struct stat64 statb;
int error;
if (fstat64(fd, &statb) != 0)
error = EBADF;
else if (S_ISFIFO(statb.st_mode))
error = ESPIPE;
else if (!S_ISREG(statb.st_mode))
error = ENODEV;
else
error = EINVAL;
return (error);
}
int
posix_fallocate(int fd, off_t offset, off_t len)
{
struct flock lck;
int error;
if (offset < 0 || len <= 0)
return (EINVAL);
lck.l_whence = 0;
lck.l_start = offset;
lck.l_len = len;
lck.l_type = F_WRLCK;
if (fcntl(fd, F_ALLOCSP, &lck) == -1) {
if ((error = errno) == EINVAL)
error = fallocate_errno(fd);
else if (error == EOVERFLOW)
error = EFBIG;
return (error);
}
return (0);
}
#if !defined(_LP64)
int
posix_fallocate64(int fd, off64_t offset, off64_t len)
{
struct flock64 lck;
int error;
if (offset < 0 || len <= 0)
return (EINVAL);
lck.l_whence = 0;
lck.l_start = offset;
lck.l_len = len;
lck.l_type = F_WRLCK;
if (fcntl(fd, F_ALLOCSP64, &lck) == -1) {
if ((error = errno) == EINVAL)
error = fallocate_errno(fd);
else if (error == EOVERFLOW)
error = EFBIG;
return (error);
}
return (0);
}
#endif