void handle_dir (char * path, int depth) { DIR * target; struct dirent * entry; char child[PATH_MAX]; execute(path, pred_head); if (depth >= options.maxdepth) return; target = opendir(path); if (!target) { fprintf(stderr, "error accessing %s\n", path); return; } while ((entry = readdir(target))) { dbprintf("read: %s\n", entry->d_name); if (!strcmp(entry->d_name, "..")) continue; if (!strcmp(entry->d_name, ".")) continue; if ((snprintf(child, PATH_MAX, "%s/%s", path, entry->d_name)) >= PATH_MAX) { fprintf(stderr, "filename %s too long\n", entry->d_name); continue; } switch (entry->d_type) { case DT_DIR: queue_dir(child, ++depth); break; case DT_REG: queue_file(child); break; case DT_LNK: queue_file(child); break; default: dbprintf("not handled: %s\n", child); break; } } closedir(target); return; }
static int do_dcc_send(int idx, char *dir, char *fn, char *nick) { char s[161], s1[161]; FILE *f; int x; Context; if (strlen(nick) > NICKMAX) nick[NICKMAX] = 0; if (dccdir[0] == 0) { dprintf(idx, "DCC file transfers not supported.\n"); putlog(LOG_FILES, "*", "Refused dcc get %s from [%s]", fn, dcc[idx].nick); return 0; } if (strchr(fn, '/') != NULL) { dprintf(idx, "Filename cannot have '/' in it...\n"); putlog(LOG_FILES, "*", "Refused dcc get %s from [%s]", fn, dcc[idx].nick); return 0; } if (dir[0]) sprintf(s, "%s%s/%s", dccdir, dir, fn); else sprintf(s, "%s%s", dccdir, fn); f = fopen(s, "r"); if (f == NULL) { dprintf(idx, "No such file.\n"); putlog(LOG_FILES, "*", "Refused dcc get %s from [%s]", fn, dcc[idx].nick); return 0; } fclose(f); if (!nick[0]) nick = dcc[idx].nick; /* already have too many transfers active for this user? queue it */ if (at_limit(nick)) { char xxx[1024]; sprintf(xxx, "%d*%s%s", strlen(dccdir), dccdir, dir); queue_file(xxx, fn, dcc[idx].nick, nick); dprintf(idx, "Queued: %s to %s\n", fn, nick); return 1; } if (copy_to_tmp) { /* copy this file to /tmp */ sprintf(s, "%s%s%s%s", dccdir, dir, dir[0] ? "/" : "", fn); sprintf(s1, "%s%s", tempdir, fn); if (copyfile(s, s1) != 0) { dprintf(idx, "Can't make temporary copy of file!\n"); putlog(LOG_FILES | LOG_MISC, "*", "Refused dcc get %s: copy to %s FAILED!", fn, tempdir); return 0; } } else sprintf(s1, "%s%s%s%s", dccdir, dir, dir[0] ? "/" : "", fn); sprintf(s, "%s%s%s", dir, dir[0] ? "/" : "", fn); x = _dcc_send(idx, s1, nick, s); if (x != DCCSEND_OK) wipe_tmp_filename(s1, -1); return x; }
int main(int argc, char *argv[]) { #if EV_MULTIPLICITY lem_loop = ev_default_loop(LEM_LOOPFLAGS); if (lem_loop == NULL) { #else if (!ev_default_loop(LEM_LOOPFLAGS)) { #endif lem_log_error("lem: error initializing event loop"); return EXIT_FAILURE; } if (setsignal(SIGPIPE, SIG_IGN, 0) #if !EV_CHILD_ENABLE || setsignal(SIGCHLD, SIG_DFL, SA_NOCLDSTOP | SA_NOCLDWAIT) #endif ) goto error; /* create main Lua state */ L = luaL_newstate(); if (L == NULL) { lem_log_error("lem: error initializing Lua state"); goto error; } luaL_openlibs(L); /* push thread table */ lua_newtable(L); /* initialize runqueue */ ev_idle_init(&rq.w, runqueue_pop); ev_idle_start(LEM_ &rq.w); rq.queue = lem_xmalloc(LEM_INITIAL_QUEUESIZE * sizeof(struct lem_runqueue_slot)); rq.first = rq.last = 0; rq.mask = LEM_INITIAL_QUEUESIZE - 1; /* initialize threadpool */ if (pool_init()) { lem_log_error("lem: error initializing threadpool"); goto error; } /* load file */ if (queue_file(argc, argv, 1)) goto error; /* start the mainloop */ ev_loop(LEM_ 0); lem_debug("event loop exited"); /* if there is an error message left on L print it */ if (lua_type(L, -1) == LUA_TSTRING) lem_log_error("lem: %s", lua_tostring(L, -1)); /* shutdown Lua */ lua_close(L); /* free runqueue */ free(rq.queue); /* destroy loop */ #if EV_MULTIPLICITY ev_loop_destroy(lem_loop); #else ev_default_destroy(); #endif lem_debug("Bye %s", exit_status == EXIT_SUCCESS ? "o/" : ":("); return exit_status; error: if (L) lua_close(L); if (rq.queue) free(rq.queue); #if EV_MULTIPLICITY ev_loop_destroy(lem_loop); #else ev_default_destroy(); #endif return EXIT_FAILURE; }
static int do_dcc_send(int idx, char *dir, char *fn, char *nick, int resend) { char *s = NULL, *s1 = NULL; int x; if (nick && strlen(nick) > NICKMAX) nick[NICKMAX] = 0; if (dccdir[0] == 0) { dprintf(idx, "DCC file transfers not supported.\n"); putlog(LOG_FILES, "*", "Refused dcc %sget %s from [%s]", resend ? "re" : "", fn, dcc[idx].nick); return 0; } if (strchr(fn, '/') != NULL) { dprintf(idx, "Filename cannot have '/' in it...\n"); putlog(LOG_FILES, "*", "Refused dcc %sget %s from [%s]", resend ? "re" : "", fn, dcc[idx].nick); return 0; } if (dir[0]) { s = nmalloc(strlen(dccdir) + strlen(dir) + strlen(fn) + 2); sprintf(s, "%s%s/%s", dccdir, dir, fn); } else { s = nmalloc(strlen(dccdir) + strlen(fn) + 1); sprintf(s, "%s%s", dccdir, fn); } if (!file_readable(s)) { dprintf(idx, "No such file.\n"); putlog(LOG_FILES, "*", "Refused dcc %sget %s from [%s]", resend ? "re" : "", fn, dcc[idx].nick); my_free(s); return 0; } if (!nick || !nick[0]) nick = dcc[idx].nick; /* Already have too many transfers active for this user? queue it */ if (at_limit(nick)) { char xxx[1024]; sprintf(xxx, "%d*%s%s", (int) strlen(dccdir), dccdir, dir); queue_file(xxx, fn, dcc[idx].nick, nick); dprintf(idx, "Queued: %s to %s\n", fn, nick); my_free(s); return 1; } if (copy_to_tmp) { char *tempfn = mktempfile(fn); /* Copy this file to /tmp, add a random prefix to the filename. */ s = nrealloc(s, strlen(dccdir) + strlen(dir) + strlen(fn) + 2); sprintf(s, "%s%s%s%s", dccdir, dir, dir[0] ? "/" : "", fn); s1 = nrealloc(s1, strlen(tempdir) + strlen(tempfn) + 1); sprintf(s1, "%s%s", tempdir, tempfn); my_free(tempfn); if (copyfile(s, s1) != 0) { dprintf(idx, "Can't make temporary copy of file!\n"); putlog(LOG_FILES | LOG_MISC, "*", "Refused dcc %sget %s: copy to %s FAILED!", resend ? "re" : "", fn, tempdir); my_free(s); my_free(s1); return 0; } } else { s1 = nrealloc(s1, strlen(dccdir) + strlen(dir) + strlen(fn) + 2); sprintf(s1, "%s%s%s%s", dccdir, dir, dir[0] ? "/" : "", fn); } s = nrealloc(s, strlen(dir) + strlen(fn) + 2); sprintf(s, "%s%s%s", dir, dir[0] ? "/" : "", fn); x = _dcc_send(idx, s1, nick, s, resend); if (x != DCCSEND_OK) wipe_tmp_filename(s1, -1); my_free(s); my_free(s1); return x; }