sbin/hammer: Cleanup header includes regarding hammer.h
[dragonfly.git] / sbin / hammer / cmd_history.c
1 /*
2  * Copyright (c) 2008 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/hammer/cmd_history.c,v 1.4 2008/06/24 17:40:21 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static void hammer_do_history(const char *path, off_t off, int len);
40 static void dumpat(const char *path, off_t off, int len);
41 static const char *timestr32(u_int32_t time32);
42 static __inline int test_strtol(int res, long val);
43 static __inline int test_strtoll(int res, long long val);
44
45 /*
46  * history <file1> ... <fileN>
47  */
48 void
49 hammer_cmd_history(const char *offset_str, char **av, int ac)
50 {
51         int i;
52         long long off;
53         long len;
54         char *rptr;
55
56         len = 32;
57         if (*offset_str == '@') {
58                 errno = 0; /* clear */
59                 off = strtoll(offset_str + 1, &rptr, 0);
60                 if (test_strtoll(errno, off)) {
61                         *rptr = '\0'; /* side effect */
62                         printf("%s: %s\n", strerror(errno), offset_str);
63                         exit(1);
64                 }
65                 if (*rptr == ',') {
66                         errno = 0; /* clear */
67                         len = strtol(rptr + 1, NULL, 0);
68                         if (test_strtol(errno, len)) {
69                                 printf("%s: %s\n", strerror(errno), rptr);
70                                 exit(1);
71                         }
72                         if (len < 0)
73                                 len = 32;
74                 }
75         } else {
76                 off = -1;
77         }
78
79         for (i = 0; i < ac; ++i)
80                 hammer_do_history(av[i], (off_t)off, (int)len);
81 }
82
83 static void
84 hammer_do_history(const char *path, off_t off, int len)
85 {
86         struct hammer_ioc_history hist;
87         const char *status;
88         int fd;
89         int i;
90
91         printf("%s\t", path);
92         fd = open(path, O_RDONLY);
93         if (fd < 0) {
94                 printf("%s\n", strerror(errno));
95                 return;
96         }
97         bzero(&hist, sizeof(hist));
98         hist.beg_tid = HAMMER_MIN_TID;
99         hist.end_tid = HAMMER_MAX_TID;
100
101         if (off >= 0) {
102                 hist.head.flags |= HAMMER_IOC_HISTORY_ATKEY;
103                 hist.key = off;
104                 hist.nxt_key = off + 1;
105         }
106
107
108         if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
109                 printf("%s\n", strerror(errno));
110                 close(fd);
111                 return;
112         }
113         status = ((hist.head.flags & HAMMER_IOC_HISTORY_UNSYNCED) ?
114                  "dirty" : "clean");
115         printf("%016jx %s {\n", (uintmax_t)hist.obj_id, status);
116         for (;;) {
117                 for (i = 0; i < hist.count; ++i) {
118                         char *hist_path = NULL;
119
120                         asprintf(&hist_path, "%s@@0x%016jx",
121                                  path, (uintmax_t)hist.hist_ary[i].tid);
122                         printf("    %016jx %s",
123                                (uintmax_t)hist.hist_ary[i].tid,
124                                timestr32(hist.hist_ary[i].time32));
125                         if (off >= 0) {
126                                 if (VerboseOpt) {
127                                         printf(" '");
128                                         dumpat(hist_path, off, len);
129                                         printf("'");
130                                 }
131                         }
132                         printf("\n");
133                         free(hist_path);
134                 }
135                 if (hist.head.flags & HAMMER_IOC_HISTORY_EOF)
136                         break;
137                 if (hist.head.flags & HAMMER_IOC_HISTORY_NEXT_KEY)
138                         break;
139                 if ((hist.head.flags & HAMMER_IOC_HISTORY_NEXT_TID) == 0)
140                         break;
141                 hist.beg_tid = hist.nxt_tid;
142                 if (ioctl(fd, HAMMERIOC_GETHISTORY, &hist) < 0) {
143                         printf("    error: %s\n", strerror(errno));
144                         break;
145                 }
146         }
147         printf("}\n");
148         close(fd);
149 }
150
151 static void
152 dumpat(const char *path, off_t off, int len)
153 {
154         char buf[1024];
155         int fd;
156         int n;
157         int r;
158
159         fd = open(path, O_RDONLY);
160         if (fd < 0)
161                 return;
162         lseek(fd, off, 0);
163         while (len) {
164                 n = (len > (int)sizeof(buf)) ? (int)sizeof(buf) : len;
165                 r = read(fd, buf, n);
166                 if (r <= 0)
167                         break;
168                 len -= r;
169                 for (n = 0; n < r; ++n) {
170                         if (isprint(buf[n]))
171                                 putc(buf[n], stdout);
172                         else
173                                 putc('.', stdout);
174                 }
175         }
176         close(fd);
177 }
178
179 /*
180  * Return a human-readable timestamp
181  */
182 static const char *
183 timestr32(u_int32_t time32)
184 {
185         static char timebuf[64];
186         time_t t = (time_t)time32;
187         struct tm *tp;
188
189         tp = localtime(&t);
190         strftime(timebuf, sizeof(timebuf), "%d-%b-%Y %H:%M:%S", tp);
191         return(timebuf);
192 }
193
194 /*
195  * Return non-zero on either overflow or underflow
196  */
197 static __inline int
198 test_strtol(int res, long val)
199 {
200         return(res == ERANGE && (val == LONG_MIN || val == LONG_MAX));
201 }
202
203 static __inline int
204 test_strtoll(int res, long long val)
205 {
206         return(res == ERANGE && (val == LLONG_MIN || val == LLONG_MAX));
207 }