summaryrefslogtreecommitdiffstats
path: root/src/ntfs_io.c
blob: 7f57edd0445f384e3754b8cdb68c74493113b3c8 (plain)
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
/*
 * ntfs_io.c - Unix style disk io functions. Part of the TestDisk project.
 *
 * Original version comes from the Linux-NTFS project.
 * Copyright (c) 2000-2003 Anton Altaparmakov 
 * Copyright (c) 2004-2006 Christophe Grenier
 *
 * This program/include file 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/include file 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 (in the main directory of the Linux-NTFS
 * distribution in the file COPYING); if not, write to the Free Software
 * Foundation,Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */


#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
 
#if defined(HAVE_LIBNTFS) || defined(HAVE_LIBNTFS3G)
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#include <errno.h>
#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
#include <stdarg.h>
#ifdef HAVE_LIBNTFS
#include <ntfs/device.h>
#endif
#ifdef HAVE_LIBNTFS3G
#include <ntfs-3g/types.h>
#include <ntfs-3g/device.h>
#endif
#include <stdio.h>
#include "types.h"
#include "common.h"
#include "log.h"

#if defined(linux) && defined(_IO) && !defined(BLKGETSIZE)
#	define BLKGETSIZE _IO(0x12,96) /* Get device size in 512byte blocks. */
#endif

static int ntfs_device_testdisk_io_open(struct ntfs_device *dev, int flags)
{
	if (NDevOpen(dev)) {
		errno = EBUSY;
		return -1;
	}
	/* Setup our read-only flag. */
	if ((flags & O_RDWR) != O_RDWR)
		NDevSetReadOnly(dev);
	/* Set our open flag. */
	NDevSetOpen(dev);
	return 0;
}

static int ntfs_device_testdisk_io_close(struct ntfs_device *dev)
{
	if (!NDevOpen(dev)) {
		errno = EBADF;
		return -1;
	}
	NDevClearOpen(dev);
	return 0;
}

static s64 ntfs_device_testdisk_io_seek(struct ntfs_device *dev, s64 offset,
		int whence)
{
  my_data_t *my_data=(my_data_t*)dev->d_private;
  switch(whence)
  {
    case SEEK_SET:
      my_data->offset=offset;
      break;
    case SEEK_CUR:
      my_data->offset+=offset;
      break;
    case SEEK_END:
      my_data->offset=my_data->partition->part_size+offset;
      break;
  }
  return my_data->offset;
}

static s64 ntfs_device_testdisk_io_read(struct ntfs_device *dev, void *buf,
		s64 count)
{
  my_data_t *my_data=(my_data_t*)dev->d_private;
  if(my_data->disk_car->pread(my_data->disk_car, buf, count, my_data->partition->part_offset + my_data->offset) != count)
    return 0;
  my_data->offset+=count;
  return count;
}

static s64 ntfs_device_testdisk_io_write(struct ntfs_device *dev, const void *buf,
		s64 count)
{
  my_data_t *my_data=(my_data_t*)dev->d_private;
  if(my_data->disk_car->pwrite(my_data->disk_car, buf, count, my_data->partition->part_offset + my_data->offset) != count)
    return 0;
  my_data->offset+=count;
  return count;
}

static s64 ntfs_device_testdisk_io_pread(struct ntfs_device *dev, void *buf,
    s64 count, s64 offset)
{
  my_data_t *my_data=(my_data_t*)dev->d_private;
  return my_data->disk_car->pread(my_data->disk_car, buf, count,
      my_data->partition->part_offset + offset);
}

static s64 ntfs_device_testdisk_io_pwrite(struct ntfs_device *dev, const void *buf,
                s64 count, s64 offset)
{
  my_data_t *my_data=(my_data_t*)dev->d_private;
  return my_data->disk_car->pwrite(my_data->disk_car, buf, count,
      my_data->partition->part_offset + offset);
}

static int ntfs_device_testdisk_io_sync(struct ntfs_device *dev)
{
  my_data_t *my_data=(my_data_t*)dev->d_private;
  return my_data->disk_car->sync(my_data->disk_car);
}

static int ntfs_device_testdisk_io_stat(struct ntfs_device *dev, struct stat *buf)
{
	log_warning("ntfs_device_testdisk_io_stat() unimplemented\n");
#ifdef ENOTSUP
	errno = ENOTSUP;
#endif
	return -1;
}

static int ntfs_device_testdisk_io_ioctl(struct ntfs_device *dev, int request,
		void *argp)
{
	log_warning( "ntfs_device_testdisk_io_ioctl() unimplemented\n");
#ifdef ENOTSUP
	errno = ENOTSUP;
#endif
	return -1;
}

/**
 * Device operations for working with unix style devices and files.
 */
struct ntfs_device_operations ntfs_device_testdisk_io_ops = {
	.open		= &ntfs_device_testdisk_io_open,
	.close		= &ntfs_device_testdisk_io_close,
	.seek		= &ntfs_device_testdisk_io_seek,
	.read		= &ntfs_device_testdisk_io_read,
	.write		= &ntfs_device_testdisk_io_write,
	.pread		= &ntfs_device_testdisk_io_pread,
	.pwrite		= &ntfs_device_testdisk_io_pwrite,
	.sync		= &ntfs_device_testdisk_io_sync,
	.stat		= &ntfs_device_testdisk_io_stat,
	.ioctl		= &ntfs_device_testdisk_io_ioctl,
};
#endif