File: file.c

package info (click to toggle)
mtools 3.8-1
  • links: PTS
  • area: main
  • in suites: hamm
  • size: 1,116 kB
  • ctags: 1,306
  • sloc: ansic: 11,489; sh: 2,052; makefile: 223; sed: 8
file content (419 lines) | stat: -rw-r--r-- 8,680 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
#include "sysincludes.h"
#include "msdos.h"
#include "stream.h"
#include "mtools.h"
#include "fsP.h"
#include "file.h"
#include "htable.h"

typedef struct File_t {
	Class_t *Class;
	int refs;
	struct Fs_t *Fs;	/* Filesystem that this fat file belongs to */
	Stream_t *Buffer;

	int (*map)(struct File_t *this, off_t where, size_t *len, int mode);
	size_t FileSize;

	/* Absolute position of first cluster of file */
	unsigned int FirstAbsCluNr;

	/* Absolute position of previous cluster */
	unsigned int PreviousAbsCluNr;

	/* Relative position of previous cluster */
	unsigned int PreviousRelCluNr;
	struct directory dir;
	int locked;
	Stream_t *ParentDir;
} File_t;

static int normal_map(File_t *This, off_t where, size_t *len, int mode)
{
	int offset;
	int NrClu; /* number of clusters to read */
	unsigned int RelCluNr;
	unsigned int CurCluNr;
	unsigned int NewCluNr;
	unsigned int AbsCluNr;
	int clus_size;
	Fs_t *Fs = This->Fs;

	clus_size = Fs->cluster_size * Fs->sector_size;
	offset = where % clus_size;

	if (mode == MT_READ)
		maximize(len, This->FileSize - where);
	if (*len == 0 )
		return 0;

	if (This->FirstAbsCluNr < 2){
		if( mode == MT_READ || *len == 0){
			*len = 0;
			return 0;
		}
		NewCluNr = get_next_free_cluster(This->Fs, 1);
		if (NewCluNr == 1 ){
			errno = ENOSPC;
			return -2;
		}
		This->FirstAbsCluNr = NewCluNr;
		Fs->fat_encode(This->Fs, NewCluNr, Fs->end_fat);
	}

	RelCluNr = where / clus_size;
	
	if (RelCluNr >= This->PreviousRelCluNr){
		CurCluNr = This->PreviousRelCluNr;
		AbsCluNr = This->PreviousAbsCluNr;
	} else {
		CurCluNr = 0;
		AbsCluNr = This->FirstAbsCluNr;
	}


	NrClu = (offset + *len - 1) / clus_size;
	while (CurCluNr <= RelCluNr + NrClu){
		if (CurCluNr == RelCluNr){
			/* we have reached the beginning of our zone. Save
			 * coordinates */
			This->PreviousRelCluNr = RelCluNr;
			This->PreviousAbsCluNr = AbsCluNr;
		}
		NewCluNr = Fs->fat_decode(This->Fs, AbsCluNr);
		if (NewCluNr == 1 ){
			fprintf(stderr,"Fat problem while decoding %d\n", 
				AbsCluNr);
			exit(1);
		}
		if(CurCluNr == RelCluNr + NrClu)			
			break;
		if (NewCluNr > Fs->last_fat && mode == MT_WRITE){
			/* if at end, and writing, extend it */
			NewCluNr = get_next_free_cluster(This->Fs, AbsCluNr);
			if (NewCluNr == 1 ){ /* no more space */
				errno = ENOSPC;
				return -2;
			}
			Fs->fat_encode(This->Fs, AbsCluNr, NewCluNr);
			Fs->fat_encode(This->Fs, NewCluNr, Fs->end_fat);
		}

		if (CurCluNr < RelCluNr && NewCluNr > Fs->last_fat){
			*len = 0;
			return 0;
		}

		if (CurCluNr >= RelCluNr && NewCluNr != AbsCluNr + 1)
			break;
		CurCluNr++;
		AbsCluNr = NewCluNr;
	}

	maximize(len, (1 + CurCluNr - RelCluNr) * clus_size - offset);

	return ((This->PreviousAbsCluNr-2) * Fs->cluster_size+Fs->clus_start) *
			Fs->sector_size + offset;
}


