Imported Upstream version 4.0.33
[platform/upstream/mtools.git] / offset.c
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 Class_t *Class;
27         int refs;
28         struct Stream_t *Next;
29         struct Stream_t *Buffer;
30
31         mt_off_t offset;
32 } Offset_t;
33
34 static ssize_t offset_read(Stream_t *Stream, char *buf,
35                           mt_off_t start, size_t len)
36 {
37         DeclareThis(Offset_t);
38         return READS(This->Next, buf, start+This->offset, len);
39 }
40
41 static ssize_t offset_write(Stream_t *Stream, char *buf,
42                            mt_off_t start, size_t len)
43 {
44         DeclareThis(Offset_t);
45         return WRITES(This->Next, buf, start+This->offset, len);
46 }
47
48 static Class_t OffsetClass = {
49         offset_read,
50         offset_write,
51         0, /* flush */
52         0, /* free */
53         set_geom_pass_through, /* set_geom */
54         0, /* get_data */
55         0, /* pre-allocate */
56         get_dosConvert_pass_through, /* dos convert */
57         0, /* discard */
58 };
59
60 Stream_t *OpenOffset(Stream_t *Next, struct device *dev, off_t offset,
61                      char *errmsg, mt_off_t *maxSize) {
62         Offset_t *This;
63
64         This = New(Offset_t);
65         if (!This){
66                 printOom();
67                 return 0;
68         }
69         memset((void*)This, 0, sizeof(Offset_t));
70         This->Class = &OffsetClass;
71         This->refs = 1;
72         This->Next = Next;
73
74         This->offset = offset;
75
76         if(maxSize) {
77                 if(This->offset > *maxSize) {
78                         if(errmsg)
79                                 sprintf(errmsg,"init: Big disks not supported");
80                         goto exit_0;
81                 }
82
83                 *maxSize -= This->offset;
84         }
85
86         if(adjust_tot_sectors(dev, This->offset, errmsg) < 0)
87                 goto exit_0;
88
89         return (Stream_t *) This;
90  exit_0:
91         Free(This);
92         return NULL;
93 }