minidlna support now Samsung TV C550/C650 (thx amir909)
[tomato.git] / release / src / router / ntfs-3g / libntfs-3g / unix_io.c
blob64b41d3e71feca90ff1bfa96173dcef4d03ca1ce
1 /**
2 * unix_io.c - Unix style disk io functions. Originated from the Linux-NTFS project.
4 * Copyright (c) 2000-2006 Anton Altaparmakov
6 * This program/include file is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as published
8 * by the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program/include file is distributed in the hope that it will be
12 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program (in the main directory of the NTFS-3G
18 * distribution in the file COPYING); if not, write to the Free Software
19 * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_STDLIB_H
30 #include <stdlib.h>
31 #endif
32 #ifdef HAVE_STRING_H
33 #include <string.h>
34 #endif
35 #ifdef HAVE_ERRNO_H
36 #include <errno.h>
37 #endif
38 #ifdef HAVE_STDIO_H
39 #include <stdio.h>
40 #endif
41 #ifdef HAVE_SYS_TYPES_H
42 #include <sys/types.h>
43 #endif
44 #ifdef HAVE_SYS_STAT_H
45 #include <sys/stat.h>
46 #endif
47 #ifdef HAVE_FCNTL_H
48 #include <fcntl.h>
49 #endif
50 #ifdef HAVE_SYS_IOCTL_H
51 #include <sys/ioctl.h>
52 #endif
53 #ifdef HAVE_LINUX_FD_H
54 #include <linux/fd.h>
55 #endif
57 #include "types.h"
58 #include "mst.h"
59 #include "debug.h"
60 #include "device.h"
61 #include "logging.h"
62 #include "misc.h"
64 #define DEV_FD(dev) (*(int *)dev->d_private)
66 /* Define to nothing if not present on this system. */
67 #ifndef O_EXCL
68 # define O_EXCL 0
69 #endif
71 /**
72 * fsync replacement which makes every effort to try to get the data down to
73 * disk, using different means for different operating systems. Specifically,
74 * it issues the proper fcntl for Mac OS X or does fsync where it is available
75 * or as a last resort calls the fsync function. Information on this problem
76 * was retrieved from:
77 * http://mirror.linux.org.au/pub/linux.conf.au/2007/video/talks/278.pdf
79 static int ntfs_fsync(int fildes)
81 int ret = -1;
82 #if defined(__APPLE__) || defined(__DARWIN__)
83 # ifndef F_FULLFSYNC
84 # 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)."
85 # endif
86 /*
87 * Apple has disabled fsync() for internal disk drives in OS X.
88 * To force a synchronization of disk contents, we use a Mac OS X
89 * specific fcntl, F_FULLFSYNC.
91 ret = fcntl(fildes, F_FULLFSYNC, NULL);
92 if (ret) {
93 /*
94 * If we are not on a file system that supports this,
95 * then fall back to a plain fsync.
97 ret = fsync(fildes);
99 #else
100 ret = fsync(fildes);
101 #endif
102 return ret;
106 * ntfs_device_unix_io_open - Open a device and lock it exclusively
107 * @dev:
108 * @flags:
110 * Description...
112 * Returns:
114 static int ntfs_device_unix_io_open(struct ntfs_device *dev, int flags)
116 struct flock flk;
117 struct stat sbuf;
118 int err;
120 if (NDevOpen(dev)) {
121 errno = EBUSY;
122 return -1;
124 if (stat(dev->d_name, &sbuf)) {
125 ntfs_log_perror("Failed to access '%s'", dev->d_name);
126 return -1;
128 if (S_ISBLK(sbuf.st_mode))
129 NDevSetBlock(dev);
131 dev->d_private = ntfs_malloc(sizeof(int));
132 if (!dev->d_private)
133 return -1;
135 * Open file for exclusive access if mounting r/w.
136 * Fuseblk takes care about block devices.
138 if (!NDevBlock(dev) && (flags & O_RDWR) == O_RDWR)
139 flags |= O_EXCL;
140 *(int*)dev->d_private = open(dev->d_name, flags);
141 if (*(int*)dev->d_private == -1) {
142 err = errno;
143 goto err_out;
146 if ((flags & O_RDWR) != O_RDWR)
147 NDevSetReadOnly(dev);
149 memset(&flk, 0, sizeof(flk));
150 if (NDevReadOnly(dev))
151 flk.l_type = F_RDLCK;
152 else
153 flk.l_type = F_WRLCK;
154 flk.l_whence = SEEK_SET;
155 flk.l_start = flk.l_len = 0LL;
156 if (fcntl(DEV_FD(dev), F_SETLK, &flk)) {
157 err = errno;
158 ntfs_log_perror("Failed to %s lock '%s'", NDevReadOnly(dev) ?
159 "read" : "write", dev->d_name);
160 if (close(DEV_FD(dev)))
161 ntfs_log_perror("Failed to close '%s'", dev->d_name);
162 goto err_out;
165 NDevSetOpen(dev);
166 return 0;
167 err_out:
168 free(dev->d_private);
169 dev->d_private = NULL;
170 errno = err;
171 return -1;
175 * ntfs_device_unix_io_close - Close the device, releasing the lock
176 * @dev:
178 * Description...
180 * Returns:
182 static int ntfs_device_unix_io_close(struct ntfs_device *dev)
184 struct flock flk;
186 if (!NDevOpen(dev)) {
187 errno = EBADF;
188 ntfs_log_perror("Device %s is not open", dev->d_name);
189 return -1;
191 if (NDevDirty(dev))
192 if (ntfs_fsync(DEV_FD(dev))) {
193 ntfs_log_perror("Failed to fsync device %s", dev->d_name);
194 return -1;
197 memset(&flk, 0, sizeof(flk));
198 flk.l_type = F_UNLCK;
199 flk.l_whence = SEEK_SET;
200 flk.l_start = flk.l_len = 0LL;
201 if (fcntl(DEV_FD(dev), F_SETLK, &flk))
202 ntfs_log_perror("Could not unlock %s", dev->d_name);
203 if (close(DEV_FD(dev))) {
204 ntfs_log_perror("Failed to close device %s", dev->d_name);
205 return -1;
207 NDevClearOpen(dev);
208 free(dev->d_private);
209 dev->d_private = NULL;
210 return 0;
214 * ntfs_device_unix_io_seek - Seek to a place on the device
215 * @dev:
216 * @offset:
217 * @whence:
219 * Description...
221 * Returns:
223 static s64 ntfs_device_unix_io_seek(struct ntfs_device *dev, s64 offset,
224 int whence)
226 return lseek(DEV_FD(dev), offset, whence);
230 * ntfs_device_unix_io_read - Read from the device, from the current location
231 * @dev:
232 * @buf:
233 * @count:
235 * Description...
237 * Returns:
239 static s64 ntfs_device_unix_io_read(struct ntfs_device *dev, void *buf,
240 s64 count)
242 return read(DEV_FD(dev), buf, count);
246 * ntfs_device_unix_io_write - Write to the device, at the current location
247 * @dev:
248 * @buf:
249 * @count:
251 * Description...
253 * Returns:
255 static s64 ntfs_device_unix_io_write(struct ntfs_device *dev, const void *buf,
256 s64 count)
258 if (NDevReadOnly(dev)) {
259 errno = EROFS;
260 return -1;
262 NDevSetDirty(dev);
263 return write(DEV_FD(dev), buf, count);
267 * ntfs_device_unix_io_pread - Perform a positioned read from the device
268 * @dev:
269 * @buf:
270 * @count:
271 * @offset:
273 * Description...
275 * Returns:
277 static s64 ntfs_device_unix_io_pread(struct ntfs_device *dev, void *buf,
278 s64 count, s64 offset)
280 return pread(DEV_FD(dev), buf, count, offset);
284 * ntfs_device_unix_io_pwrite - Perform a positioned write to the device
285 * @dev:
286 * @buf:
287 * @count:
288 * @offset:
290 * Description...
292 * Returns:
294 static s64 ntfs_device_unix_io_pwrite(struct ntfs_device *dev, const void *buf,
295 s64 count, s64 offset)
297 if (NDevReadOnly(dev)) {
298 errno = EROFS;
299 return -1;
301 NDevSetDirty(dev);
302 return pwrite(DEV_FD(dev), buf, count, offset);
306 * ntfs_device_unix_io_sync - Flush any buffered changes to the device
307 * @dev:
309 * Description...
311 * Returns:
313 static int ntfs_device_unix_io_sync(struct ntfs_device *dev)
315 int res = 0;
317 if (!NDevReadOnly(dev)) {
318 res = ntfs_fsync(DEV_FD(dev));
319 if (res)
320 ntfs_log_perror("Failed to sync device %s", dev->d_name);
321 else
322 NDevClearDirty(dev);
324 return res;
328 * ntfs_device_unix_io_stat - Get information about the device
329 * @dev:
330 * @buf:
332 * Description...
334 * Returns:
336 static int ntfs_device_unix_io_stat(struct ntfs_device *dev, struct stat *buf)
338 return fstat(DEV_FD(dev), buf);
342 * ntfs_device_unix_io_ioctl - Perform an ioctl on the device
343 * @dev:
344 * @request:
345 * @argp:
347 * Description...
349 * Returns:
351 static int ntfs_device_unix_io_ioctl(struct ntfs_device *dev, int request,
352 void *argp)
354 return ioctl(DEV_FD(dev), request, argp);
358 * Device operations for working with unix style devices and files.
360 struct ntfs_device_operations ntfs_device_unix_io_ops = {
361 .open = ntfs_device_unix_io_open,
362 .close = ntfs_device_unix_io_close,
363 .seek = ntfs_device_unix_io_seek,
364 .read = ntfs_device_unix_io_read,
365 .write = ntfs_device_unix_io_write,
366 .pread = ntfs_device_unix_io_pread,
367 .pwrite = ntfs_device_unix_io_pwrite,
368 .sync = ntfs_device_unix_io_sync,
369 .stat = ntfs_device_unix_io_stat,
370 .ioctl = ntfs_device_unix_io_ioctl,