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