File: mfile.cc

package info (click to toggle)
ht 2.0.20-2
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,324 kB
  • sloc: cpp: 97,563; ansic: 17,183; sh: 3,811; lex: 226; makefile: 213; yacc: 127
file content (873 lines) | stat: -rw-r--r-- 22,432 bytes parent folder | download | duplicates (4)
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
/*
 *	HT Editor
 *	mfile.cc
 *
 *	Copyright (C) 1999-2003 Stefan Weyergraf (stefan@weyergraf.de)
 *
 *	This program is free software; you can redistribute it and/or modify
 *	it under the terms of the GNU General Public License version 2 as
 *	published by the Free Software Foundation.
 *
 *	This program is distributed in the hope that it will be useful,
 *	but WITHOUT ANY WARRANTY; without even the implied warranty of
 *	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *	GNU General Public License for more details.
 *
 *	You should have received a copy of the GNU General Public License
 *	along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <cerrno>
#include <cstdlib>
#include <cstring>

// <debug>
#include "htdebug.h"
#include "snprintf.h"
// </debug>

#include "except.h"
#include "mfile.h"

#define OBJID_MFA MAGIC32("FAr\0")
#define OBJID_CFA MAGIC32("FAr\1")

// "min condition" value, see makeAreaModified(...)
#define MIN_COND_MFA_SIZE	(16)

// absolute maximum size of a MFA
#define MAX_MFA_SIZE		(128)

/*
 *	File area (FA)
 */
FileArea::FileArea(FileOfs Start, FileOfs Size)
{
	start = Start;
	size = Size;
}

int FileArea::compareTo(const Object *obj) const
{
	const FileArea *l, *r;
	int sign = 1;
	l = (FileArea*)obj;
	r = this;
	if (r->start < l->start) {
		const FileArea *t = l;
		l = r;
		r = t;
		sign = -sign;
	}

	if (r->start < l->start + l->size) return 0;
	return sign;
}

/*
 *	Modified file area (MFA)
 */
ModifiedFileArea::ModifiedFileArea(FileOfs start, FileOfs size)
: FileArea(start, size)
{
	buf = ht_malloc(size);
}

ModifiedFileArea::~ModifiedFileArea()
{
	free(buf);
}

ObjectID ModifiedFileArea::getObjectID() const
{
	return OBJID_MFA;
}

/*
 *	Copied file area (CFA)
 */
CopiedFileArea::CopiedFileArea(FileOfs start, FileOfs size, FileOfs Src_start)
: FileArea(start, size)
{
	src_start = Src_start;
}

ObjectID CopiedFileArea::getObjectID() const
{
	return OBJID_CFA;
}

/*
 *	File modification layer (FML)
 *
 *	FML is a FileLayer, which adds efficient in-memory file modification
 *	abilities.
 *	To achieve this, FML uses a tree containing FileArea (FA) objects, that
 *	describe the differences between the modified and the original file.
 *
 *	'mods' (tree which contains FAs) rules:
 *	(1) all FAs together must exactly cover the area [0, getSize()-1]
 *	(2) no two FAs may overlap
 *	(3) MFAs size may not be less than 1 (but not necessarily less than
 *	    MIN_COND_MFA_SIZE, as the name might suggest !)
 *	(4) MFAs size may not be greater than MAX_MFA_SIZE
 *	(5) two adjacent MFAs must have the sum of their sizes > MAX_MFA_SIZE
 *
 *	(general) minimize both the number of FAs in 'mods' and the space wasted
 *	          by each FA (ie. make MFAs at least 48 bytes in 'size',
 *	          because malloc will always alloc this many bytes for
 *	          MFA->buf (~16 bytes), the MFA itself (~16 bytes) and it's
 *	          entry in AVLTree (~16 bytes) on most architectures)
 */
FileModificator::FileModificator(File *file, bool own_file)
: FileLayer(file, own_file), mods(true)
{
	mcount = 0;
	inv_mcount = 0;
	invalidateMods();
}

