GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.bin/rcs/worklist.c Lines: 23 24 95.8 %
Date: 2017-11-13 Branches: 5 6 83.3 %

Line Branch Exec Source
1
/*	$OpenBSD: worklist.c,v 1.4 2015/06/13 20:15:21 nicm Exp $	*/
2
/*
3
 * Copyright (c) 2006 Joris Vink <joris@openbsd.org>
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions
8
 * are met:
9
 *
10
 * 1. Redistributions of source code must retain the above copyright
11
 *    notice, this list of conditions and the following disclaimer.
12
 * 2. The name of the author may not be used to endorse or promote products
13
 *    derived from this software without specific prior written permission.
14
 *
15
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
16
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
17
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
18
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
 */
26
27
#include <sys/queue.h>
28
29
#include <err.h>
30
#include <signal.h>
31
#include <stdlib.h>
32
#include <string.h>
33
#include <unistd.h>
34
35
#include "worklist.h"
36
#include "xmalloc.h"
37
38
/*
39
 * adds a path to a worklist.
40
 */
41
void
42
worklist_add(const char *path, struct wklhead *worklist)
43
{
44
	size_t len;
45
	struct worklist *wkl;
46
50
	sigset_t old, new;
47
48
25
	wkl = xcalloc(1, sizeof(*wkl));
49
50
25
	len = strlcpy(wkl->wkl_path, path, sizeof(wkl->wkl_path));
51
25
	if (len >= sizeof(wkl->wkl_path))
52
		errx(1, "path truncation in worklist_add");
53
54
25
	sigfillset(&new);
55
25
	sigprocmask(SIG_BLOCK, &new, &old);
56
25
	SLIST_INSERT_HEAD(worklist, wkl, wkl_list);
57
25
	sigprocmask(SIG_SETMASK, &old, NULL);
58
25
}
59
60
/*
61
 * run over the given worklist, calling cb for each element.
62
 * this is just like worklist_clean(), except we block signals first.
63
 */
64
void
65
worklist_run(struct wklhead *list, void (*cb)(struct worklist *))
66
{
67
10
	sigset_t old, new;
68
	struct worklist *wkl;
69
70
5
	sigfillset(&new);
71
5
	sigprocmask(SIG_BLOCK, &new, &old);
72
73
5
	worklist_clean(list, cb);
74
75
60
	while ((wkl = SLIST_FIRST(list)) != NULL) {
76
25
		SLIST_REMOVE_HEAD(list, wkl_list);
77
25
		free(wkl);
78
	}
79
80
5
	sigprocmask(SIG_SETMASK, &old, NULL);
81
5
}
82
83
/*
84
 * pass elements to the specified callback, which has to be signal safe.
85
 */
86
void
87
worklist_clean(struct wklhead *list, void (*cb)(struct worklist *))
88
{
89
	struct worklist *wkl;
90
91
65
	SLIST_FOREACH(wkl, list, wkl_list)
92
25
	    cb(wkl);
93
5
}
94
95
void
96
worklist_unlink(struct worklist *wkl)
97
{
98
50
	(void)unlink(wkl->wkl_path);
99
25
}