1*d5c9a868SElliott Hughes /* Copyright 1996,1997,1999,2001,2002,2008,2009 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
18*d5c9a868SElliott Hughes #include "sysincludes.h"
19*d5c9a868SElliott Hughes #include "msdos.h"
20*d5c9a868SElliott Hughes #include "stream.h"
21*d5c9a868SElliott Hughes
22*d5c9a868SElliott Hughes int batchmode = 0;
23*d5c9a868SElliott Hughes
limitSizeToOffT(size_t * len,mt_off_t maxLen)24*d5c9a868SElliott Hughes void limitSizeToOffT(size_t *len, mt_off_t maxLen)
25*d5c9a868SElliott Hughes {
26*d5c9a868SElliott Hughes #if SIZEOF_SIZE_T >= SIZEOF_MT_OFF_T
27*d5c9a868SElliott Hughes if(*len > (size_t) maxLen)
28*d5c9a868SElliott Hughes #else
29*d5c9a868SElliott Hughes if(*len > maxLen)
30*d5c9a868SElliott Hughes #endif
31*d5c9a868SElliott Hughes *len = (size_t) maxLen;
32*d5c9a868SElliott Hughes }
33*d5c9a868SElliott Hughes
init_head(Stream_t * Stream,struct Class_t * Class,Stream_t * Next)34*d5c9a868SElliott Hughes void init_head(Stream_t *Stream, struct Class_t *Class, Stream_t *Next)
35*d5c9a868SElliott Hughes {
36*d5c9a868SElliott Hughes Stream->Class = Class;
37*d5c9a868SElliott Hughes Stream->refs = 1;
38*d5c9a868SElliott Hughes Stream->Next = Next;
39*d5c9a868SElliott Hughes }
40*d5c9a868SElliott Hughes
flush_stream(Stream_t * Stream)41*d5c9a868SElliott Hughes int flush_stream(Stream_t *Stream)
42*d5c9a868SElliott Hughes {
43*d5c9a868SElliott Hughes int ret=0;
44*d5c9a868SElliott Hughes if(!batchmode) {
45*d5c9a868SElliott Hughes if(Stream->Class->flush)
46*d5c9a868SElliott Hughes ret |= Stream->Class->flush(Stream);
47*d5c9a868SElliott Hughes if(Stream->Next)
48*d5c9a868SElliott Hughes ret |= flush_stream(Stream->Next);
49*d5c9a868SElliott Hughes }
50*d5c9a868SElliott Hughes return ret;
51*d5c9a868SElliott Hughes }
52*d5c9a868SElliott Hughes
copy_stream(Stream_t * Stream)53*d5c9a868SElliott Hughes Stream_t *copy_stream(Stream_t *Stream)
54*d5c9a868SElliott Hughes {
55*d5c9a868SElliott Hughes if(Stream)
56*d5c9a868SElliott Hughes Stream->refs++;
57*d5c9a868SElliott Hughes return Stream;
58*d5c9a868SElliott Hughes }
59*d5c9a868SElliott Hughes
free_stream(Stream_t ** Stream)60*d5c9a868SElliott Hughes int free_stream(Stream_t **Stream)
61*d5c9a868SElliott Hughes {
62*d5c9a868SElliott Hughes int ret=0;
63*d5c9a868SElliott Hughes
64*d5c9a868SElliott Hughes if(!*Stream)
65*d5c9a868SElliott Hughes return -1;
66*d5c9a868SElliott Hughes if(! --(*Stream)->refs){
67*d5c9a868SElliott Hughes if((*Stream)->Class->flush)
68*d5c9a868SElliott Hughes ret |= (*Stream)->Class->flush(*Stream);
69*d5c9a868SElliott Hughes if((*Stream)->Class->freeFunc)
70*d5c9a868SElliott Hughes ret |= (*Stream)->Class->freeFunc(*Stream);
71*d5c9a868SElliott Hughes if((*Stream)->Next)
72*d5c9a868SElliott Hughes ret |= free_stream(&(*Stream)->Next);
73*d5c9a868SElliott Hughes Free(*Stream);
74*d5c9a868SElliott Hughes }
75*d5c9a868SElliott Hughes *Stream = NULL;
76*d5c9a868SElliott Hughes return ret;
77*d5c9a868SElliott Hughes }
78*d5c9a868SElliott Hughes
79*d5c9a868SElliott Hughes
80*d5c9a868SElliott Hughes #define GET_DATA(stream, date, size, type, address) \
81*d5c9a868SElliott Hughes (stream)->Class->get_data( (stream), (date), (size), (type), (address) )
82*d5c9a868SElliott Hughes
set_geom_pass_through(Stream_t * Stream,device_t * dev,device_t * orig_dev)83*d5c9a868SElliott Hughes int set_geom_pass_through(Stream_t *Stream, device_t *dev, device_t *orig_dev)
84*d5c9a868SElliott Hughes {
85*d5c9a868SElliott Hughes return SET_GEOM(Stream->Next, dev, orig_dev);
86*d5c9a868SElliott Hughes }
87*d5c9a868SElliott Hughes
set_geom_noop(Stream_t * Stream UNUSEDP,device_t * dev UNUSEDP,device_t * orig_dev UNUSEDP)88*d5c9a868SElliott Hughes int set_geom_noop(Stream_t *Stream UNUSEDP,
89*d5c9a868SElliott Hughes device_t *dev UNUSEDP,
90*d5c9a868SElliott Hughes device_t *orig_dev UNUSEDP)
91*d5c9a868SElliott Hughes {
92*d5c9a868SElliott Hughes return 0;
93*d5c9a868SElliott Hughes }
94*d5c9a868SElliott Hughes
get_data_pass_through(Stream_t * Stream,time_t * date,mt_off_t * size,int * type,uint32_t * address)95*d5c9a868SElliott Hughes int get_data_pass_through(Stream_t *Stream, time_t *date, mt_off_t *size,
96*d5c9a868SElliott Hughes int *type, uint32_t *address)
97*d5c9a868SElliott Hughes {
98*d5c9a868SElliott Hughes return GET_DATA(Stream->Next, date, size, type, address);
99*d5c9a868SElliott Hughes }
100*d5c9a868SElliott Hughes
pread_pass_through(Stream_t * Stream,char * buf,mt_off_t start,size_t len)101*d5c9a868SElliott Hughes ssize_t pread_pass_through(Stream_t *Stream, char *buf,
102*d5c9a868SElliott Hughes mt_off_t start, size_t len)
103*d5c9a868SElliott Hughes {
104*d5c9a868SElliott Hughes return PREADS(Stream->Next, buf, start, len);
105*d5c9a868SElliott Hughes }
106*d5c9a868SElliott Hughes
pwrite_pass_through(Stream_t * Stream,char * buf,mt_off_t start,size_t len)107*d5c9a868SElliott Hughes ssize_t pwrite_pass_through(Stream_t *Stream, char *buf,
108*d5c9a868SElliott Hughes mt_off_t start, size_t len)
109*d5c9a868SElliott Hughes {
110*d5c9a868SElliott Hughes return PWRITES(Stream->Next, buf, start, len);
111*d5c9a868SElliott Hughes }
112*d5c9a868SElliott Hughes
get_dosConvert_pass_through(Stream_t * Stream)113*d5c9a868SElliott Hughes doscp_t *get_dosConvert_pass_through(Stream_t *Stream)
114*d5c9a868SElliott Hughes {
115*d5c9a868SElliott Hughes return GET_DOSCONVERT(Stream->Next);
116*d5c9a868SElliott Hughes }
117*d5c9a868SElliott Hughes
118*d5c9a868SElliott Hughes /*
119*d5c9a868SElliott Hughes * Adjust number of total sectors by given offset in bytes
120*d5c9a868SElliott Hughes */
adjust_tot_sectors(struct device * dev,mt_off_t offset,char * errmsg)121*d5c9a868SElliott Hughes int adjust_tot_sectors(struct device *dev, mt_off_t offset, char *errmsg)
122*d5c9a868SElliott Hughes {
123*d5c9a868SElliott Hughes if(!dev->tot_sectors)
124*d5c9a868SElliott Hughes /* tot_sectors not set, do nothing */
125*d5c9a868SElliott Hughes return 0;
126*d5c9a868SElliott Hughes
127*d5c9a868SElliott Hughes mt_off_t offs_sectors = offset /
128*d5c9a868SElliott Hughes (dev->sector_size ? dev->sector_size : 512);
129*d5c9a868SElliott Hughes if(offs_sectors > 0 && dev->tot_sectors < (smt_off_t) offs_sectors) {
130*d5c9a868SElliott Hughes if(errmsg)
131*d5c9a868SElliott Hughes sprintf(errmsg,"init: Offset bigger than base image");
132*d5c9a868SElliott Hughes return -1;
133*d5c9a868SElliott Hughes }
134*d5c9a868SElliott Hughes dev->tot_sectors -= (uint32_t) offs_sectors;
135*d5c9a868SElliott Hughes return 0;
136*d5c9a868SElliott Hughes }
137