#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_LINUX_FD_H
#include <linux/fd.h>
#endif
#ifdef HAVE_LINUX_FS_H
#include <linux/fs.h>
#endif
#include "types.h"
#include "mst.h"
#include "debug.h"
#include "device.h"
#include "logging.h"
#include "misc.h"
#define DEV_FD(dev) (*(int *)dev->d_private)
#ifndef O_EXCL
# define O_EXCL 0
#endif
static int ntfs_fsync(int fildes)
{
int ret = -1;
#if defined(__APPLE__) || defined(__DARWIN__)
# ifndef F_FULLFSYNC
# error "Mac OS X: F_FULLFSYNC is not defined. Either you didn't include fcntl.h or you're using an older, unsupported version of Mac OS X (pre-10.3)."
# endif
ret = fcntl(fildes, F_FULLFSYNC, NULL);
if (ret) {
ret = fsync(fildes);
}
#else
ret = fsync(fildes);
#endif
return ret;
}
static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags)
{
struct flock flk;
struct stat sbuf;
int err;
if (NDevOpen(dev)) {
errno = EBUSY;
return -1;
}
if (stat(dev->d_name, &sbuf)) {
ntfs_log_perror("Failed to access '%s'", dev->d_name);
return -1;
}
if (S_ISBLK(sbuf.st_mode))
NDevSetBlock(dev);
dev->d_private = ntfs_malloc(sizeof(int));
if (!dev->d_private)
return -1;
if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR)
flags |= O_EXCL;
*(int*)dev->d_private = open(dev->d_name, flags);
if (*(int*)dev->d_private == -1) {
err = errno;
if ((err == EACCES) && ((flags & O_RDWR) == O_RDWR))
err = EROFS;
goto err_out;
}
#ifdef HAVE_LINUX_FS_H
if (NDevBlock(dev) && ((flags & O_RDWR) == O_RDWR)) {
int r;
int state;
r = ioctl(DEV_FD(dev), BLKROGET, &state);
if (!r && state) {
err = EROFS;
if (close(DEV_FD(dev)))
err = errno;
goto err_out;
}
}
#endif
if ((flags & O_RDWR) != O_RDWR)
NDevSetReadOnly(dev);
memset(&flk, 0, sizeof(flk));
if (NDevReadOnly(dev))
flk.l_type = F_RDLCK;
else
flk.l_type = F_WRLCK;
flk.l_whence = SEEK_SET;
flk.l_start = flk.l_len = 0LL;
if (fcntl(DEV_FD(dev), F_SETLK, &flk)) {
err = errno;
ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ?
"read" : "write", dev->d_name);
if (close(DEV_FD(dev)))
ntfs_log_perror("Failed to close '%s'", dev->d_name);
goto err_out;
}
NDevSetOpen(dev);
return 0;
err_out:
free(dev->d_private);
dev->d_private = NULL;
errno = err;
return -1;
}
static int ntfs_device_unix_io_close(struct ntfs_device *dev)
{
struct flock flk;
if (!NDevOpen(dev)) {
errno = EBADF;
ntfs_log_perror("Device %s is not open", dev->d_name);
return -1;
}
if (NDevDirty(dev))
if (ntfs_fsync(DEV_FD(dev))) {
ntfs_log_perror("Failed to fsync device %s", dev->d_name);
return -1;
}
memset(&flk, 0, sizeof(flk));
flk.l_type = F_UNLCK;
flk.l_whence = SEEK_SET;
flk.l_start = flk.l_len = 0LL;
if (fcntl(DEV_FD(dev), F_SETLK, &flk))
ntfs_log_perror("Could not unlock %s", dev->d_name);
if (close(DEV_FD(dev))) {
ntfs_log_perror("Failed to close device %s", dev->d_name);
return -1;
}
NDevClearOpen(dev);
free(dev->d_private);
dev->d_private = NULL;
return 0;
}
static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset,
int whence)
{
return lseek(DEV_FD(dev), offset, whence);
}
static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf,
s64 count)
{
return read(DEV_FD(dev), buf, count);
}
static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf,
s64 count)
{
if (NDevReadOnly(dev)) {
errno = EROFS;
return -1;
}
NDevSetDirty(dev);
return write(DEV_FD(dev), buf, count);
}
static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf,
s64 count, s64 offset)
{
return pread(DEV_FD(dev), buf, count, offset);
}
static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf,
s64 count, s64 offset)
{
if (NDevReadOnly(dev)) {
errno = EROFS;
return -1;
}
NDevSetDirty(dev);
return pwrite(DEV_FD(dev), buf, count, offset);
}
static int ntfs_device_unix_io_sync(struct ntfs_device *dev)
{
int res = 0;
if (!NDevReadOnly(dev)) {
res = ntfs_fsync(DEV_FD(dev));
if (res)
ntfs_log_perror("Failed to sync device %s", dev->d_name);
else
NDevClearDirty(dev);
}
return res;
}
static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf)
{
return fstat(DEV_FD(dev), buf);
}
static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev,
unsigned long request, void *argp)
{
return ioctl(DEV_FD(dev), request, argp);
}
struct ntfs_device_operations ntfs_device_unix_io_ops = {
.open = ntfs_device_unix_io_open,
.close = ntfs_device_unix_io_close,
.seek = ntfs_device_unix_io_seek,
.read = ntfs_device_unix_io_read,
.write = ntfs_device_unix_io_write,
.pread = ntfs_device_unix_io_pread,
.pwrite = ntfs_device_unix_io_pwrite,
.sync = ntfs_device_unix_io_sync,
.stat = ntfs_device_unix_io_stat,
.control = ntfs_device_unix_io_ioctl,
};