void FileModificator::checkSanity()
{
	FileOfs s = 0;
	FileArea *prevx = NULL;
	foreach(FileArea, x, mods,
//		bool iscopy = dynamic_cast<CopiedFileArea*>(x) != NULL;
		bool ismod = dynamic_cast<ModifiedFileArea*>(x) != NULL;
		// rule (1) part 1 and rule (2)
		if (x->start != s)
			throw MsgfException("mfile-sanity: area starts at 0x%qx, should be 0x%qx", x->start, s);
		// rule (3)
		if (x->size < 1)
			throw MsgfException("mfile-sanity: area at 0x%qx, has size %qd (must be >= %d)", x->start, x->size, 1);
		// rule (4)
		if (x->size > MAX_MFA_SIZE && ismod)
			throw MsgfException("mfile-sanity: area at 0x%qx, has size %qd (must be <= %d)", x->start, x->size, MAX_MFA_SIZE);
		// rule (5)
/*		if (prevx && (dynamic_cast<ModifiedFileArea*>(prevx) != NULL)
		&& ismod && (((ModifiedFileArea*)prevx)->size + 
		((ModifiedFileArea*)x)->size <= MAX_MFA_SIZE))
			throw MsgException("mfile-sanity: two adjacent MFAs with sum of sizes <= MAX_MFA_SIZE = %d", MAX_MFA_SIZE);
*/		s += x->size;
		prevx = x;
	);
	// rule (1) part 2
	if (newsize != s)
		throw MsgfException("mfile-sanity: newsize = %qx != %qx = calculated_size", &newsize, &s);
}

void FileModificator::debug()
{
	FILE *f = stderr;
	fprintf(f, "<areas>\n");
	foreach(FileArea, x, mods,
		if (dynamic_cast<ModifiedFileArea*>(x)) {
			ModifiedFileArea *m = (ModifiedFileArea *)x;
			ht_fprintf(f, "\tmodify %08qx, %qd bytes", &x->start, &x->size);
			for (uint i=0; i < x->size; i++) {
				ht_fprintf(f, " %02x", m->buf[i]);
			}
			ht_fprintf(f, " '");
			for (uint i=0; i<x->size; i++) {
				byte c = m->buf[i];
				if ((c<32) || (c>0x80)) c = 32;
				ht_fprintf(f, "%c", c);
			}
			ht_fprintf(f, "'\n");
		} else {
			CopiedFileArea *c = (CopiedFileArea *)x;
			ht_fprintf(f, "\tcopy   %08qx, %qd bytes, orig start %08qx\n", &x->start, &x->size, &c->src_start);
		}
	);
	fprintf(f, "</areas>\n");
	fprintf(f, "sanity:\n");
	checkSanity();
}

bool FileModificator::cut1(ObjHandle h, FileOfs rstart, FileOfs size)
{
	FileArea *a = (FileArea*)mods.get(h);
	bool have_head_gap = rstart != 0;
	bool have_tail_gap = a->size > rstart+size;
	if (!have_head_gap && !have_tail_gap) {
		mods -= h;
		return true;
	}
	if (a->getObjectID() == OBJID_CFA) {
		CopiedFileArea *c = (CopiedFileArea*)a;
		if (have_head_gap) {
			FileOfs csize = c->size;
			c->size = rstart;
			if (have_tail_gap) {
				mods += new CopiedFileArea(c->start + rstart, csize - rstart - size, c->src_start +rstart+size);
			}
		} else {
			c->size -= size;
			c->src_start += size;
		}
	} else {
		assert(a->getObjectID() == OBJID_MFA);
		ModifiedFileArea *m = (ModifiedFileArea*)a;
		memmove(m->buf + rstart, m->buf + rstart+size, m->size-rstart-size);
		m->size -= size;
		m->buf = (byte*)realloc(m->buf, m->size);
/*		if (have_head_gap) {
			byte *newbuf = ht_malloc(m->size);
			memcpy(newbuf, m->buf, rstart);
			memcpy(newbuf+rstart, m->buf+rstart+size, m->size-rstart);
			free(m->buf);
			m->buf = newbuf;
		} else {
			memmove(m->buf, m->buf + size, m->size);
			m->buf = (byte*)realloc(m->buf, m->size);
		}
*/
	}
	return false;
}

/*
 *	NOTE 1 (aka changing keys of already-inserted Tree-elements, see below)
 *	======
 *	this is bad code because we change the key
 *	of an already-inserted Tree-element, but here it is both
 *	fast and functional...
 */