static int root_map(File_t *This, off_t where, size_t *len, int mode)
{
	Fs_t *Fs = This->Fs;

	if(Fs->dir_len * Fs->sector_size < where) {
		*len = 0;
		errno = ENOSPC;
		return -2;
	}

	maximize(len, Fs->dir_len * Fs->sector_size - where);
	return Fs->dir_start * Fs->sector_size + where;
}
	

static int read_file(Stream_t *Stream, char *buf, off_t where, size_t len)
{
	DeclareThis(File_t);
	int pos;

	Stream_t *Disk = This->Fs->Next;
	
	pos = This->map(This, where, &len, MT_READ);
	if(pos < 0)
		return pos;
	return READS(Disk, buf, pos, len);
}

static int write_file(Stream_t *Stream, char *buf, off_t where, size_t len)
{
	DeclareThis(File_t);
	int pos;
	int ret;
	Stream_t *Disk = This->Fs->Next;

	pos = This->map(This, where, &len, MT_WRITE);
	if( pos < 0)
		return pos;
	ret = WRITES(Disk, buf, pos, len);
	if ( ret > 0 && where + ret > This->FileSize )
		This->FileSize = where + ret;
	return ret;
}


/*
 * Convert an MSDOS time & date stamp to the Unix time() format
 */

static int month[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
static inline long conv_stamp(struct directory *dir)
{
	struct tm *tmbuf;
	long tzone, dst;
	time_t accum;

	accum = DOS_YEAR(dir) - 1970; /* years past */

	/* days passed */
	accum = accum * 365L + month[DOS_MONTH(dir)-1] + DOS_DAY(dir);

	/* leap years */
	accum += (DOS_YEAR(dir) - 1972) / 4L;

	/* back off 1 day if before 29 Feb */
	if (!(DOS_YEAR(dir) % 4) && DOS_MONTH(dir) < 3)
	        accum--;
	accum = accum * 24L + DOS_HOUR(dir); /* hours passed */
	accum = accum * 60L + DOS_MINUTE(dir); /* minutes passed */
	accum = accum * 60L + DOS_SEC(dir); /* seconds passed */

	/* correct for Time Zone */
#ifdef HAVE_GETTIMEOFDAY
	{
		struct timeval tv;
		struct timezone tz;
		
		gettimeofday(&tv, &tz);
		tzone = tz.tz_minuteswest * 60L;
	}
#else
#ifdef HAVE_TZSET
	{
#ifndef OS_ultrix
		/* Ultrix defines this to be a different type */
		extern long timezone;
#endif
		tzset();
		tzone = (long) timezone;
	}
#else
	tzone = 0;
#endif /* HAVE_TZSET */
#endif /* HAVE_GETTIMEOFDAY */
	
	accum += tzone;

	/* correct for Daylight Saving Time */
	tmbuf = localtime(&accum);
	dst = (tmbuf->tm_isdst) ? (-60L * 60L) : 0L;
	accum += dst;
	
	return accum;
}


static int get_file_data(Stream_t *Stream, long *date, size_t *size,
			 int *type, int *address)
{
	DeclareThis(File_t);

	if(date)
		*date = conv_stamp(& This->dir);
	if(size)
		*size = This->FileSize;
	if(type)
		*type = This->dir.attr & 0x10;
	if(address)
		*address = This->FirstAbsCluNr;
	return 0;
}

T_HashTable *filehash;

static int free_file(Stream_t *Stream)
{
	DeclareThis(File_t);
	FREE(&This->ParentDir);
	return hash_remove(filehash, (void *) Stream, 0);
}

static Class_t FileClass = { 
	read_file, 
	write_file, 
	0, /* flush */
	free_file, /* free */
	0, /* get_geom */
	get_file_data 
};


static unsigned int func1(void *Stream)
{
	DeclareThis(File_t);

	return This->FirstAbsCluNr ^ (long) This->Fs;
}

static unsigned int func2(void *Stream)
{
	DeclareThis(File_t);

	return This->FirstAbsCluNr;
}

static int comp(void *Stream, void *Stream2)
{
	DeclareThis(File_t);

	File_t *This2 = (File_t *) Stream2;

	return This->Fs != This2->Fs ||
		This->FirstAbsCluNr != This2->FirstAbsCluNr;
}

static void init_hash(void)
{
	static int is_initialised=0;
	
	if(!is_initialised){
		make_ht(func1, func2, comp, 20, &filehash);
		is_initialised = 1;
	}
}


static Stream_t *OpenFileByNumAndSize(Stream_t *Dir, unsigned int first, 
				      size_t size, struct directory *dir);

Stream_t *open_root(Stream_t *Dir)
{
	Stream_t *Stream = GetFs(Dir);
	DeclareThis(Fs_t);
	File_t *File;
	File_t Pattern;
	unsigned int num;

	num = fat32RootCluster(Dir);

	if(num)		
		return OpenFileByNumAndSize(Dir, num, MAX_SIZE, 0);

	init_hash();
	This->refs++;
	Pattern.Fs = (Fs_t *) This;
	Pattern.FirstAbsCluNr = 0;
	if(!hash_lookup(filehash, (T_HashTableEl) &Pattern, 
			(T_HashTableEl **)&File, 0)){
		File->refs++;
		This->refs--;
		return (Stream_t *) File;
	}

	File = New(File_t);
	File->Fs = This;
	if (!File)
		return NULL;

	File->ParentDir = 0;
	File->FirstAbsCluNr = 0;	
	File->FileSize = -1;
	File->Class = &FileClass;
	File->map = root_map;
	File->refs = 1;
	File->Buffer = 0;
	hash_add(filehash, (void *) File);
	return (Stream_t *) File;
}

Stream_t *open_file(Stream_t *Dir, struct directory *dir)
{
	Stream_t *Stream = GetFs(Dir);
	unsigned int first;
	size_t size;

	first = START(dir);
	if(fat32RootCluster(Stream))
		first |= STARTHI(dir) << 16;

	if(!first && (dir->attr & 0x10))
		return open_root(Stream);
	
	if (dir->attr & 0x10 )
		size = MAX_SIZE;
	else 
		size = FILE_SIZE(dir);

	return OpenFileByNumAndSize(Dir, first, size, dir);
}

static Stream_t *OpenFileByNumAndSize(Stream_t *Dir, unsigned int first, 
				      size_t size, struct directory *dir)
{
	Stream_t *Stream = GetFs(Dir);
	DeclareThis(Fs_t);
	File_t Pattern;
	File_t *File;

	init_hash();
	This->refs++;
	if(first >= 2){
		/* if we have a zero-addressed file here, it is certainly
		 * _not_ the root directory, but rather a newly created
		 * file */
		Pattern.Fs = This;
		Pattern.FirstAbsCluNr = first;
		if(!hash_lookup(filehash, (T_HashTableEl) &Pattern, 
				(T_HashTableEl **)&File, 0)){
			File->refs++;
			This->refs--;
			return (Stream_t *) File;
		}
	}

	File = New(File_t);
	if (!File)
		return NULL;
	
	/* memorize dir for date and attrib */
	if(dir)
		File->dir = *dir;
	File->ParentDir = COPY(Dir);
	File->locked = 0;
	File->Class = &FileClass;
	File->Fs = This;
	File->map = normal_map;
	File->FirstAbsCluNr = first;
	File->PreviousRelCluNr = 0xffff;
	File->FileSize = size;
	File->refs = 1;
	File->Buffer = 0;
	hash_add(filehash, (void *) File);
	return (Stream_t *) File;
}

int FileIsLocked(Stream_t *Stream)
{
	DeclareThis(File_t);

	return This->locked;
}

void LockFile(Stream_t *Stream)
{
	DeclareThis(File_t);

	This->locked = 1;
}