char *xrindex(const char *s, int c) { #ifdef NO_POSIX_SYSTEM return xstrrchr(s, c); #else return rindex(fix(s), c); #endif }
// dopack copies the package src to dst, // appending the files listed in extra. // The archive format is the traditional Unix ar format. static void dopack(char *dst, char *src, char **extra, int nextra) { int i; char c, *p, *q; Buf b, bdst; binit(&b); binit(&bdst); readfile(&bdst, src); for(i=0; i<nextra; i++) { readfile(&b, extra[i]); // find last path element for archive member name p = xstrrchr(extra[i], '/'); if(p) p++; q = xstrrchr(extra[i], '\\'); if(q) { q++; if(p == nil || q > p) p = q; } if(p == nil) p = extra[i]; bwritef(&bdst, "%-16.16s%-12d%-6d%-6d%-8o%-10d`\n", p, 0, 0, 0, 0644, b.len); bwriteb(&bdst, &b); if(b.len&1) { c = 0; bwrite(&bdst, &c, 1); } } writefile(&bdst, dst, 0); bfree(&b); bfree(&bdst); }
// matchfield reports whether the field matches this build. static bool matchfield(char *f) { char *p; bool res; p = xstrrchr(f, ','); if(p == nil) return streq(f, goos) || streq(f, goarch) || streq(f, "cmd_go_bootstrap") || streq(f, "go1.1"); *p = 0; res = matchfield(f) && matchfield(p+1); *p = ','; return res; }
// install installs the library, package, or binary associated with dir, // which is relative to $GOROOT/src. static void install(char *dir) { char *name, *p, *elem, *prefix, *exe; bool islib, ispkg, isgo, stale, ispackcmd; Buf b, b1, path; Vec compile, files, link, go, missing, clean, lib, extra; Time ttarg, t; int i, j, k, n, doclean, targ; if(vflag) { if(!streq(goos, gohostos) || !streq(goarch, gohostarch)) errprintf("%s (%s/%s)\n", dir, goos, goarch); else errprintf("%s\n", dir); } binit(&b); binit(&b1); binit(&path); vinit(&compile); vinit(&files); vinit(&link); vinit(&go); vinit(&missing); vinit(&clean); vinit(&lib); vinit(&extra); // path = full path to dir. bpathf(&path, "%s/src/%s", goroot, dir); name = lastelem(dir); // For misc/prof, copy into the tool directory and we're done. if(hasprefix(dir, "misc/")) { copy(bpathf(&b, "%s/%s", tooldir, name), bpathf(&b1, "%s/misc/%s", goroot, name), 1); goto out; } // For release, cmd/prof is not included. if((streq(dir, "cmd/prof")) && !isdir(bstr(&path))) { if(vflag > 1) errprintf("skipping %s - does not exist\n", dir); goto out; } // set up gcc command line on first run. if(gccargs.len == 0) { bprintf(&b, "%s %s", defaultcc, defaultcflags); splitfields(&gccargs, bstr(&b)); for(i=0; i<nelem(proto_gccargs); i++) vadd(&gccargs, proto_gccargs[i]); if(defaultcflags[0] == '\0') { for(i=0; i<nelem(proto_gccargs2); i++) vadd(&gccargs, proto_gccargs2[i]); } if(contains(gccargs.p[0], "clang")) { // disable ASCII art in clang errors, if possible vadd(&gccargs, "-fno-caret-diagnostics"); // clang is too smart about unused command-line arguments vadd(&gccargs, "-Qunused-arguments"); } // disable word wrapping in error messages vadd(&gccargs, "-fmessage-length=0"); if(streq(gohostos, "darwin")) { // golang.org/issue/5261 vadd(&gccargs, "-mmacosx-version-min=10.6"); } } if(ldargs.len == 0 && defaultldflags[0] != '\0') { bprintf(&b, "%s", defaultldflags); splitfields(&ldargs, bstr(&b)); } islib = hasprefix(dir, "lib") || streq(dir, "cmd/cc") || streq(dir, "cmd/gc"); ispkg = hasprefix(dir, "pkg"); isgo = ispkg || streq(dir, "cmd/go") || streq(dir, "cmd/cgo"); exe = ""; if(streq(gohostos, "windows")) exe = ".exe"; // Start final link command line. // Note: code below knows that link.p[targ] is the target. ispackcmd = 0; if(islib) { // C library. vadd(&link, "ar"); if(streq(gohostos, "plan9")) vadd(&link, "rc"); else vadd(&link, "rsc"); prefix = ""; if(!hasprefix(name, "lib")) prefix = "lib"; targ = link.len; vadd(&link, bpathf(&b, "%s/pkg/obj/%s_%s/%s%s.a", goroot, gohostos, gohostarch, prefix, name)); } else if(ispkg) { // Go library (package). ispackcmd = 1; vadd(&link, "pack"); // program name - unused here, but all the other cases record one p = bprintf(&b, "%s/pkg/%s_%s/%s", goroot, goos, goarch, dir+4); *xstrrchr(p, '/') = '\0'; xmkdirall(p); targ = link.len; vadd(&link, bpathf(&b, "%s/pkg/%s_%s/%s.a", goroot, goos, goarch, dir+4)); } else if(streq(dir, "cmd/go") || streq(dir, "cmd/cgo")) { // Go command. vadd(&link, bpathf(&b, "%s/%sl", tooldir, gochar)); vadd(&link, "-o"); elem = name; if(streq(elem, "go")) elem = "go_bootstrap"; targ = link.len; vadd(&link, bpathf(&b, "%s/%s%s", tooldir, elem, exe)); } else { // C command. Use gccargs and ldargs. if(streq(gohostos, "plan9")) { vadd(&link, bprintf(&b, "%sl", gohostchar)); vadd(&link, "-o"); targ = link.len; vadd(&link, bpathf(&b, "%s/%s", tooldir, name)); } else { vcopy(&link, gccargs.p, gccargs.len); vcopy(&link, ldargs.p, ldargs.len); if(sflag) vadd(&link, "-static"); vadd(&link, "-o"); targ = link.len; vadd(&link, bpathf(&b, "%s/%s%s", tooldir, name, exe)); if(streq(gohostarch, "amd64")) vadd(&link, "-m64"); else if(streq(gohostarch, "386")) vadd(&link, "-m32"); } } ttarg = mtime(link.p[targ]); // Gather files that are sources for this target. // Everything in that directory, and any target-specific // additions. xreaddir(&files, bstr(&path)); // Remove files beginning with . or _, // which are likely to be editor temporary files. // This is the same heuristic build.ScanDir uses. // There do exist real C files beginning with _, // so limit that check to just Go files. n = 0; for(i=0; i<files.len; i++) { p = files.p[i]; if(hasprefix(p, ".") || (hasprefix(p, "_") && hassuffix(p, ".go"))) xfree(p); else files.p[n++] = p; } files.len = n; for(i=0; i<nelem(deptab); i++) { if(streq(dir, deptab[i].prefix) || (hassuffix(deptab[i].prefix, "/") && hasprefix(dir, deptab[i].prefix))) { for(j=0; (p=deptab[i].dep[j])!=nil; j++) { breset(&b1); bwritestr(&b1, p); bsubst(&b1, "$GOROOT", goroot); bsubst(&b1, "$GOOS", goos); bsubst(&b1, "$GOARCH", goarch); p = bstr(&b1); if(hassuffix(p, ".a")) { vadd(&lib, bpathf(&b, "%s", p)); continue; } if(hassuffix(p, "/*")) { bpathf(&b, "%s/%s", bstr(&path), p); b.len -= 2; xreaddir(&extra, bstr(&b)); bprintf(&b, "%s", p); b.len -= 2; for(k=0; k<extra.len; k++) vadd(&files, bpathf(&b1, "%s/%s", bstr(&b), extra.p[k])); continue; } if(hasprefix(p, "-")) { p++; n = 0; for(k=0; k<files.len; k++) { if(hasprefix(files.p[k], p)) xfree(files.p[k]); else files.p[n++] = files.p[k]; } files.len = n; continue; } vadd(&files, p); } } } vuniq(&files); // Convert to absolute paths. for(i=0; i<files.len; i++) { if(!isabs(files.p[i])) { bpathf(&b, "%s/%s", bstr(&path), files.p[i]); xfree(files.p[i]); files.p[i] = btake(&b); } } // Is the target up-to-date? stale = rebuildall; n = 0; for(i=0; i<files.len; i++) { p = files.p[i]; for(j=0; j<nelem(depsuffix); j++) if(hassuffix(p, depsuffix[j])) goto ok; xfree(files.p[i]); continue; ok: t = mtime(p); if(t != 0 && !hassuffix(p, ".a") && !shouldbuild(p, dir)) { xfree(files.p[i]); continue; } if(hassuffix(p, ".go")) vadd(&go, p); if(t > ttarg) stale = 1; if(t == 0) { vadd(&missing, p); files.p[n++] = files.p[i]; continue; } files.p[n++] = files.p[i]; } files.len = n; // If there are no files to compile, we're done. if(files.len == 0) goto out; for(i=0; i<lib.len && !stale; i++) if(mtime(lib.p[i]) > ttarg) stale = 1; if(!stale) goto out; // For package runtime, copy some files into the work space. if(streq(dir, "pkg/runtime")) { copy(bpathf(&b, "%s/arch_GOARCH.h", workdir), bpathf(&b1, "%s/arch_%s.h", bstr(&path), goarch), 0); copy(bpathf(&b, "%s/defs_GOOS_GOARCH.h", workdir), bpathf(&b1, "%s/defs_%s_%s.h", bstr(&path), goos, goarch), 0); p = bpathf(&b1, "%s/signal_%s_%s.h", bstr(&path), goos, goarch); if(isfile(p)) copy(bpathf(&b, "%s/signal_GOOS_GOARCH.h", workdir), p, 0); copy(bpathf(&b, "%s/os_GOOS.h", workdir), bpathf(&b1, "%s/os_%s.h", bstr(&path), goos), 0); copy(bpathf(&b, "%s/signals_GOOS.h", workdir), bpathf(&b1, "%s/signals_%s.h", bstr(&path), goos), 0); } // Generate any missing files; regenerate existing ones. for(i=0; i<files.len; i++) { p = files.p[i]; elem = lastelem(p); for(j=0; j<nelem(gentab); j++) { if(gentab[j].gen == nil) continue; if(hasprefix(elem, gentab[j].nameprefix)) { if(vflag > 1) errprintf("generate %s\n", p); gentab[j].gen(bstr(&path), p); // Do not add generated file to clean list. // In pkg/runtime, we want to be able to // build the package with the go tool, // and it assumes these generated files already // exist (it does not know how to build them). // The 'clean' command can remove // the generated files. goto built; } } // Did not rebuild p. if(find(p, missing.p, missing.len) >= 0) fatal("missing file %s", p); built:; } // One more copy for package runtime. // The last batch was required for the generators. // This one is generated. if(streq(dir, "pkg/runtime")) { copy(bpathf(&b, "%s/zasm_GOOS_GOARCH.h", workdir), bpathf(&b1, "%s/zasm_%s_%s.h", bstr(&path), goos, goarch), 0); } // Generate .c files from .goc files. if(streq(dir, "pkg/runtime")) { for(i=0; i<files.len; i++) { p = files.p[i]; if(!hassuffix(p, ".goc")) continue; // b = path/zp but with _goos_goarch.c instead of .goc bprintf(&b, "%s%sz%s", bstr(&path), slash, lastelem(p)); b.len -= 4; bwritef(&b, "_%s_%s.c", goos, goarch); goc2c(p, bstr(&b)); vadd(&files, bstr(&b)); } vuniq(&files); } if((!streq(goos, gohostos) || !streq(goarch, gohostarch)) && isgo) { // We've generated the right files; the go command can do the build. if(vflag > 1) errprintf("skip build for cross-compile %s\n", dir); goto nobuild; } // Compile the files. for(i=0; i<files.len; i++) { if(!hassuffix(files.p[i], ".c") && !hassuffix(files.p[i], ".s")) continue; name = lastelem(files.p[i]); vreset(&compile); if(!isgo) { // C library or tool. if(streq(gohostos, "plan9")) { vadd(&compile, bprintf(&b, "%sc", gohostchar)); vadd(&compile, "-FTVwp"); vadd(&compile, "-DPLAN9"); vadd(&compile, "-D__STDC__=1"); vadd(&compile, "-D__SIZE_TYPE__=ulong"); // for GNU Bison vadd(&compile, bpathf(&b, "-I%s/include/plan9", goroot)); vadd(&compile, bpathf(&b, "-I%s/include/plan9/%s", goroot, gohostarch)); } else { vcopy(&compile, gccargs.p, gccargs.len); vadd(&compile, "-c"); if(streq(gohostarch, "amd64")) vadd(&compile, "-m64"); else if(streq(gohostarch, "386")) vadd(&compile, "-m32"); vadd(&compile, "-I"); vadd(&compile, bpathf(&b, "%s/include", goroot)); } if(streq(dir, "lib9")) vadd(&compile, "-DPLAN9PORT"); vadd(&compile, "-I"); vadd(&compile, bstr(&path)); // lib9/goos.c gets the default constants hard-coded. if(streq(name, "goos.c")) { vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOOS=\"%s\"", goos)); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOARCH=\"%s\"", goarch)); bprintf(&b1, "%s", goroot_final); bsubst(&b1, "\\", "\\\\"); // turn into C string vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOROOT=\"%s\"", bstr(&b1))); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOVERSION=\"%s\"", goversion)); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOARM=\"%s\"", goarm)); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GO386=\"%s\"", go386)); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GO_EXTLINK_ENABLED=\"%s\"", goextlinkenabled)); } // gc/lex.c records the GOEXPERIMENT setting used during the build. if(streq(name, "lex.c")) { xgetenv(&b, "GOEXPERIMENT"); vadd(&compile, "-D"); vadd(&compile, bprintf(&b1, "GOEXPERIMENT=\"%s\"", bstr(&b))); } } else { // Supporting files for a Go package. if(hassuffix(files.p[i], ".s")) vadd(&compile, bpathf(&b, "%s/%sa", tooldir, gochar)); else { vadd(&compile, bpathf(&b, "%s/%sc", tooldir, gochar)); vadd(&compile, "-F"); vadd(&compile, "-V"); vadd(&compile, "-w"); } vadd(&compile, "-I"); vadd(&compile, workdir); vadd(&compile, "-I"); vadd(&compile, bprintf(&b, "%s/pkg/%s_%s", goroot, goos, goarch)); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOOS_%s", goos)); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOARCH_%s", goarch)); vadd(&compile, "-D"); vadd(&compile, bprintf(&b, "GOOS_GOARCH_%s_%s", goos, goarch)); } bpathf(&b, "%s/%s", workdir, lastelem(files.p[i])); doclean = 1; if(!isgo && streq(gohostos, "darwin")) { // To debug C programs on OS X, it is not enough to say -ggdb // on the command line. You have to leave the object files // lying around too. Leave them in pkg/obj/, which does not // get removed when this tool exits. bpathf(&b1, "%s/pkg/obj/%s", goroot, dir); xmkdirall(bstr(&b1)); bpathf(&b, "%s/%s", bstr(&b1), lastelem(files.p[i])); doclean = 0; } // Change the last character of the output file (which was c or s). if(streq(gohostos, "plan9")) b.p[b.len-1] = gohostchar[0]; else b.p[b.len-1] = 'o'; vadd(&compile, "-o"); vadd(&compile, bstr(&b)); vadd(&compile, files.p[i]); bgrunv(bstr(&path), CheckExit, &compile); vadd(&link, bstr(&b)); if(doclean) vadd(&clean, bstr(&b)); } bgwait(); if(isgo) { // The last loop was compiling individual files. // Hand the Go files to the compiler en masse. vreset(&compile); vadd(&compile, bpathf(&b, "%s/%sg", tooldir, gochar)); bpathf(&b, "%s/_go_.a", workdir); vadd(&compile, "-pack"); vadd(&compile, "-o"); vadd(&compile, bstr(&b)); vadd(&clean, bstr(&b)); if(!ispackcmd) vadd(&link, bstr(&b)); vadd(&compile, "-p"); if(hasprefix(dir, "pkg/")) vadd(&compile, dir+4); else vadd(&compile, "main"); if(streq(dir, "pkg/runtime")) vadd(&compile, "-+"); vcopy(&compile, go.p, go.len); runv(nil, bstr(&path), CheckExit, &compile); if(ispackcmd) { xremove(link.p[targ]); dopack(link.p[targ], bstr(&b), &link.p[targ+1], link.len - (targ+1)); goto nobuild; } } if(!islib && !isgo) { // C binaries need the libraries explicitly, and -lm. vcopy(&link, lib.p, lib.len); if(!streq(gohostos, "plan9")) vadd(&link, "-lm"); } // Remove target before writing it. xremove(link.p[targ]); runv(nil, nil, CheckExit, &link); nobuild: // In package runtime, we install runtime.h and cgocall.h too, // for use by cgo compilation. if(streq(dir, "pkg/runtime")) { copy(bpathf(&b, "%s/pkg/%s_%s/cgocall.h", goroot, goos, goarch), bpathf(&b1, "%s/src/pkg/runtime/cgocall.h", goroot), 0); copy(bpathf(&b, "%s/pkg/%s_%s/runtime.h", goroot, goos, goarch), bpathf(&b1, "%s/src/pkg/runtime/runtime.h", goroot), 0); } out: for(i=0; i<clean.len; i++) xremove(clean.p[i]); bfree(&b); bfree(&b1); bfree(&path); vfree(&compile); vfree(&files); vfree(&link); vfree(&go); vfree(&missing); vfree(&clean); vfree(&lib); vfree(&extra); }
/* when cgroups are configured with cpuset, at least * cpuset.cpus and cpuset.mems must be set or the cgroup * will not be available at all. * we duplicate the ancestor configuration in the init step */ static int _xcgroup_cpuset_init(xcgroup_t* cg) { int fstatus, i; char* cpuset_metafiles[] = { "cpus", "mems" }; char* cpuset_conf = NULL; size_t csize = 0; xcgroup_t acg; char* acg_name = NULL; char* p; fstatus = XCGROUP_ERROR; /* load ancestor cg */ acg_name = (char*) xstrdup(cg->name); p = xstrrchr(acg_name, '/'); if (p == NULL) { debug2("system cgroup: unable to get ancestor path for " "cpuset cg '%s' : %m", cg->path); xfree(acg_name); return fstatus; } else *p = '\0'; if (xcgroup_load(cg->ns, &acg, acg_name) != XCGROUP_SUCCESS) { debug2("system cgroup: unable to load ancestor for " "cpuset cg '%s' : %m", cg->path); xfree(acg_name); return fstatus; } xfree(acg_name); /* inherits ancestor params */ for (i = 0 ; i < 2 ; i++) { again: snprintf(cpuset_meta, sizeof(cpuset_meta), "%s%s", cpuset_prefix, cpuset_metafiles[i]); if (xcgroup_get_param(&acg ,cpuset_meta, &cpuset_conf, &csize) != XCGROUP_SUCCESS) { if (!cpuset_prefix_set) { cpuset_prefix_set = 1; cpuset_prefix = "cpuset."; goto again; } debug("system cgroup: assuming no cpuset cg " "support for '%s'",acg.path); xcgroup_destroy(&acg); return fstatus; } if (csize > 0) cpuset_conf[csize-1] = '\0'; if (xcgroup_set_param(cg,cpuset_meta, cpuset_conf) != XCGROUP_SUCCESS) { debug("system cgroup: unable to write %s configuration " "(%s) for cpuset cg '%s'",cpuset_meta, cpuset_conf, cg->path); xcgroup_destroy(&acg); xfree(cpuset_conf); return fstatus; } xfree(cpuset_conf); } xcgroup_destroy(&acg); return XCGROUP_SUCCESS; }
/* items == -1 display all */ static int logs_buffer_raw_display(const char *file, int items) { struct buffer **bs = NULL; struct buffer *b; char *beg = NULL, *profile = NULL, *sesja = NULL, *target = NULL; int item = 0; int i; session_t *s; window_t *w; if (!file) return -1; if (!items) return 0; /* i'm weird, let's search for logs/__internal__ than check if there are smth after it... & check if there are two '/' */ if ((beg = xstrstr(file, "logs/__internal__")) && xstrlen(beg) > 19 && xstrchr(beg+18, '/') && xstrchr(beg+18, '/') != xstrrchr(beg+18, '/')) { profile = beg + 18; sesja = xstrchr(profile, '/')+1; target = xstrchr(sesja, '/')+1; } debug("[logs_buffer_raw_display()] profile: 0x%x sesja: 0x%x target: 0x%x\n", profile, sesja, target); if (!profile || !sesja || !target) return -1; profile = (xstrcmp(target, "_default_")) ? xstrndup(profile, sesja-profile-1) : NULL; sesja = (xstrcmp(target, "_null_")) ? xstrndup(sesja, target-sesja-1) : NULL; target = xstrdup(target); debug("[logs_buffer_raw_display()] profile: %s sesja: %s target: %s\n", __(profile), __(sesja), __(target)); /* search for session+window */ s = session_find(sesja); w = window_find_s(s, target); debug("[logs_buffer_raw_display()] s:0x%x; w:0x%x;\n", s, w); if (!w) w = window_current; if (w) w->lock++; for (b = buffer_lograw.data; b; b = b->next) { if (!xstrcmp(b->target, file)) { /* we asume that (b->ts < (b->next)->ts, it's quite correct unless other plugin do this trick... */ if (items == -1) { logs_print_window(s, w, b->line, b->ts); } else { bs = (struct buffer **) xrealloc(bs, (item+2) * sizeof(struct buffer *)); bs[item + 1] = NULL; bs[item] = b; } item++; } } if (bs) for (i = item < items ? 0 : item-items; i < item; i++) logs_print_window(s, w, bs[i]->line, bs[i]->ts); if (w) { w->lock--; query_emit(NULL, "ui-window-refresh"); } xfree(bs); xfree(profile); xfree(sesja); xfree(target); return item; }
static void add_repo(const char *base, struct strbuf *path, repo_config_fn fn) { struct stat st; struct passwd *pwd; size_t pathlen; struct strbuf rel = STRBUF_INIT; char *p, *slash; int n; size_t size; if (stat(path->buf, &st)) { fprintf(stderr, "Error accessing %s: %s (%d)\n", path->buf, strerror(errno), errno); return; } strbuf_addch(path, '/'); pathlen = path->len; if (ctx.cfg.strict_export) { strbuf_addstr(path, ctx.cfg.strict_export); if(stat(path->buf, &st)) return; strbuf_setlen(path, pathlen); } strbuf_addstr(path, "noweb"); if (!stat(path->buf, &st)) return; strbuf_setlen(path, pathlen); if (!starts_with(path->buf, base)) strbuf_addbuf(&rel, path); else strbuf_addstr(&rel, path->buf + strlen(base) + 1); if (!strcmp(rel.buf + rel.len - 5, "/.git")) strbuf_setlen(&rel, rel.len - 5); else if (rel.len && rel.buf[rel.len - 1] == '/') strbuf_setlen(&rel, rel.len - 1); repo = cgit_add_repo(rel.buf); config_fn = fn; if (ctx.cfg.enable_git_config) { strbuf_addstr(path, "config"); git_config_from_file(gitconfig_config, path->buf, NULL); strbuf_setlen(path, pathlen); } if (ctx.cfg.remove_suffix) { size_t urllen; strip_suffix(repo->url, ".git", &urllen); strip_suffix_mem(repo->url, &urllen, "/"); repo->url[urllen] = '\0'; } repo->path = xstrdup(path->buf); while (!repo->owner) { if ((pwd = getpwuid(st.st_uid)) == NULL) { fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", path->buf, strerror(errno), errno); break; } if (pwd->pw_gecos) if ((p = strchr(pwd->pw_gecos, ','))) *p = '\0'; repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); } if (repo->desc == cgit_default_repo_desc || !repo->desc) { strbuf_addstr(path, "description"); if (!stat(path->buf, &st)) readfile(path->buf, &repo->desc, &size); strbuf_setlen(path, pathlen); } if (ctx.cfg.section_from_path) { n = ctx.cfg.section_from_path; if (n > 0) { slash = rel.buf - 1; while (slash && n && (slash = strchr(slash + 1, '/'))) n--; } else { slash = rel.buf + rel.len; while (slash && n && (slash = xstrrchr(rel.buf, slash - 1, '/'))) n++; } if (slash && !n) { *slash = '\0'; repo->section = xstrdup(rel.buf); *slash = '/'; if (starts_with(repo->name, repo->section)) { repo->name += strlen(repo->section); if (*repo->name == '/') repo->name++; } } } strbuf_addstr(path, "cgitrc"); if (!stat(path->buf, &st)) parse_configfile(path->buf, &repo_config); strbuf_release(&rel); }
static void add_repo(const char *base, const char *path, repo_config_fn fn) { struct stat st; struct passwd *pwd; char *rel, *p, *slash; int n; size_t size; if (stat(path, &st)) { fprintf(stderr, "Error accessing %s: %s (%d)\n", path, strerror(errno), errno); return; } if (ctx.cfg.strict_export && stat(fmt("%s/%s", path, ctx.cfg.strict_export), &st)) return; if (!stat(fmt("%s/noweb", path), &st)) return; if (base == path) rel = xstrdup(fmt("%s", path)); else rel = xstrdup(fmt("%s", path + strlen(base) + 1)); if (!strcmp(rel + strlen(rel) - 5, "/.git")) rel[strlen(rel) - 5] = '\0'; repo = cgit_add_repo(rel); config_fn = fn; if (ctx.cfg.enable_git_config) git_config_from_file(gitconfig_config, fmt("%s/config", path), NULL); if (ctx.cfg.remove_suffix) if ((p = strrchr(repo->url, '.')) && !strcmp(p, ".git")) *p = '\0'; repo->path = xstrdup(path); while (!repo->owner) { if ((pwd = getpwuid(st.st_uid)) == NULL) { fprintf(stderr, "Error reading owner-info for %s: %s (%d)\n", path, strerror(errno), errno); break; } if (pwd->pw_gecos) if ((p = strchr(pwd->pw_gecos, ','))) *p = '\0'; repo->owner = xstrdup(pwd->pw_gecos ? pwd->pw_gecos : pwd->pw_name); } if (repo->desc == cgit_default_repo_desc || !repo->desc) { p = fmt("%s/description", path); if (!stat(p, &st)) readfile(p, &repo->desc, &size); } if (!repo->readme) { p = fmt("%s/README.html", path); if (!stat(p, &st)) repo->readme = "README.html"; } if (ctx.cfg.section_from_path) { n = ctx.cfg.section_from_path; if (n > 0) { slash = rel; while (slash && n && (slash = strchr(slash, '/'))) n--; } else { slash = rel + strlen(rel); while (slash && n && (slash = xstrrchr(rel, slash, '/'))) n++; } if (slash && !n) { *slash = '\0'; repo->section = xstrdup(rel); *slash = '/'; if (!prefixcmp(repo->name, repo->section)) { repo->name += strlen(repo->section); if (*repo->name == '/') repo->name++; } } } p = fmt("%s/cgitrc", path); if (!stat(p, &st)) parse_configfile(xstrdup(p), &repo_config); free(rel); }
static int xmsg_handle_file(session_t *s, const char *fn) { const int nounlink = !session_int_get(s, "unlink_sent"); const int utb = session_int_get(s, "unlink_toobig"); const int maxfs = session_int_get(s, "max_filesize"); const char *dfsuffix = session_get(s, "dotfile_suffix"); char *namesep = (char*) session_get(s, "name_separator"); char *dir; int dirlen; char *msg = NULL; int err, fs; time_t ft = 0; if (*fn == '.') /* we're skipping ALL dotfiles */ return -1; dir = (char*) xmsg_dirfix(session_uid_get(s)+XMSG_UID_DIROFFSET); dirlen = xstrlen(dir); /* first check if buffer is long enough to fit the whole path for dotfile */ if (strlcpy(dir+dirlen+1, fn, PATH_MAX-dirlen-2-xstrlen(dfsuffix)) >= PATH_MAX-dirlen-2-xstrlen(dfsuffix)) xerr("Buffer too small for: fn = %s, len(fn) = %d, dirlen = %d, dfsuffixlen = %d", fn, xstrlen(fn), dirlen, xstrlen(dfsuffix)); /* then fill in middle part of path */ dir[dirlen] = '/'; /* and take a much closer look the file */ xdebug("s = %s, d = %s, fn = %s", session_uid_get(s), dir, fn); if ((err = xmsg_checkoutfile(dir, &msg, &fs, &ft, maxfs))) { if (err == EFBIG) { print((utb ? "xmsg_toobigrm" : "xmsg_toobig"), fn, session_name(s)); if (utb) { unlink(dir); return -1; } /* else we need to create the dotfile first */ } else if (err != ENOENT && err != EINVAL) return -1; } else if (!nounlink && (utb == (err == EFBIG))) unlink(dir); /* here: dir = dotf */ memmove(dir+dirlen+2, dir+dirlen+1, xstrlen(dir) - dirlen); dir[dirlen+1] = '.'; xstrcpy(dir+xstrlen(dir), dfsuffix); /* we've already checked whether it fits */ { struct stat st; int r; if (nounlink || !utb) { r = !(stat(dir, &st) || S_ISDIR(st.st_mode)); } else r = 0; if (err == ENOENT) { if (r) /* clean up stale dotfile */ unlink(dir); xfree(msg); return -1; } else if (r) { xfree(msg); /* XXX: I think that we rather shouldn't first read, then check if it is needed, at least for nounlink mode */ return -1; } else if ((nounlink && !(utb && err == EFBIG)) || (!utb && err == EFBIG)) close(open(dir, O_WRONLY|O_CREAT|O_TRUNC|O_NOFOLLOW, 0600)); } if (err == EFBIG) return -1; else if (err == EINVAL) xdebug("empty file, not submitting"); else { char *uid = xmalloc(strlen(fn) + 6); char *msgx = NULL; { const char *charset = session_get(s, "charset"); if (charset && (msgx = ekg_convert_string(msg, charset, NULL))) xfree(msg); else msgx = msg; } xstrcpy(uid, "xmsg:"); xstrcat(uid, fn); if (namesep) { char *p, *q = NULL; for (p = namesep; *p; p++) { char *r = xstrrchr(uid+XMSG_UID_DIROFFSET, *p); if (r > q) q = r; } if (q) *q = '\0'; } protocol_message_emit(s, uid, NULL, msgx, NULL, ft, EKG_MSGCLASS_CHAT, NULL, EKG_TRY_BEEP, 0); xfree(msgx); xfree(uid); } return 0; }