summaryrefslogtreecommitdiff
path: root/tests/progs/test_rel.c
blob: 351c662e046526aba9325e761a4abcdcd8a6254e (plain)
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
/*
 * test_rel.c
 * 
 * Copyright (C) 1997 Theodore Ts'o.
 *
 * %Begin-Header%
 * This file may be redistributed under the terms of the GNU Public
 * License.
 * %End-Header%
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_GETOPT_H
#include <getopt.h>
#endif
#include <fcntl.h>

#include <et/com_err.h>
#include <ss/ss.h>
#include <ext2fs/ext2_fs.h>
#include <ext2fs/ext2fs.h>
#include <ext2fs/irel.h>
#include <ext2fs/brel.h>

#include "test_rel.h"

extern ss_request_table test_cmds;

ext2_irel irel = NULL;
ext2_brel brel = NULL;

/*
 * Helper function which parses an inode number.
 */
static int parse_inode(const char *request, const char *desc,
		       const char *str, ext2_ino_t *ino)
{
	char *tmp;
	
	*ino = strtoul(str, &tmp, 0);
	if (*tmp) {
		com_err(request, 0, "Bad %s - %s", desc, str);
		return 1;
	}
	return 0;
}

/*
 * Helper function which parses a block number.
 */
static int parse_block(const char *request, const char *desc,
		       const char *str, blk_t *blk)
{
	char *tmp;
	
	*blk = strtoul(str, &tmp, 0);
	if (*tmp) {
		com_err(request, 0, "Bad %s - %s", desc, str);
		return 1;
	}
	return 0;
}

/*
 * Helper function which assures that a brel table is open
 */
static int check_brel(char *request)
{
	if (brel)
		return 0;
	com_err(request, 0, "A block relocation table must be open.");
	return 1;
}

/*
 * Helper function which assures that an irel table is open
 */
static int check_irel(char *request)
{
	if (irel)
		return 0;
	com_err(request, 0, "An inode relocation table must be open.");
	return 1;
}

/*
 * Helper function which displays a brel entry
 */
static void display_brel_entry(blk_t old,
			       struct ext2_block_relocate_entry *ent)
{
	printf("Old= %u, New= %u, Owner= %u:%u\n", old, ent->new,
	       ent->owner.block_ref, ent->offset);
}

/*
 * Helper function which displays an irel entry
 */
static void display_irel_entry(ext2_ino_t old,
			       struct ext2_inode_relocate_entry *ent,
			       int do_refs)
{
	struct ext2_inode_reference ref;
	errcode_t	retval;
	int		first = 1;
	
	printf("Old= %lu, New= %lu, Original=%lu, Max_refs=%u\n", old,
	       ent->new, ent->orig, ent->max_refs);
	if (!do_refs)
		return;

	retval = ext2fs_irel_start_iter_ref(irel, old);
	if (retval) {
		printf("\tCouldn't get references: %s\n",
		       error_message(retval));
		return;
	}
	while (1) {
		retval = ext2fs_irel_next_ref(irel, &ref);
		if (retval) {
			printf("(%s) ", error_message(retval));
			break;
		}
		if (ref.block == 0)
			break;
		if (first) {
			fputc('\t', stdout);
			first = 0;
		} else
			printf(", ");
		printf("%u:%u", ref.block, ref.offset);
	}
	if (!first)
		fputc('\n', stdout);
}

/*
 * These are the actual command table procedures
 */
void do_brel_ma_create(int argc, char **argv)
{
	const char *usage = "Usage: %s name max_blocks\n";
	errcode_t	retval;
	blk_t		max_blk;
	
	if (argc < 3) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_block(argv[0], "max_blocks", argv[2], &max_blk))
		return;
	retval = ext2fs_brel_memarray_create(argv[1], max_blk, &brel);
	if (retval) {
		com_err(argv[0], retval, "while opening memarray brel");
		return;
	}
	return;
}

void do_brel_free(int argc, char **argv)
{
	if (check_brel(argv[0]))
		return;
	ext2fs_brel_free(brel);
	brel = NULL;
	return;
}

