Major continuing work on jscan, the userland backend for the journaling
[dragonfly.git] / sbin / jscan / jscan.c
1 /*
2  * Copyright (c) 2003,2004 The DragonFly Project.  All rights reserved.
3  * 
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * 
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  * 
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  * 
34  * $DragonFly: src/sbin/jscan/jscan.c,v 1.3 2005/07/05 00:26:03 dillon Exp $
35  */
36
37 #include "jscan.h"
38
39 enum jmode { JS_NONE, JS_DEBUG, JS_MIRROR };
40
41 static void usage(const char *av0);
42
43 int debug_opt;
44
45 int
46 main(int ac, char **av)
47 {
48     int ch;
49     int i;
50     enum jdirection direction = JF_FORWARDS;
51     enum jmode jmode = JS_NONE;
52     struct jfile *jf;
53
54     while ((ch = getopt(ac, av, "dmr")) != -1) {
55         switch(ch) {
56         case 'd':
57             debug_opt = 1;
58             if (jmode == JS_NONE)
59                 jmode = JS_DEBUG;
60             break;
61         case 'm':
62             jmode = JS_MIRROR;
63             break;
64         case 'r':
65             direction = JF_BACKWARDS;
66             break;
67         default:
68             fprintf(stderr, "unknown option: -%c\n", optopt);
69             usage(av[0]);
70         }
71     }
72     if (jmode == JS_NONE)
73         usage(av[0]);
74     if (jmode == JS_MIRROR && direction == JF_BACKWARDS) {
75         fprintf(stderr, "Cannot mirror in reverse scan mode\n");
76         usage(av[0]);
77     }
78
79     /*
80      * Using specified input streams.  If no files are specified, stdin
81      * is used.
82      */
83     if (ac == optind) {
84         usage(av[0]);
85     } else {
86         for (i = optind; i < ac; ++i) {
87             if (strcmp(av[i], "stdin") == 0)
88                 jf = jopen_fp(stdin, direction);
89             else
90                 jf = jopen_stream(av[i], direction);
91             if (jf != NULL) {
92                 switch(jmode) {
93                 case JS_MIRROR:
94                     dump_mirror(jf);
95                     break;
96                 case JS_DEBUG:
97                     dump_debug(jf);
98                     break;
99                 case JS_NONE:
100                     break;
101                 }
102                 jclose_stream(jf);
103             } else {
104                 fprintf(stderr, "Unable to open %s: %s\n", 
105                                 av[i], strerror(errno));
106             }
107         }
108     }
109     return(0);
110 }
111
112 static void
113 usage(const char *av0)
114 {
115     fprintf(stderr, "%s [-dm] [journal_file/stdin]*\n", av0);
116     exit(1);
117 }
118