styling fixes
This commit is contained in:
@@ -45,10 +45,10 @@ static pthread_mutex_t dir_mx = PTHREAD_MUTEX_INITIALIZER;
|
|||||||
|
|
||||||
static void push_dir(const char *path) {
|
static void push_dir(const char *path) {
|
||||||
DirJob *j = malloc(sizeof *j);
|
DirJob *j = malloc(sizeof *j);
|
||||||
if (j == NULL)
|
if(j == NULL)
|
||||||
return;
|
return;
|
||||||
j->path = strdup(path);
|
j->path = strdup(path);
|
||||||
if (j->path == NULL) {
|
if(j->path == NULL) {
|
||||||
free(j);
|
free(j);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -66,10 +66,10 @@ static void die(const char *msg) {
|
|||||||
static char *pop_dir(void) {
|
static char *pop_dir(void) {
|
||||||
pthread_mutex_lock(&dir_mx);
|
pthread_mutex_lock(&dir_mx);
|
||||||
DirJob *j = dir_stack;
|
DirJob *j = dir_stack;
|
||||||
if (j != NULL)
|
if(j != NULL)
|
||||||
dir_stack = j->next;
|
dir_stack = j->next;
|
||||||
pthread_mutex_unlock(&dir_mx);
|
pthread_mutex_unlock(&dir_mx);
|
||||||
if (j == NULL)
|
if(j == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
char *p = j->path;
|
char *p = j->path;
|
||||||
free(j);
|
free(j);
|
||||||
@@ -88,14 +88,14 @@ static int scan_fd(int fd, ScanBufs *sb) {
|
|||||||
size_t carryn = 0;
|
size_t carryn = 0;
|
||||||
int hit = 0;
|
int hit = 0;
|
||||||
|
|
||||||
for (;;) {
|
for(;;) {
|
||||||
ssize_t r = read(fd, buf + carryn, IO_BUFSZ);
|
ssize_t r = read(fd, buf + carryn, IO_BUFSZ);
|
||||||
if (r < 0) {
|
if(r < 0) {
|
||||||
if (errno == EINTR)
|
if(errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (r == 0)
|
if(r == 0)
|
||||||
break; /* EOF */
|
break; /* EOF */
|
||||||
size_t wn = carryn + (size_t)r;
|
size_t wn = carryn + (size_t)r;
|
||||||
|
|
||||||
@@ -105,17 +105,17 @@ static int scan_fd(int fd, ScanBufs *sb) {
|
|||||||
* before the first NUL it is still a valid text match. */
|
* before the first NUL it is still a valid text match. */
|
||||||
void *nul = memchr(buf, '\0', wn);
|
void *nul = memchr(buf, '\0', wn);
|
||||||
size_t text_end = (nul != NULL) ? (size_t)((const char *)nul - buf) : wn;
|
size_t text_end = (nul != NULL) ? (size_t)((const char *)nul - buf) : wn;
|
||||||
if (text_end >= needle_len) {
|
if(text_end >= needle_len) {
|
||||||
if (memmem(buf, text_end, needle, needle_len) != NULL) {
|
if(memmem(buf, text_end, needle, needle_len) != NULL) {
|
||||||
hit = 1;
|
hit = 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (nul != NULL)
|
if(nul != NULL)
|
||||||
break; /* binary file: no further scanning */
|
break; /* binary file: no further scanning */
|
||||||
|
|
||||||
/* No NUL and no match yet: carry the tail across chunks. */
|
/* No NUL and no match yet: carry the tail across chunks. */
|
||||||
if (wn >= needle_len)
|
if(wn >= needle_len)
|
||||||
carryn = needle_len - 1;
|
carryn = needle_len - 1;
|
||||||
else
|
else
|
||||||
carryn = wn;
|
carryn = wn;
|
||||||
@@ -135,9 +135,9 @@ static void report_match(const char *path) {
|
|||||||
|
|
||||||
static void scan_file(const char *path, ScanBufs *sb) {
|
static void scan_file(const char *path, ScanBufs *sb) {
|
||||||
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
int fd = open(path, O_RDONLY | O_CLOEXEC);
|
||||||
if (fd < 0)
|
if(fd < 0)
|
||||||
return; /* unreadable entries are silently skipped */
|
return; /* unreadable entries are silently skipped */
|
||||||
if (scan_fd(fd, sb))
|
if(scan_fd(fd, sb))
|
||||||
report_match(path);
|
report_match(path);
|
||||||
close(fd);
|
close(fd);
|
||||||
}
|
}
|
||||||
@@ -148,39 +148,39 @@ static void *worker(void *arg) {
|
|||||||
ScanBufs sb;
|
ScanBufs sb;
|
||||||
sb.buf = malloc(IO_BUFSZ + needle_len);
|
sb.buf = malloc(IO_BUFSZ + needle_len);
|
||||||
sb.carry = malloc(needle_len);
|
sb.carry = malloc(needle_len);
|
||||||
if (sb.buf == NULL || sb.carry == NULL) {
|
if(sb.buf == NULL || sb.carry == NULL) {
|
||||||
free(sb.buf);
|
free(sb.buf);
|
||||||
free(sb.carry);
|
free(sb.carry);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
char child[PATH_MAX];
|
char child[PATH_MAX];
|
||||||
for (;;) {
|
for(;;) {
|
||||||
char *dir = pop_dir();
|
char *dir = pop_dir();
|
||||||
if (dir == NULL)
|
if(dir == NULL)
|
||||||
break;
|
break;
|
||||||
DIR *d = opendir(dir);
|
DIR *d = opendir(dir);
|
||||||
if (d != NULL) {
|
if(d != NULL) {
|
||||||
struct dirent *ent;
|
struct dirent *ent;
|
||||||
while ((ent = readdir(d)) != NULL) {
|
while((ent = readdir(d)) != NULL) {
|
||||||
if (strcmp(ent->d_name, ".") == 0 ||
|
if(strcmp(ent->d_name, ".") == 0 ||
|
||||||
strcmp(ent->d_name, "..") == 0)
|
strcmp(ent->d_name, "..") == 0)
|
||||||
continue;
|
continue;
|
||||||
int n = snprintf(child, sizeof child, "%s/%s", dir, ent->d_name);
|
int n = snprintf(child, sizeof child, "%s/%s", dir, ent->d_name);
|
||||||
if (n < 0 || (size_t)n >= sizeof child)
|
if(n < 0 || (size_t)n >= sizeof child)
|
||||||
continue;
|
continue;
|
||||||
if (ent->d_type == DT_REG) {
|
if(ent->d_type == DT_REG) {
|
||||||
scan_file(child, &sb);
|
scan_file(child, &sb);
|
||||||
} else if (ent->d_type == DT_DIR) {
|
} else if(ent->d_type == DT_DIR) {
|
||||||
push_dir(child);
|
push_dir(child);
|
||||||
} else if (ent->d_type == DT_LNK) {
|
} else if(ent->d_type == DT_LNK) {
|
||||||
continue; /* do not follow symlinks: avoids cycles */
|
continue; /* do not follow symlinks: avoids cycles */
|
||||||
} else {
|
} else {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (lstat(child, &st) != 0)
|
if(lstat(child, &st) != 0)
|
||||||
continue;
|
continue;
|
||||||
if (S_ISDIR(st.st_mode))
|
if(S_ISDIR(st.st_mode))
|
||||||
push_dir(child);
|
push_dir(child);
|
||||||
else if (S_ISREG(st.st_mode))
|
else if(S_ISREG(st.st_mode))
|
||||||
scan_file(child, &sb);
|
scan_file(child, &sb);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -194,39 +194,39 @@ static void *worker(void *arg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
if (argc < 2 || argc > 3) {
|
if(argc < 2 || argc > 3) {
|
||||||
fprintf(stderr, "usage: %s SEARCH_TERM [FILE_OR_DIRECTORY]\n", argv[0]);
|
fprintf(stderr, "usage: %s SEARCH_TERM [FILE_OR_DIRECTORY]\n", argv[0]);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
needle = argv[1];
|
needle = argv[1];
|
||||||
needle_len = strlen(needle);
|
needle_len = strlen(needle);
|
||||||
if (needle_len == 0) {
|
if(needle_len == 0) {
|
||||||
fprintf(stderr, "gg: empty search term\n");
|
fprintf(stderr, "gg: empty search term\n");
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
char cwd[PATH_MAX];
|
char cwd[PATH_MAX];
|
||||||
const char *root;
|
const char *root;
|
||||||
if (argc == 3) {
|
if(argc == 3) {
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(argv[2], &st) != 0) {
|
if(stat(argv[2], &st) != 0) {
|
||||||
fprintf(stderr, "gg: %s: %s\n", argv[2], strerror(errno));
|
fprintf(stderr, "gg: %s: %s\n", argv[2], strerror(errno));
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) {
|
if(!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) {
|
||||||
fprintf(stderr, "gg: %s: not a regular file or directory\n", argv[2]);
|
fprintf(stderr, "gg: %s: not a regular file or directory\n", argv[2]);
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
root = argv[2];
|
root = argv[2];
|
||||||
} else {
|
} else {
|
||||||
if (getcwd(cwd, sizeof cwd) == NULL)
|
if(getcwd(cwd, sizeof cwd) == NULL)
|
||||||
die("getcwd");
|
die("getcwd");
|
||||||
root = cwd;
|
root = cwd;
|
||||||
}
|
}
|
||||||
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
if (stat(root, &st) != 0) {
|
if(stat(root, &st) != 0) {
|
||||||
fprintf(stderr, "gg: %s: %s\n", root, strerror(errno));
|
fprintf(stderr, "gg: %s: %s\n", root, strerror(errno));
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
@@ -235,7 +235,7 @@ int main(int argc, char **argv) {
|
|||||||
* parallel: each worker pops a directory, scans its regular files inline,
|
* parallel: each worker pops a directory, scans its regular files inline,
|
||||||
* and pushes any sub-directories it finds. This overlaps the (metadata)
|
* and pushes any sub-directories it finds. This overlaps the (metadata)
|
||||||
* walk with the (data) scan across all online CPUs. */
|
* walk with the (data) scan across all online CPUs. */
|
||||||
if (S_ISREG(st.st_mode)) {
|
if(S_ISREG(st.st_mode)) {
|
||||||
scan_file(root, &(ScanBufs){ malloc(IO_BUFSZ + needle_len),
|
scan_file(root, &(ScanBufs){ malloc(IO_BUFSZ + needle_len),
|
||||||
malloc(needle_len) });
|
malloc(needle_len) });
|
||||||
} else {
|
} else {
|
||||||
@@ -243,20 +243,20 @@ int main(int argc, char **argv) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
long nproc = sysconf(_SC_NPROCESSORS_ONLN);
|
long nproc = sysconf(_SC_NPROCESSORS_ONLN);
|
||||||
if (nproc < 1)
|
if(nproc < 1)
|
||||||
nproc = 1;
|
nproc = 1;
|
||||||
int nth = (int)nproc;
|
int nth = (int)nproc;
|
||||||
if (nth > 256)
|
if(nth > 256)
|
||||||
nth = 256;
|
nth = 256;
|
||||||
|
|
||||||
pthread_t t[256];
|
pthread_t t[256];
|
||||||
int started = 0;
|
int started = 0;
|
||||||
for (int i = 0; i < nth; i++) {
|
for(int i = 0; i < nth; i++) {
|
||||||
if (pthread_create(&t[i], NULL, worker, NULL) != 0)
|
if(pthread_create(&t[i], NULL, worker, NULL) != 0)
|
||||||
break;
|
break;
|
||||||
started++;
|
started++;
|
||||||
}
|
}
|
||||||
for (int i = 0; i < started; i++)
|
for(int i = 0; i < started; i++)
|
||||||
pthread_join(t[i], NULL);
|
pthread_join(t[i], NULL);
|
||||||
|
|
||||||
return found_any ? 0 : 1;
|
return found_any ? 0 : 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user