static int grep_file(struct grep_opt *opt, const char *filename) { struct strbuf buf = STRBUF_INIT; char *name; if (opt->relative && opt->prefix_length) quote_path_relative(filename, -1, &buf, opt->prefix); else strbuf_addstr(&buf, filename); name = strbuf_detach(&buf, NULL); #ifndef NO_PTHREADS if (use_threads) { grep_file_async(opt, name, filename); return 0; } else #endif { int hit; size_t sz; void *data = load_file(filename, &sz); if (!data) hit = 0; else hit = grep_buffer(opt, name, data, sz); free(data); free(name); return hit; } }
static int grep_file(struct grep_opt *opt, const char *filename) { struct stat st; int i; char *data; size_t sz; if (lstat(filename, &st) < 0) { err_ret: if (errno != ENOENT) error("'%s': %s", filename, strerror(errno)); return 0; } if (!st.st_size) return 0; /* empty file -- no grep hit */ if (!S_ISREG(st.st_mode)) return 0; sz = xsize_t(st.st_size); i = open(filename, O_RDONLY); if (i < 0) goto err_ret; data = xmalloc(sz + 1); if (st.st_size != read_in_full(i, data, sz)) { error("'%s': short read %s", filename, strerror(errno)); close(i); free(data); return 0; } close(i); if (opt->relative && opt->prefix_length) filename += opt->prefix_length; i = grep_buffer(opt, filename, data, sz); free(data); return i; }
static int commit_match(struct commit *commit, struct rev_info *opt) { if (!opt->grep_filter.pattern_list) return 1; return grep_buffer(&opt->grep_filter, NULL, /* we say nothing, not even filename */ commit->buffer, strlen(commit->buffer)); }
static void *run(void *arg) { int hit = 0; struct grep_opt *opt = arg; while (1) { struct work_item *w = get_work(); if (!w) break; if (skip_binary(opt, (const char *)w->identifier)) continue; opt->output_priv = w; if (w->type == WORK_SHA1) { unsigned long sz; void* data = load_sha1(w->identifier, &sz, w->name); if (data) { hit |= grep_buffer(opt, w->name, data, sz); free(data); } } else if (w->type == WORK_FILE) { size_t sz; void* data = load_file(w->identifier, &sz); if (data) { hit |= grep_buffer(opt, w->name, data, sz); free(data); } } else { assert(0); } work_done(w); } free_grep_patterns(arg); free(arg); return (void*) (intptr_t) hit; }
static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *filename, int tree_name_len) { struct strbuf pathbuf = STRBUF_INIT; char *name; if (opt->relative && opt->prefix_length) { quote_path_relative(filename + tree_name_len, -1, &pathbuf, opt->prefix); strbuf_insert(&pathbuf, 0, filename, tree_name_len); } else { strbuf_addstr(&pathbuf, filename); } name = strbuf_detach(&pathbuf, NULL); #ifndef NO_PTHREADS if (use_threads) { grep_sha1_async(opt, name, sha1); return 0; } else #endif { int hit; unsigned long sz; void *data = load_sha1(sha1, &sz, name); if (!data) hit = 0; else hit = grep_buffer(opt, name, data, sz); free(data); free(name); return hit; } }
static int grep_sha1(struct grep_opt *opt, const unsigned char *sha1, const char *name, int tree_name_len) { unsigned long size; char *data; enum object_type type; char *to_free = NULL; int hit; data = read_sha1_file(sha1, &type, &size); if (!data) { error("'%s': unable to read %s", name, sha1_to_hex(sha1)); return 0; } if (opt->relative && opt->prefix_length) { static char name_buf[PATH_MAX]; char *cp; int name_len = strlen(name) - opt->prefix_length + 1; if (!tree_name_len) name += opt->prefix_length; else { if (ARRAY_SIZE(name_buf) <= name_len) cp = to_free = xmalloc(name_len); else cp = name_buf; memcpy(cp, name, tree_name_len); strcpy(cp + tree_name_len, name + tree_name_len + opt->prefix_length); name = cp; } } hit = grep_buffer(opt, name, data, size); free(data); free(to_free); return hit; }