static bfpath *bfpath_split(bfpath *bfp, const char *home) { /* precondition: bfp->dirname and bfp->filename free'd if need be */ char *t = strrchr(bfp->filepath, DIRSEP_C); xfree(bfp->dirname); xfree(bfp->filename); if (t != NULL) { /* if directory separator present .... */ *t = '\0'; bfp->dirname = xstrdup(bfp->filepath); *t = DIRSEP_C; bfp->filename = xstrdup(t+1); } else if (home != NULL){ bfp->dirname = xstrdup(home); bfp->filename = bfp->filepath; bfp->filepath = mxcat(bfp->dirname, DIRSEP_S, bfp->filename, NULL); } else { bfp->dirname = NULL; bfp->filename = xstrdup(bfp->filepath); } return bfp; }
void bfpath_set_filename(bfpath *bfp, const char *filename) { xfree(bfp->filename); bfp->filename = xstrdup(filename); xfree(bfp->filepath); bfp->filepath = mxcat(bfp->dirname, DIRSEP_S, bfp->filename, NULL); check_for_file(bfp); return; }
static int db_try_glock(bfpath *bfp, short locktype, int lockcmd) { int ret; char *t; /* lock */ ret = bf_mkdir(bfp->dirname, DIR_MODE); if (ret && errno != EEXIST) { print_error(__FILE__, __LINE__, "mkdir(%s): %s", bfp->dirname, strerror(errno)); exit(EX_ERROR); } t = mxcat(bfp->dirname, DIRSEP_S, "lockfile-d", NULL); /* All we are interested in is that this file exists, we'll close it * right away as plock down will open it again */ ret = open(t, O_RDWR|O_CREAT|O_EXCL, DS_MODE); if (ret < 0 && errno != EEXIST) { print_error(__FILE__, __LINE__, "open(%s): %s", t, strerror(errno)); exit(EX_ERROR); } if (ret >= 0) close(ret); lockfd = plock(t, locktype, lockcmd); if (lockfd < 0 && errno != EAGAIN && errno != EACCES) { print_error(__FILE__, __LINE__, "lock(%s): %s", t, strerror(errno)); exit(EX_ERROR); } xfree(t); /* lock set up */ return lockfd; }
int main(int argc, char **argv) /*@globals errno,stderr,stdout@*/ { ex_t exitcode = EX_OK; fBogotune = true; /* for rob_compute_spamicity() */ dbgout = stderr; progtype = build_progtype(progname, DB_TYPE); ham_files = filelist_new("ham"); spam_files = filelist_new("spam"); /* process args and read mailboxes */ process_arglist(argc, argv); /* directories from command line and config file are already handled */ if (ds_flag == DS_DSK) { bfpath *bfp; if (ds_path == NULL) ds_path = get_directory(PR_ENV_BOGO); if (ds_path == NULL) ds_path = get_directory(PR_ENV_HOME); if (ds_path == NULL) { fprintf(stderr, "Cannot derive bogofilter directory from environment, aborting.\n"); exit(EX_ERROR); } set_bogohome(ds_path); bfp = bfpath_create(ds_path); if (!bfpath_check_mode(bfp, BFP_MUST_EXIST)) { fprintf(stderr, "Can't open wordlist '%s'\n", bfp->filepath); exit(EX_ERROR); } if (bfp->exists && bfp->isdir) { bfpath_free(bfp); ds_path = mxcat(ds_path, DIRSEP_S, WORDLIST, NULL); bfp = bfpath_create(ds_path); if (!bfpath_check_mode(bfp, BFP_MUST_EXIST)) { fprintf(stderr, "Can't open wordlist '%s'\n", bfp->filepath); exit(EX_ERROR); } } env = ds_init(bfp); init_wordlist("word", ds_path, 0, WL_REGULAR); } bogotune_init(); if (ds_flag == DS_DSK) load_wordlist(load_hook, train); /* if encoding not yet set, assume old style */ if (encoding == E_UNKNOWN) encoding = E_RAW; if (bogolex_file != NULL) bogolex(); else bogotune(); bogotune_free(); if (ds_flag == DS_DSK) ds_cleanup(env); exit(exitcode); }