aboutsummaryrefslogtreecommitdiff
path: root/offset.c
blob: e8be549a526f4085f829b56994ce7e46cada78e0 (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
/*  Copyright 2021 Alain Knaff.
 *  This file is part of mtools.
 *
 *  Mtools 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 3 of the License, or
 *  (at your option) any later version.
 *
 *  Mtools 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 Mtools.  If not, see <http://www.gnu.org/licenses/>.
 *
 * filter to support filesystems stored at an offset into their image
 */

#include "sysincludes.h"
#include "msdos.h"
#include "mtools.h"
#include "offset.h"

typedef struct Offset_t {
	struct Stream_t head;

	mt_off_t offset;
} Offset_t;

static ssize_t offset_pread(Stream_t *Stream, char *buf,
			    mt_off_t start, size_t len)
{
	DeclareThis(Offset_t);
	return PREADS(This->head.Next, buf, start+This->offset, len);
}

static ssize_t offset_pwrite(Stream_t *Stream, char *buf,
			     mt_off_t start, size_t len)
{
	DeclareThis(Offset_t);
	return PWRITES(This->head.Next, buf, start+This->offset, len);
}

static Class_t OffsetClass = {
	0,
	0,
	offset_pread,
	offset_pwrite,
	0, /* flush */
	0, /* free */
	set_geom_pass_through, /* set_geom */
	0, /* get_data */
	0, /* pre-allocate */
	get_dosConvert_pass_through, /* dos convert */
	0, /* discard */
};

Stream_t *OpenOffset(Stream_t *Next, struct device *dev, off_t offset,
		     char *errmsg, mt_off_t *maxSize) {
	Offset_t *This;

	This = New(Offset_t);
	if (!This){
		printOom();
		return 0;
	}
	memset((void*)This, 0, sizeof(Offset_t));
	init_head(&This->head, &OffsetClass, Next);

	This->offset = offset;

	if(maxSize) {
		if(This->offset > *maxSize) {
			if(errmsg)
				sprintf(errmsg,"init: Big disks not supported");
			goto exit_0;
		}

		*maxSize -= This->offset;
	}

	if(adjust_tot_sectors(dev, This->offset, errmsg) < 0)
		goto exit_0;

	return &This->head;
 exit_0:
	Free(This);
	return NULL;
}