#define STREAM_COPYBUF_SIZE	(64*1024)
FileOfs FileModificator::copyAllTo(Stream *stream)
{
	byte *buf = new byte[STREAM_COPYBUF_SIZE];
	FileOfs result = 0;
	uint r, t;
	do {
		uint k = STREAM_COPYBUF_SIZE;
		r = read(buf, k);
		assert(r <= k);
		t = stream->write(buf, r);
		assert(t <= r);
		result += t;
		if (t != k) break;
	} while (t);
	delete[] buf;
	return result;
}

FileOfs FileModificator::copyTo(Stream *stream, FileOfs count)
{
	byte *buf = new byte[STREAM_COPYBUF_SIZE];
	FileOfs result = 0;
	while (count) {
		FileOfs k = STREAM_COPYBUF_SIZE;
		if (k > count) k = count;
		uint r = read(buf, k);
		assert(r <= k);
		uint t = stream->write(buf, r);
		assert(t <= r);
		count -= t;
		result += t;
		if (t != k) break;
	}
	delete[] buf;
	return result;
}

void FileModificator::cut(FileOfs size)
{
	if (!(getAccessMode() & IOAM_WRITE)) throw IOException(EACCES);

	FileOfs o = tell();
	if (o + size > newsize) throw IOException(EINVAL);
	ObjHandle h = findArea(o);
	FileOfs ssize = size;

//	int i = 1;
	while (size) {
//		printf("it %d\n", i++);
		assert(h != invObjHandle)
		FileArea *a = (FileArea*)mods.get(h);
		FileOfs s = o - a->start;
		FileOfs z = a->size - s;
		if (z > size) z = size;
		ObjHandle hnext = mods.findNext(h);
		bool deleted = cut1(h, s, z);

		if (!deleted && o == a->start)
			a->start -= ssize-size;// see NOTE 1 above
		o += z;
		size -= z;
		newsize -= z;
		if (deleted) {
			h = findArea(o);
		} else {
			h = hnext;
		}			
	}
	while (h != invObjHandle) {
		FileArea *a = (FileArea*)mods.get(h);
		h = mods.findNext(h);
		a->start -= ssize;	// see NOTE 1 above
	}
	mcount++;
}

void FileModificator::extend(FileOfs Newsize)
{
	if (Newsize == newsize) return;
	if (Newsize < newsize) throw IOException(EINVAL);
	if (!(getAccessMode() & IOAM_WRITE)) throw IOException(EACCES);

	seek(0);
	// find last area
	ObjHandle h = (newsize!=0) ? findArea(newsize-1) : invObjHandle;
	FileOfs c = Newsize-newsize;
	if (h != invObjHandle) {
		FileArea *a = (FileArea*)mods.get(h);
		ModifiedFileArea *m = dynamic_cast<ModifiedFileArea*>(a);
		if (m) {
			// if its a modification, merge with the following modifications
			if (m->size < MAX_MFA_SIZE) {
				FileOfs s = m->size + c;
				if (s > MAX_MFA_SIZE) s = MAX_MFA_SIZE;
				m->buf = (byte*)realloc(m->buf, s);
				FileOfs d = s - m->size;
				memset(m->buf+m->size, 0, d);
				newsize += d;
				c -= d;
				m->size = s;
			}
		}
	}
	while (c != 0) {
		FileOfs s = c;
		if (s > MAX_MFA_SIZE) s = MAX_MFA_SIZE;
		ModifiedFileArea *a = new ModifiedFileArea(newsize, s);
		memset(a->buf, 0, s);
		mods += a;
		newsize += s;
		c -= s;
	}
	mcount++;
}

ObjHandle FileModificator::findArea(FileOfs o)
{
	ObjHandle h;
//	if (o != 0) {
		FileArea a(o, 1);
		h = mods.find(&a);
/*	} else {
		h = mods.findFirst();
	}*/
	return h;
}

String &FileModificator::getDesc(String &result) const
{
	mFile->getDesc(result);
	if (isModified()) result.prepend("in-memory modified ");
	return result;
}

FileOfs FileModificator::getSize() const
{
	return newsize;
}