void do_brel_put(int argc, char **argv)
{
	const char *usage = "usage: %s old_block new_block [owner] [offset]";
	errcode_t retval;
	struct ext2_block_relocate_entry ent;
	blk_t	old, new, offset=0, owner=0;
	
	if (check_brel(argv[0]))
		return;

	if (argc < 3) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_block(argv[0], "old block", argv[1], &old))
		return;
	if (parse_block(argv[0], "new block", argv[2], &new))
		return;
	if (argc > 3 &&
	    parse_block(argv[0], "owner block", argv[3], &owner))
		return;
	if (argc > 4 &&
	    parse_block(argv[0], "offset", argv[4], &offset))
		return;
	if (offset > 65535) {
		printf("Offset too large.\n");
		return;
	}
	ent.new = new;
	ent.offset = (__u16) offset;
	ent.flags = 0;
	ent.owner.block_ref = owner;

	retval = ext2fs_brel_put(brel, old, &ent);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_brel_put");
		return;
	}
	return;
}

void do_brel_get(int argc, char **argv)
{
	const char *usage = "%s block";
	errcode_t retval;
	struct ext2_block_relocate_entry ent;
	blk_t	blk;

	if (check_brel(argv[0]))
		return;
	if (argc < 2) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_block(argv[0], "block", argv[1], &blk))
		return;
	retval = ext2fs_brel_get(brel, blk, &ent);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_brel_get");
		return;
	}
	display_brel_entry(blk, &ent);
	return;
}

void do_brel_start_iter(int argc, char **argv)
{
	errcode_t retval;

	if (check_brel(argv[0]))
		return;

	retval = ext2fs_brel_start_iter(brel);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_brel_start_iter");
		return;
	}
	return;
}

void do_brel_next(int argc, char **argv)
{
	errcode_t retval;
	struct ext2_block_relocate_entry ent;
	blk_t	blk;

	if (check_brel(argv[0]))
		return;

	retval = ext2fs_brel_next(brel, &blk, &ent);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_brel_next");
		return;
	}
	if (blk == 0) {
		printf("No more entries!\n");
		return;
	}
	display_brel_entry(blk, &ent);
	return;
}

void do_brel_dump(int argc, char **argv)
{
	errcode_t retval;
	struct ext2_block_relocate_entry ent;
	blk_t	blk;

	if (check_brel(argv[0]))
		return;

	retval = ext2fs_brel_start_iter(brel);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_brel_start_iter");
		return;
	}

	while (1) {
		retval = ext2fs_brel_next(brel, &blk, &ent);
		if (retval) {
			com_err(argv[0], retval, "while calling ext2fs_brel_next");
			return;
		}
		if (blk == 0)
			break;
		
		display_brel_entry(blk, &ent);
	}
	return;
}

void do_brel_move(int argc, char **argv)
{
	const char *usage = "%s old_block new_block";
	errcode_t retval;
	blk_t	old, new;

	if (check_brel(argv[0]))
		return;
	if (argc < 2) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_block(argv[0], "old block", argv[1], &old))
		return;
	if (parse_block(argv[0], "new block", argv[2], &new))
		return;

	retval = ext2fs_brel_move(brel, old, new);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_brel_move");
		return;
	}
	return;
}

void do_brel_delete(int argc, char **argv)
{
	const char *usage = "%s block";
	errcode_t retval;
	blk_t	blk;

	if (check_brel(argv[0]))
		return;
	if (argc < 2) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_block(argv[0], "block", argv[1], &blk))
		return;

	retval = ext2fs_brel_delete(brel, blk);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_brel_delete");
		return;
	}
}

void do_irel_ma_create(int argc, char **argv)
{
	const char	*usage = "Usage: %s name max_inode\n";
	errcode_t	retval;
	ext2_ino_t	max_ino;
	
	if (argc < 3) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_inode(argv[0], "max_inodes", argv[2], &max_ino))
		return;
	retval = ext2fs_irel_memarray_create(argv[1], max_ino, &irel);
	if (retval) {
		com_err(argv[0], retval, "while opening memarray irel");
		return;
	}
	return;
}

void do_irel_free(int argc, char **argv)
{
	if (check_irel(argv[0]))
		return;

	ext2fs_irel_free(irel);
	irel = NULL;
	return;
}

