1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* libhfs - library for reading and writing Macintosh HFS volumes
* Copyright (C) 1996, 1997 Robert Leslie
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: unix.c,v 1.13 1997/11/05 05:31:25 rob Exp $
*/
# ifdef HAVE_CONFIG_H
# include "config.h"
# endif
# ifdef HAVE_FCNTL_H
# include <fcntl.h>
# else
int open(const char *, int, ...);
int fcntl(int, int, ...);
# endif
# ifdef HAVE_UNISTD_H
# include <unistd.h>
# else
int close(int);
off_t lseek(int, off_t, int);
ssize_t read(int, void *, size_t);
ssize_t write(int, const char *, size_t);
int stat(const char *, struct stat *);
int fstat(int, struct stat *);
# endif
# include <errno.h>
# include <sys/stat.h>
# include "libhfs.h"
# include "os.h"
/*
* NAME: os->open()
* DESCRIPTION: open and lock a new descriptor from the given path and mode
*/
int os_open(void **priv, const char *path, int mode)
{
int fd;
struct flock lock;
switch (mode)
{
case HFS_MODE_RDONLY:
mode = O_RDONLY;
break;
case HFS_MODE_RDWR:
default:
mode = O_RDWR;
break;
}
fd = open(path, mode);
if (fd == -1)
ERROR(errno, "error opening medium");
/* lock descriptor against concurrent access */
lock.l_type = (mode == O_RDONLY) ? F_RDLCK : F_WRLCK;
lock.l_start = 0;
lock.l_whence = SEEK_SET;
lock.l_len = 0;
if (fcntl(fd, F_SETLK, &lock) == -1 &&
(errno == EACCES || errno == EAGAIN))
ERROR(EAGAIN, "unable to obtain lock for medium");
*priv = (void *) fd;
return 0;
fail:
if (fd != -1)
close(fd);
return -1;
}
/*
* NAME: os->close()
* DESCRIPTION: close an open descriptor
*/
int os_close(void **priv)
{
int fd = (int) *priv;
*priv = (void *) -1;
if (close(fd) == -1)
ERROR(errno, "error closing medium");
return 0;
fail:
return -1;
}
/*
* NAME: os->same()
* DESCRIPTION: return 1 iff path is same as the open descriptor
*/
int os_same(void **priv, const char *path)
{
int fd = (int) *priv;
struct stat fdev, dev;
if (fstat(fd, &fdev) == -1 ||
stat(path, &dev) == -1)
ERROR(errno, "can't get path information");
return fdev.st_dev == dev.st_dev &&
fdev.st_ino == dev.st_ino;
fail:
return -1;
}
/*
* NAME: os->seek()
* DESCRIPTION: set a descriptor's seek pointer
*/
long os_seek(void **priv, long offset, int from)
{
long result;
switch (from)
{
case HFS_SEEK_CUR:
from = SEEK_CUR;
break;
case HFS_SEEK_END:
from = SEEK_END;
break;
case HFS_SEEK_SET:
default:
from = SEEK_SET;
break;
}
result = lseek((int) *priv, offset, from);
if (result == -1)
ERROR(errno, "error seeking medium");
return result;
fail:
return -1;
}
/*
* NAME: os->read()
* DESCRIPTION: read bytes from an open descriptor
*/
long os_read(void **priv, void *buf, unsigned long len)
{
long result;
result = read((int) *priv, buf, len);
if (result == -1)
ERROR(errno, "error reading from medium");
return result;
fail:
return -1;
}
/*
* NAME: os->write()
* DESCRIPTION: write bytes to an open descriptor
*/
long os_write(void **priv, const void *buf, unsigned long len)
{
long result;
result = write((int) *priv, buf, len);
if (result == -1)
ERROR(errno, "error writing to medium");
return result;
fail:
return -1;
}
|