void FileModificator::insert(const void *buf, FileOfs size)
{
	if (!(getAccessMode() & IOAM_WRITE)) throw IOException(EACCES);

	ObjHandle h;

	if (!size) return;
	FileOfs ssize = size;
	// (1) insert/merge or append MFAs
	FileOfs t = tell();
//	ht_printf("tell %qx\n", t);
	ObjHandle h_a = findArea(t);
	FileArea *a = (FileArea*)mods.get(h_a);

	// really insert or append ?
	if (a) {
		// yes, insert.

		// relocate all FAs after 'a'
		h = mods.findLast();
		while (h != h_a) {
			FileArea *a = (FileArea*)mods.get(h);
			h = mods.findPrev(h);
			a->start += ssize;	// see NOTE 1 above
		}
		if (a->start == t) {
			a->start += ssize;
			FileOfs start = t;
			while (ssize != 0) {
				FileOfs k = MAX_MFA_SIZE;
				if (k > ssize) k = ssize;
				mods += new ModifiedFileArea(start, k);
				ssize -= k;
				start += k;
			}
		} else if (a->getObjectID() == OBJID_CFA) {
			// split CFA at offset t, and relocate high (offset) part
			CopiedFileArea *c = (CopiedFileArea*)a;
			FileOfs d = t - a->start;
			// b becomes 'high' (offset) a
			FileArea *b = new CopiedFileArea(c->start + d + ssize, c->size -d, c->src_start+d);
			// a becomes 'low' (offset) a
			a->size = d;
			mods += b;

			// create new MFAs between 'a' and 'b'
			FileOfs start = t;
			while (ssize != 0) {
				FileOfs k = MAX_MFA_SIZE;
				if (k > ssize) k = ssize;
				mods += new ModifiedFileArea(start, k);
				ssize -= k;
				start += k;
			}
		} else {
			ModifiedFileArea *m = (ModifiedFileArea*)a;
			// FIXME: should merge existing MFA with new one(s)

			// split MFA at offset t and relocate high (offset) part
			FileOfs d = t - a->start;
			ModifiedFileArea *b = new ModifiedFileArea(m->start + d + ssize, m->size -d);
			memcpy(b->buf, m->buf+d, b->size);
			m->size = d;
			m->buf = (byte*)realloc(m->buf, d);

			mods += b;

			// create new MFAs between 'a' and 'b'
			FileOfs start = t;
			while (ssize != 0) {
				FileOfs k = MAX_MFA_SIZE;
				if (k > ssize) k = ssize;
				mods += new ModifiedFileArea(start, k);
				ssize -= k;
				start += k;
			}
		}
	} else {
		// no, append.
		FileOfs start = t;
		while (ssize != 0) {
			FileOfs k = MAX_MFA_SIZE;
			if (k > ssize) k = ssize;
			mods += new ModifiedFileArea(start, k);
			ssize -= k;
			start += k;
		}
	}
	
	// (2) write data
	const byte *b = (const byte*)buf;
	FileOfs o = t;
	h = findArea(o);
	while (size && (h != invObjHandle)) {
		FileArea *a = (FileArea*)mods.get(h);
		FileOfs s = o - a->start;
		FileOfs z = a->size - s;
		if (z > size) z = size;
		if (z > a->size) z = a->size;
//		ht_printf("%qx, now write %qx\n", a->start, z);
		write1(a, s, b, z);
		o += z;
		size -= z;
		b += z;
		h = mods.findNext(h);
	}
	newsize += o-t;
	seek(o);
	mcount++;
}