void do_irel_put(int argc, char **argv)
{
	const char	*usage = "%s old new max_refs";
	errcode_t	retval;
	ext2_ino_t	old, new, max_refs;
	struct ext2_inode_relocate_entry ent;

	if (check_irel(argv[0]))
		return;
	
	if (argc < 4) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_inode(argv[0], "old inode", argv[1], &old))
		return;
	if (parse_inode(argv[0], "new inode", argv[2], &new))
		return;
	if (parse_inode(argv[0], "max_refs", argv[3], &max_refs))
		return;
	if (max_refs > 65535) {
		printf("max_refs too big\n");
		return;
	}
	ent.new = new;
	ent.max_refs = (__u16) max_refs;
	ent.flags = 0;

	retval = ext2fs_irel_put(irel, old, &ent);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_put");
		return;
	}
	return;
}

void do_irel_get(int argc, char **argv)
{
	const char	*usage = "%s inode";
	errcode_t	retval;
	ext2_ino_t	old;
	struct ext2_inode_relocate_entry ent;

	if (check_irel(argv[0]))
		return;
	
	if (argc < 2) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_inode(argv[0], "inode", argv[1], &old))
		return;

	retval = ext2fs_irel_get(irel, old, &ent);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_get");
		return;
	}
	display_irel_entry(old, &ent, 1);
	return;
}

void do_irel_get_by_orig(int argc, char **argv)
{
	const char	*usage = "%s orig_inode";
	errcode_t	retval;
	ext2_ino_t	orig, old;
	struct ext2_inode_relocate_entry ent;

	if (check_irel(argv[0]))
		return;
	
	if (argc < 2) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_inode(argv[0], "original inode", argv[1], &orig))
		return;

	retval = ext2fs_irel_get_by_orig(irel, orig, &old, &ent);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_get_by_orig");
		return;
	}
	display_irel_entry(old, &ent, 1);
	return;
}

void do_irel_start_iter(int argc, char **argv)
{
	errcode_t retval;

	if (check_irel(argv[0]))
		return;
	
	retval = ext2fs_irel_start_iter(irel);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_start_iter");
		return;
	}
	return;
}

void do_irel_next(int argc, char **argv)
{
	errcode_t	retval;
	ext2_ino_t	old;
	struct ext2_inode_relocate_entry ent;

	if (check_irel(argv[0]))
		return;

	retval = ext2fs_irel_next(irel, &old, &ent);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_next");
		return;
	}
	if (old == 0) {
		printf("No more entries!\n");
		return;
	}
	display_irel_entry(old, &ent, 1);
	return;
}

void do_irel_dump(int argc, char **argv)
{
	errcode_t	retval;
	ext2_ino_t	ino;
	struct ext2_inode_relocate_entry ent;

	if (check_irel(argv[0]))
		return;

	retval = ext2fs_irel_start_iter(irel);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_start_iter");
		return;
	}

	while (1) {
		retval = ext2fs_irel_next(irel, &ino, &ent);
		if (retval) {
			com_err(argv[0], retval, "while calling ext2fs_irel_next");
			return;
		}
		if (ino == 0)
			break;
		
		display_irel_entry(ino, &ent, 1);
	}
	return;
}

void do_irel_add_ref(int argc, char **argv)
{
	const char	*usage = "%s inode block offset";
	errcode_t	retval;
	blk_t		block, offset;
	ext2_ino_t	ino;
	struct ext2_inode_reference ref;
	

	if (check_irel(argv[0]))
		return;
	
	if (argc < 4) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_inode(argv[0], "inode", argv[1], &ino))
		return;
	if (parse_block(argv[0], "block", argv[2], &block))
		return;
	if (parse_block(argv[0], "offset", argv[3], &offset))
		return;
	if (offset > 65535) {
		printf("Offset too big.\n");
		return;
	}
	ref.block = block;
	ref.offset = offset;

	retval = ext2fs_irel_add_ref(irel, ino, &ref);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_add_ref");
		return;
	}
	return;
}

void do_irel_start_iter_ref(int argc, char **argv)
{
	const char	*usage = "%s inode";
	errcode_t	retval;
	ext2_ino_t	ino;

	if (check_irel(argv[0]))
		return;
	
	if (argc < 2) {
		printf(usage, argv[0]);
		return;
	}

	if (parse_inode(argv[0], "inode", argv[1], &ino))
		return;
	retval = ext2fs_irel_start_iter_ref(irel, ino);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_start_iter_ref");
		return;
	}
	return;
}

