xref: /aosp_15_r20/external/mtools/offset.c (revision d5c9a868b113e0ec0db2f27bc2ce8a253e77c4b0)
1*d5c9a868SElliott Hughes /*  Copyright 2021 Alain Knaff.
2*d5c9a868SElliott Hughes  *  This file is part of mtools.
3*d5c9a868SElliott Hughes  *
4*d5c9a868SElliott Hughes  *  Mtools is free software: you can redistribute it and/or modify
5*d5c9a868SElliott Hughes  *  it under the terms of the GNU General Public License as published by
6*d5c9a868SElliott Hughes  *  the Free Software Foundation, either version 3 of the License, or
7*d5c9a868SElliott Hughes  *  (at your option) any later version.
8*d5c9a868SElliott Hughes  *
9*d5c9a868SElliott Hughes  *  Mtools is distributed in the hope that it will be useful,
10*d5c9a868SElliott Hughes  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11*d5c9a868SElliott Hughes  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*d5c9a868SElliott Hughes  *  GNU General Public License for more details.
13*d5c9a868SElliott Hughes  *
14*d5c9a868SElliott Hughes  *  You should have received a copy of the GNU General Public License
15*d5c9a868SElliott Hughes  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16*d5c9a868SElliott Hughes  *
17*d5c9a868SElliott Hughes  * filter to support filesystems stored at an offset into their image
18*d5c9a868SElliott Hughes  */
19*d5c9a868SElliott Hughes 
20*d5c9a868SElliott Hughes #include "sysincludes.h"
21*d5c9a868SElliott Hughes #include "msdos.h"
22*d5c9a868SElliott Hughes #include "mtools.h"
23*d5c9a868SElliott Hughes #include "offset.h"
24*d5c9a868SElliott Hughes 
25*d5c9a868SElliott Hughes typedef struct Offset_t {
26*d5c9a868SElliott Hughes 	struct Stream_t head;
27*d5c9a868SElliott Hughes 
28*d5c9a868SElliott Hughes 	mt_off_t offset;
29*d5c9a868SElliott Hughes } Offset_t;
30*d5c9a868SElliott Hughes 
offset_pread(Stream_t * Stream,char * buf,mt_off_t start,size_t len)31*d5c9a868SElliott Hughes static ssize_t offset_pread(Stream_t *Stream, char *buf,
32*d5c9a868SElliott Hughes 			    mt_off_t start, size_t len)
33*d5c9a868SElliott Hughes {
34*d5c9a868SElliott Hughes 	DeclareThis(Offset_t);
35*d5c9a868SElliott Hughes 	return PREADS(This->head.Next, buf, start+This->offset, len);
36*d5c9a868SElliott Hughes }
37*d5c9a868SElliott Hughes 
offset_pwrite(Stream_t * Stream,char * buf,mt_off_t start,size_t len)38*d5c9a868SElliott Hughes static ssize_t offset_pwrite(Stream_t *Stream, char *buf,
39*d5c9a868SElliott Hughes 			     mt_off_t start, size_t len)
40*d5c9a868SElliott Hughes {
41*d5c9a868SElliott Hughes 	DeclareThis(Offset_t);
42*d5c9a868SElliott Hughes 	return PWRITES(This->head.Next, buf, start+This->offset, len);
43*d5c9a868SElliott Hughes }
44*d5c9a868SElliott Hughes 
45*d5c9a868SElliott Hughes static Class_t OffsetClass = {
46*d5c9a868SElliott Hughes 	0,
47*d5c9a868SElliott Hughes 	0,
48*d5c9a868SElliott Hughes 	offset_pread,
49*d5c9a868SElliott Hughes 	offset_pwrite,
50*d5c9a868SElliott Hughes 	0, /* flush */
51*d5c9a868SElliott Hughes 	0, /* free */
52*d5c9a868SElliott Hughes 	set_geom_pass_through, /* set_geom */
53*d5c9a868SElliott Hughes 	0, /* get_data */
54*d5c9a868SElliott Hughes 	0, /* pre-allocate */
55*d5c9a868SElliott Hughes 	get_dosConvert_pass_through, /* dos convert */
56*d5c9a868SElliott Hughes 	0, /* discard */
57*d5c9a868SElliott Hughes };
58*d5c9a868SElliott Hughes 
OpenOffset(Stream_t * Next,struct device * dev,off_t offset,char * errmsg,mt_off_t * maxSize)59*d5c9a868SElliott Hughes Stream_t *OpenOffset(Stream_t *Next, struct device *dev, off_t offset,
60*d5c9a868SElliott Hughes 		     char *errmsg, mt_off_t *maxSize) {
61*d5c9a868SElliott Hughes 	Offset_t *This;
62*d5c9a868SElliott Hughes 
63*d5c9a868SElliott Hughes 	This = New(Offset_t);
64*d5c9a868SElliott Hughes 	if (!This){
65*d5c9a868SElliott Hughes 		printOom();
66*d5c9a868SElliott Hughes 		return 0;
67*d5c9a868SElliott Hughes 	}
68*d5c9a868SElliott Hughes 	memset((void*)This, 0, sizeof(Offset_t));
69*d5c9a868SElliott Hughes 	init_head(&This->head, &OffsetClass, Next);
70*d5c9a868SElliott Hughes 
71*d5c9a868SElliott Hughes 	This->offset = offset;
72*d5c9a868SElliott Hughes 
73*d5c9a868SElliott Hughes 	if(maxSize) {
74*d5c9a868SElliott Hughes 		if(This->offset > *maxSize) {
75*d5c9a868SElliott Hughes 			if(errmsg)
76*d5c9a868SElliott Hughes 				sprintf(errmsg,"init: Big disks not supported");
77*d5c9a868SElliott Hughes 			goto exit_0;
78*d5c9a868SElliott Hughes 		}
79*d5c9a868SElliott Hughes 
80*d5c9a868SElliott Hughes 		*maxSize -= This->offset;
81*d5c9a868SElliott Hughes 	}
82*d5c9a868SElliott Hughes 
83*d5c9a868SElliott Hughes 	if(adjust_tot_sectors(dev, This->offset, errmsg) < 0)
84*d5c9a868SElliott Hughes 		goto exit_0;
85*d5c9a868SElliott Hughes 
86*d5c9a868SElliott Hughes 	return &This->head;
87*d5c9a868SElliott Hughes  exit_0:
88*d5c9a868SElliott Hughes 	Free(This);
89*d5c9a868SElliott Hughes 	return NULL;
90*d5c9a868SElliott Hughes }
91