void FileModificator::makeAreaModified(ObjHandle h, FileOfs rstart, FileOfs size)
{
	FileArea *a = (FileArea*)mods.get(h);
	if (a->getObjectID() == OBJID_CFA) {
		// not marked modified, do something
		CopiedFileArea *c = (CopiedFileArea*)a;
		FileOfs csrc_start = c->src_start;
		FileOfs cstart = c->start;
		FileOfs csize = c->size;
		bool have_head_gap = rstart != 0;
		bool have_tail_gap = csize > rstart+size;
		ModifiedFileArea *prev_m = (ModifiedFileArea*)mods.get(mods.findPrev(h));
//		ASSERT(!prev_m || (prev_m->getObjectID() == OBJID_MFA));
		if (prev_m && (prev_m->getObjectID() != OBJID_MFA)) prev_m = NULL;
		ModifiedFileArea *next_m = (ModifiedFileArea*)mods.get(mods.findNext(h));
		if (next_m && (next_m->getObjectID() != OBJID_MFA)) next_m = NULL;
//		ASSERT(!next_m || (next_m->getObjectID() == OBJID_MFA));
		// "min condition"
		// ===============
		// trick for FA packing: we simply make modified areas bigger
		// than they would have to be. (either at least MIN_COND_MFA_SIZE
		// big or adjacent/mergable with another MFA)
		bool min_condition = (size < MIN_COND_MFA_SIZE) && (!prev_m || have_head_gap) && (!next_m || have_tail_gap);
		FileOfs min_addsize;
		FileOfs min_ofs;
		FileOfs min_src_ofs;
		if (min_condition) {
			// if the MFA we will create would be too small, make it bigger
			// (compare gap sizes, then try to reach boundary to
			// reduce number of FAs)
			if (rstart < csize-rstart-size) {
				// head gap is smaller than tail gap
				FileOfs z = rstart;
				if (z+size > MIN_COND_MFA_SIZE) z = MIN_COND_MFA_SIZE-size;
				min_addsize = z;
				min_ofs = cstart+rstart-z;
				min_src_ofs = csrc_start+rstart-z;
				rstart -= z;
			} else {
				// tail gap is smaller than head gap
				FileOfs z = csize-rstart-size;
				if (z+size > MIN_COND_MFA_SIZE) z = MIN_COND_MFA_SIZE-size;
				min_addsize = z;
				min_ofs = cstart+rstart+size;
				min_src_ofs = csrc_start+rstart+size;
			}
			size += min_addsize;
			have_head_gap = rstart != 0;
			have_tail_gap = csize > rstart+size;
		}
		if (have_head_gap) {
			c->size = rstart;
			cstart += rstart;
			csrc_start += rstart;
			csize -= rstart;
		} else {
			mods -= h;
		}			
		if (next_m && !have_tail_gap) {
			// merge with next MFA (successor)
			FileOfs nnsize = next_m->size + size;
			if (nnsize > MAX_MFA_SIZE) nnsize = MAX_MFA_SIZE;
			mods.removeObj(next_m);
			FileOfs rnnsize = nnsize - next_m->size;
			next_m->buf = (byte*)realloc(next_m->buf, nnsize);
			next_m->start -= rnnsize;
			memmove(next_m->buf+rnnsize, next_m->buf, next_m->size);
			next_m->size = nnsize;
			mods += next_m;
			csize -= rnnsize;
			size -= rnnsize;
		}
		if (prev_m && !have_head_gap) {
			// merge with previous MFA (predecessor)
			FileOfs pnsize = prev_m->size + size;
			if (pnsize > MAX_MFA_SIZE) pnsize = MAX_MFA_SIZE;
			FileOfs rpnsize = pnsize - prev_m->size;
			prev_m->size = pnsize;
			prev_m->buf = (byte*)realloc(prev_m->buf, pnsize);
			csrc_start += rpnsize;
			cstart += rpnsize;
			csize -= rpnsize;
			size -= rpnsize;
		}
		while (size != 0) {
			FileOfs k = MAX_MFA_SIZE;
			if (k > size) k = size;
			mods += new ModifiedFileArea(cstart, k);
			size -= k;
			csize -= k;
			cstart += k;
			csrc_start += k;
		}
		if (have_tail_gap) {
			mods += new CopiedFileArea(cstart, csize, csrc_start);
		}
		if (min_condition) {
			byte *buf = ht_malloc(min_addsize);
			mFile->seek(min_src_ofs);
			mFile->readx(buf, min_addsize);
			ObjHandle ha = findArea(min_ofs);
			while (min_addsize != 0) {
				FileArea *a = (FileArea*)mods.get(ha);
				assert(dynamic_cast<ModifiedFileArea*>(a));
				FileOfs k = min_addsize;
				if (k > a->size- (min_ofs-a->start)) k = a->size- (min_ofs-a->start);
				write1(a, min_ofs - a->start, buf, k);
				min_addsize -= k;
				min_ofs += k;
				ha = mods.findNext(ha);
			}
			free(buf);
		}
	}
}