void do_irel_next_ref(int argc, char **argv)
{
	struct ext2_inode_reference ref;
	errcode_t retval;

	if (check_irel(argv[0]))
		return;
	
	retval = ext2fs_irel_next_ref(irel, &ref);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_next_ref");
		return;
	}
	printf("Inode reference: %u:%u\n", ref.block, ref.offset);
	return;
}

void do_irel_move(int argc, char **argv)
{
	const char	*usage = "%s old new";
	errcode_t	retval;
	ext2_ino_t	old, new;

	if (check_irel(argv[0]))
		return;
	
	if (argc < 3) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_inode(argv[0], "old inode", argv[1], &old))
		return;
	if (parse_inode(argv[0], "new inode", argv[2], &new))
		return;

	retval = ext2fs_irel_move(irel, old, new);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_move");
		return;
	}
	return;
}

void do_irel_delete(int argc, char **argv)
{
	const char	*usage = "%s inode";
	errcode_t	retval;
	ext2_ino_t	ino;

	if (check_irel(argv[0]))
		return;
	
	if (argc < 2) {
		printf(usage, argv[0]);
		return;
	}
	if (parse_inode(argv[0], "inode", argv[1], &ino))
		return;

	retval = ext2fs_irel_delete(irel, ino);
	if (retval) {
		com_err(argv[0], retval, "while calling ext2fs_irel_delete");
		return;
	}
	return;
}

static int source_file(const char *cmd_file, int sci_idx)
{
	FILE		*f;
	char		buf[256];
	char		*cp;
	int		exit_status = 0;
	int		retval;
	int 		noecho;

	if (strcmp(cmd_file, "-") == 0)
		f = stdin;
	else {
		f = fopen(cmd_file, "r");
		if (!f) {
			perror(cmd_file);
			exit(1);
		}
	}
	setbuf(stdout, NULL);
	setbuf(stderr, NULL);
	while (!feof(f)) {
		if (fgets(buf, sizeof(buf), f) == NULL)
			break;
		if (buf[0] == '#')
			continue;
		noecho = 0;
		if (buf[0] == '-') {
			noecho = 1;
			buf[0] = ' ';
		}
		cp = strchr(buf, '\n');
		if (cp)
			*cp = 0;
		cp = strchr(buf, '\r');
		if (cp)
			*cp = 0;
		if (!noecho)
			printf("test_rel: %s\n", buf);
		retval = ss_execute_line(sci_idx, buf);
		if (retval) {
			ss_perror(sci_idx, retval, buf);
			exit_status++;
		}
	}
	return exit_status;
}

void main(int argc, char **argv)
{
	int		retval;
	int		sci_idx;
	const char	*usage = "Usage: test_rel [-R request] [-f cmd_file]";
	char		c;
	char		*request = 0;
	int		exit_status = 0;
	char		*cmd_file = 0;
	
	initialize_ext2_error_table();

	while ((c = getopt (argc, argv, "wR:f:")) != EOF) {
		switch (c) {
		case 'R':
			request = optarg;
			break;
		case 'f':
			cmd_file = optarg;
			break;
		default:
			com_err(argv[0], 0, usage);
			return;
		}
	}
	sci_idx = ss_create_invocation("test_rel", "0.0", (char *) NULL,
				       &test_cmds, &retval);
	if (retval) {
		ss_perror(sci_idx, retval, "creating invocation");
		exit(1);
	}

	(void) ss_add_request_table (sci_idx, &ss_std_requests, 1, &retval);
	if (retval) {
		ss_perror(sci_idx, retval, "adding standard requests");
		exit (1);
	}
	if (request) {
		retval = 0;
		retval = ss_execute_line(sci_idx, request);
		if (retval) {
			ss_perror(sci_idx, retval, request);
			exit_status++;
		}
	} else if (cmd_file) {
		exit_status = source_file(cmd_file, sci_idx);
	} else {
		ss_listen(sci_idx);
	}

	exit(exit_status);
}