xref: /dragonfly/sbin/hammer/cycle.c (revision f254e677)
1d7ae405cSMatthew Dillon /*
2d7ae405cSMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3d7ae405cSMatthew Dillon  *
4d7ae405cSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5d7ae405cSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6d7ae405cSMatthew Dillon  *
7d7ae405cSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8d7ae405cSMatthew Dillon  * modification, are permitted provided that the following conditions
9d7ae405cSMatthew Dillon  * are met:
10d7ae405cSMatthew Dillon  *
11d7ae405cSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12d7ae405cSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13d7ae405cSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14d7ae405cSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15d7ae405cSMatthew Dillon  *    the documentation and/or other materials provided with the
16d7ae405cSMatthew Dillon  *    distribution.
17d7ae405cSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18d7ae405cSMatthew Dillon  *    contributors may be used to endorse or promote products derived
19d7ae405cSMatthew Dillon  *    from this software without specific, prior written permission.
20d7ae405cSMatthew Dillon  *
21d7ae405cSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22d7ae405cSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23d7ae405cSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24d7ae405cSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25d7ae405cSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26d7ae405cSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27d7ae405cSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28d7ae405cSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29d7ae405cSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30d7ae405cSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31d7ae405cSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d7ae405cSMatthew Dillon  * SUCH DAMAGE.
33d7ae405cSMatthew Dillon  *
34243ca327SMatthew Dillon  * $DragonFly: src/sbin/hammer/cycle.c,v 1.5 2008/07/07 00:27:22 dillon Exp $
35d7ae405cSMatthew Dillon  */
36d7ae405cSMatthew Dillon 
37d7ae405cSMatthew Dillon #include "hammer.h"
38d7ae405cSMatthew Dillon 
3958c17893SMatthew Dillon void
hammer_get_cycle(hammer_base_elm_t base,hammer_tid_t * extra)40243ca327SMatthew Dillon hammer_get_cycle(hammer_base_elm_t base, hammer_tid_t *extra)
41d7ae405cSMatthew Dillon {
42243ca327SMatthew Dillon 	struct stat st;
43243ca327SMatthew Dillon 	int fd;
44d7ae405cSMatthew Dillon 
45243ca327SMatthew Dillon 	if (CyclePath && (fd = open(CyclePath, O_RDONLY)) >= 0) {
46243ca327SMatthew Dillon 		if (fstat(fd, &st) < 0) {
47243ca327SMatthew Dillon 			fprintf(stderr, "cycle-file %s: cannot stat\n",
48243ca327SMatthew Dillon 				CyclePath);
49243ca327SMatthew Dillon 			close(fd);
50243ca327SMatthew Dillon 			return;
51243ca327SMatthew Dillon 		}
52a276dc6bSMatthew Dillon 		if (st.st_size < (off_t)sizeof(*base)) {
53243ca327SMatthew Dillon 			fprintf(stderr, "cycle-file %s: clearing old version\n",
54243ca327SMatthew Dillon 				CyclePath);
55243ca327SMatthew Dillon 			close(fd);
56243ca327SMatthew Dillon 			remove(CyclePath);
57243ca327SMatthew Dillon 			return;
58243ca327SMatthew Dillon 		}
59243ca327SMatthew Dillon 		if (read(fd, base, sizeof(*base)) != sizeof(*base)) {
60243ca327SMatthew Dillon 			fprintf(stderr, "cycle-file %s: read failed %s\n",
61243ca327SMatthew Dillon 				CyclePath, strerror(errno));
62243ca327SMatthew Dillon 			return;
63243ca327SMatthew Dillon 		}
64*f254e677STomohiro Kusumi 		if (extra) {
65*f254e677STomohiro Kusumi 			if (read(fd, extra, sizeof(*extra)) != sizeof(*extra)) {
66243ca327SMatthew Dillon 				fprintf(stderr, "cycle-file %s: Warning, malformed\n",
67d7ae405cSMatthew Dillon 					CyclePath);
68*f254e677STomohiro Kusumi 			}
69*f254e677STomohiro Kusumi 		}
70243ca327SMatthew Dillon 		close(fd);
71243ca327SMatthew Dillon 	}
72243ca327SMatthew Dillon 	/* ok if the file does not exist */
73d7ae405cSMatthew Dillon }
74d7ae405cSMatthew Dillon 
75d7ae405cSMatthew Dillon void
hammer_set_cycle(hammer_base_elm_t base,hammer_tid_t extra)76243ca327SMatthew Dillon hammer_set_cycle(hammer_base_elm_t base, hammer_tid_t extra)
77d7ae405cSMatthew Dillon {
78243ca327SMatthew Dillon 	int fd;
79d7ae405cSMatthew Dillon 
80243ca327SMatthew Dillon 	if ((fd = open(CyclePath, O_RDWR|O_CREAT|O_TRUNC, 0666)) >= 0) {
81243ca327SMatthew Dillon 		write(fd, base, sizeof(*base));
82243ca327SMatthew Dillon 		write(fd, &extra, sizeof(extra));
83243ca327SMatthew Dillon 		close(fd);
84d7ae405cSMatthew Dillon 	} else {
85d7ae405cSMatthew Dillon 		fprintf(stderr, "Warning: Unable to write to %s: %s\n",
86d7ae405cSMatthew Dillon 			CyclePath, strerror(errno));
87d7ae405cSMatthew Dillon 	}
88d7ae405cSMatthew Dillon }
89d7ae405cSMatthew Dillon 
90d7ae405cSMatthew Dillon void
hammer_reset_cycle(void)91d7ae405cSMatthew Dillon hammer_reset_cycle(void)
92d7ae405cSMatthew Dillon {
93d7ae405cSMatthew Dillon 	remove(CyclePath);
94d7ae405cSMatthew Dillon }
95d7ae405cSMatthew Dillon 
96