void FileModificator::read1(FileArea *a, FileOfs rstart, byte *buf, uint count)
{
	CopiedFileArea *c;
	ModifiedFileArea *m;
	if ((m = dynamic_cast<ModifiedFileArea*>(a))) {
		memcpy(buf, m->buf + rstart, count);
	} else if ((c = dynamic_cast<CopiedFileArea*>(a))) {
		mFile->seek(c->src_start+rstart);
		mFile->read(buf, count);
	} else assert(0);
}

uint FileModificator::read(void *buf, uint size)
{
	if (!(getAccessMode() & IOAM_READ)) throw IOException(EACCES);

	FileOfs t = tell();
	if (t == 0x00000000ULL) {
		int a = 1;
	}
	FileOfs o = t;
	ObjHandle h = findArea(o);
	byte *b = (byte*)buf;

	while (size && h != invObjHandle) {
		FileArea *a = (FileArea*)mods.get(h);
		FileOfs s = o - a->start;
		FileOfs z = a->size - s;
		if (z > size) z = size;
		h = mods.findNext(h);
		read1(a, s, b, z);
		o += z;
		size -= z;
		b += z;
	}
	seek(o);
	return o - t;
}

void FileModificator::seek(FileOfs offset)
{
	if (offset > newsize) throw IOException(EINVAL);
	pos = offset;
}

FileOfs FileModificator::tell() const
{
	return pos;
}

void FileModificator::truncate(FileOfs Newsize)
{
	if (Newsize == newsize) return;
	if (Newsize > newsize) throw IOException(EINVAL);
	if (!(getAccessMode() & IOAM_WRITE)) throw IOException(EACCES);
	seek(0);
	ObjHandle h = findArea(Newsize);
	FileArea *a = (FileArea*)mods.get(h);
	if (a->start < Newsize) {
		if (a->getObjectID() == OBJID_CFA) {
			CopiedFileArea *c = (CopiedFileArea*)a;
			c->size = Newsize - a->start;
		} else {
			ModifiedFileArea *m = (ModifiedFileArea*)a;
			m->size = Newsize - a->start;
			m->buf = (byte*)realloc(m->buf, m->size);
		}
		h = mods.findNext(h);
	}
	while (h != invObjHandle) {
		ObjHandle hnext = mods.findNext(h);
		FileArea *a = (FileArea*)mods.get(hnext);
		mods -= h;
		if (!a) break;
		h = findArea(a->start);
	}
	newsize = Newsize;
	mcount++;
}

void FileModificator::flushMods()
{
	// make sure we can read and write
	int e;
	IOAccessMode old_am = mFile->getAccessMode();
	e = mFile->setAccessMode(IOAM_READ | IOAM_WRITE);
	if (e) throw IOException(e);
	// start work
	if (newsize > mFile->getSize()) mFile->extend(newsize);
	// store CFAs with start > src_start in descending order
	foreachbwd(FileArea, fa, mods,
		if (fa->getObjectID() == OBJID_CFA) {
			CopiedFileArea *cfa = (CopiedFileArea*)fa;
			if (cfa->start > cfa->src_start) {
				mFile->move(cfa->src_start, cfa->start, cfa->size);
			}
		}
	);
	// store CFAs with start < src_start in ascending order
	foreach(FileArea, fa, mods,
		if (fa->getObjectID() == OBJID_CFA) {
			CopiedFileArea *cfa = (CopiedFileArea*)fa;
			if (cfa->start < cfa->src_start) {
				mFile->move(cfa->src_start, cfa->start, cfa->size);
			}
		}
	);
	// store MFAs
	foreach(FileArea, fa, mods,
		if (fa->getObjectID() == OBJID_MFA) {
			ModifiedFileArea *mfa = (ModifiedFileArea*)fa;
			mFile->seek(mfa->start);
			mFile->writex(mfa->buf, mfa->size);
		}
	);
	//
	if (newsize < mFile->getSize()) mFile->truncate(newsize);
	// restore old access mode
	e = mFile->setAccessMode(old_am);
	if (e) throw IOException(e);
	//
	inv_mcount = mcount;
	invalidateMods();
}

void FileModificator::invalidateMods()
{
	mods.delAll();
	newsize = mFile->getSize();
	if (newsize != 0) mods += new CopiedFileArea(0, newsize, 0);
	pos = 0;
	mcount = inv_mcount;
}

