First cut of the jscan utility. This will become the core utility for
[dragonfly.git] / sbin / jscan / jfile.c
1 /*
2  * Copyright (c) 2004,2005 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/jfile.c,v 1.1 2005/03/07 02:38:28 dillon Exp $
35  */
36
37 #include "jscan.h"
38
39 /*
40  * Open a journal for directional scanning
41  */
42 struct jfile *
43 jopen_stream(const char *path, enum jdirection jdir)
44 {
45     struct jfile *jf;
46
47     jf = malloc(sizeof(struct jfile));
48     bzero(jf, sizeof(struct jfile));
49     if ((jf->jf_fp = fopen(path, "r")) == NULL) {
50         free(jf);
51         return (NULL);
52     }
53     jf->jf_direction = jdir;
54     jf->jf_setpt = -1;
55     if (jdir == JF_BACKWARDS) {
56         fseeko(jf->jf_fp, 0L, SEEK_END);
57         jf->jf_pos = ftello(jf->jf_fp);
58     }
59     return(jf);
60 }
61
62 /*
63  * Close a previously opened journal, clean up any side allocations.
64  */
65 void
66 jclose_stream(struct jfile *jf)
67 {
68     struct jdata *jd;
69
70     fclose(jf->jf_fp);
71     jf->jf_fp = NULL;
72     while ((jd = jf->jf_saved) != NULL) {
73         jf->jf_saved = jd->jd_next;
74         free(jd);
75     }
76     free(jf);
77 }
78
79 /*
80  * Align us to the next 16 byte boundary.  If scanning forwards we align
81  * forwards if not already aligned.  If scanning backwards we align
82  * backwards if not already aligned.
83  */
84 void
85 jalign(struct jfile *jf)
86 {
87     if (jf->jf_direction == JF_FORWARDS) {
88         jf->jf_pos = (jf->jf_pos + 15) & ~15;
89         fseeko(jf->jf_fp, jf->jf_pos, SEEK_SET);
90     } else {
91         jf->jf_pos = jf->jf_pos & ~15;
92     }
93 }
94
95 /*
96  * Read data from a journal forwards or backwards.  Note that the file
97  * pointer's actual seek position does not match jf_pos in the reverse
98  * scan case.  Callers should never access jf_fp directly.
99  */
100 int
101 jread(struct jfile *jf, void *buf, int bytes)
102 {
103     if (jf->jf_direction == JF_FORWARDS) {
104         if (fread(buf, bytes, 1, jf->jf_fp) == 1) {
105                 jf->jf_pos += bytes;
106                 return (0);
107         } else {
108                 fseeko(jf->jf_fp, jf->jf_pos, SEEK_SET);
109                 return (errno);
110         }
111     } else {
112         if (bytes > jf->jf_pos)
113                 return (ENOENT);
114         jf->jf_pos -= bytes;
115         fseeko(jf->jf_fp, jf->jf_pos, SEEK_SET);
116         if (fread(buf, bytes, 1, jf->jf_fp) == 1) {
117                 return (0);
118         } else {
119                 jf->jf_pos += bytes;
120                 return (errno);
121         }
122     }
123 }
124
125 void
126 jset(struct jfile *jf)
127 {
128     jf->jf_setpt = jf->jf_pos;
129 }
130
131 void
132 jreturn(struct jfile *jf)
133 {
134     jf->jf_pos = jf->jf_setpt;
135     jf->jf_setpt = -1;
136     fseeko(jf->jf_fp, jf->jf_pos, SEEK_SET);
137 }
138
139 void
140 jflush(struct jfile *jf)
141 {
142     jf->jf_setpt = -1;
143 }
144