-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmksums_common.c
More file actions
133 lines (110 loc) · 2.95 KB
/
mksums_common.c
File metadata and controls
133 lines (110 loc) · 2.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
* mksums, a tool for hashing all files in a directory tree
* Copyright (C) 2015, 2016 Lennert Buytenhek
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version
* 2.1 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License version 2.1 for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License version 2.1 along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <iv_list.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>
#include "mksums_common.h"
int openat_try_noatime(int dirfd, const char *pathname, int flags)
{
int fd;
fd = openat(dirfd, pathname, flags | O_RDONLY | O_NOFOLLOW | O_NOATIME);
if (fd < 0 && errno == EPERM)
fd = openat(dirfd, pathname, flags | O_RDONLY | O_NOFOLLOW);
return fd;
}
void print_dir_path(FILE *fp, struct dir *dir)
{
if (dir->parent != NULL) {
print_dir_path(fp, dir->parent);
putc('/', fp);
}
fprintf(fp, "%s", dir->name);
}
static void queue_free_dir(struct dir **dir_chain, struct dir *dir)
{
if (dir->dirfd != -1) {
if (dir->parent != NULL)
queue_free_dir(dir_chain, dir->parent);
close(dir->dirfd);
dir->dirfd = -1;
dir->parent = *dir_chain;
*dir_chain = dir;
}
}
void free_file_chain(struct iv_list_head *files)
{
struct dir *dir_chain;
dir_chain = NULL;
while (!iv_list_empty(files)) {
struct file_to_hash *fh;
fh = iv_container_of(files->next, struct file_to_hash, list);
iv_list_del(&fh->list);
queue_free_dir(&dir_chain, fh->dir);
free(fh);
}
while (dir_chain != NULL) {
struct dir *dir;
dir = dir_chain;
dir_chain = dir->parent;
free(dir);
}
}
void run_threads(void *(*handler)(void *), void *cookie, int nthreads)
{
pthread_attr_t attr;
int ret;
pthread_t tid[nthreads];
int i;
ret = pthread_attr_init(&attr);
if (ret) {
fprintf(stderr, "pthread_attr_init: %s\n", strerror(ret));
exit(1);
}
ret = pthread_attr_setstacksize(&attr, 536870912);
if (ret) {
fprintf(stderr, "pthread_attr_setstacksize: %s\n",
strerror(ret));
exit(1);
}
for (i = 0; i < nthreads; i++) {
ret = pthread_create(tid + i, &attr, handler, cookie);
if (ret) {
fprintf(stderr, "pthread_create: %s\n", strerror(ret));
exit(1);
}
}
ret = pthread_attr_destroy(&attr);
if (ret) {
fprintf(stderr, "pthread_attr_destroy: %s\n", strerror(ret));
exit(1);
}
for (i = 0; i < nthreads; i++) {
ret = pthread_join(tid[i], NULL);
if (ret) {
fprintf(stderr, "pthread_join: %s\n", strerror(ret));
exit(1);
}
}
}