bool FileModificator::isModified() const
{
	if (newsize != mFile->getSize()) return true;
	foreach(FileArea, fa, mods,
		CopiedFileArea *cfa = dynamic_cast<CopiedFileArea*>(fa);
		if (!cfa || (cfa->start != cfa->src_start)) return true;
	);
	return false;
}

bool FileModificator::isModifiedByte(FileOfs o)
{
	ObjHandle h = findArea(o);
	FileArea *fa = (FileArea*)mods.get(h);
	if (!fa) return true;
	if (fa->getObjectID() == OBJID_MFA) {
		ModifiedFileArea *mfa = (ModifiedFileArea*)fa;
		int pg_ofs = o - fa->start;
		byte b;
		mFile->seek(o);
		mFile->readx(&b, 1);
		return mfa->buf[pg_ofs] != b;
	} else if (fa->getObjectID() == OBJID_CFA) {
		CopiedFileArea *cfa = (CopiedFileArea*)fa;
		if (cfa->src_start == cfa->start) return false;
		int pg_ofs = o - fa->start;
		byte b1, b2;
		mFile->seek(o);
		mFile->readx(&b1, 1);
		mFile->seek(cfa->src_start + pg_ofs);
		mFile->readx(&b2, 1);
		return (b1 != b2);
	}
	return false;
}

int FileModificator::vcntl(uint cmd, va_list vargs)
{
	switch (cmd) {
	case FCNTL_MODS_INVD:
		invalidateMods();
		return 0;
	case FCNTL_MODS_FLUSH:
		flushMods();
		return 0;
	case FCNTL_MODS_CLEAR_DIRTY_RANGE: {
/*
		// BEFORE-IMPLEMENT: decl changed !
		FileOfs o = va_arg(vargs, FileOfs);
		uint s = va_arg(vargs, uint);
		uint i = 0;
		while (s--) {
			cleardirtybyte(o+i);
			i++;
		}
*/
		return 0;
	}
	case FCNTL_MODS_IS_DIRTY: {
		// const FileOfs offset, const FileOfs range, bool &isdirty
		FileOfs o = va_arg(vargs, FileOfs);
		FileOfs s = va_arg(vargs, FileOfs);
		bool &b = (bool&)*va_arg(vargs, bool*);
		if (o == 0 && s == newsize) {
			b = isModified();
		} else if (s <= 16) {
			try {
				bool bb = false;
				while (s--) {
					bb |= isModifiedByte(o++);
				}
				b = bb;
			} catch (const IOException &x) {
				return EIO;
			}
		} else {
			return ENOSYS;
		}
		return 0;
	}
	case FCNTL_GET_MOD_COUNT: {	// int &mcount
		int *mc = va_arg(vargs, int *);
		*mc = mcount;
		return 0;
	}		
	}
	return mFile->vcntl(cmd, vargs);
}

void FileModificator::write1(FileArea *a, FileOfs rstart, const byte *buf, uint count)
{
	assert(a->getObjectID() == OBJID_MFA);
//	ObjectID id = a->getObjectID();
	ModifiedFileArea *m = (ModifiedFileArea*)a;
	memcpy(m->buf+rstart, buf, count);
}

uint FileModificator::write(const void *buf, uint size)
{
	if (!(getAccessMode() & IOAM_WRITE)) throw IOException(EACCES);
	// (1) make areas modified
	FileOfs t = tell();
	FileOfs o = t;
	uint osize = size;
	while (size) {
		ObjHandle h = findArea(o);
		if (h == invObjHandle) break;
		FileArea *a = (FileArea*)mods.get(h);
		FileOfs s = o - a->start;
		FileOfs z = a->size - s;
		if (z > size) z = size;
		makeAreaModified(h, s, z);
		o += z;
		size -= z;
	}
	// (2) write data
	const byte *b = (const byte*)buf;
	o = t;
	size = osize;
	ObjHandle h = findArea(o);
	while (size && (h != invObjHandle)) {
		FileArea *a = (FileArea*)mods.get(h);
		FileOfs s = o - a->start;
		FileOfs z = a->size - s;
		if (z > size) z = size;
		if (z > a->size) z = a->size;
		write1(a, s, b, z);
		o += z;
		size -= z;
		b += z;
		h = mods.findNext(h);
	}
	seek(o);
	mcount++;
	return o - t;
}