/* * Mbuni - Open Source MMS Gateway * * MMSBox Dlr: Dlr storage for MMSBox * * Copyright (C) 2003 - 2005, Digital Solutions Ltd. - http://www.dsmagic.com * * Paul Bagyenda * * This program is free software, distributed under the terms of * the GNU General Public License, with a few exceptions granted (see LICENSE) */ #include #include #include #include #include #include #include #include #include #include "mmsbox.h" #include "mms_queue.h" #define MAXTRIES 10 static int dlr_entry_fname(char *msgid, char *rtype, Octstr *mmc_id, Octstr **efname) { char d1[2], d2[3], fbuf[512], *p; unsigned long h = _mshash(msgid); Octstr *t, *s; int i, fd; /* Make toplevel dir. */ d1[0] = _TT[h%_TTSIZE]; d1[1] = '\0'; /* Then lower level. */ h /= _TTSIZE; d2[0] = _TT[h%_TTSIZE]; h /= _TTSIZE; d2[1] = _TT[h%_TTSIZE]; d2[2] = '\0'; /* Try and create the next level dir (first level was created by root_init) */ sprintf(fbuf, "%s/%s/%s", octstr_get_cstr(dlr_dir), d1, d2); if (mkdir(fbuf, S_IRWXU|S_IRWXG) < 0 && errno != EEXIST) { error(0, "mmsbox: failed to create dir [%s] " "while initialising dlr dir for %s: %s!", fbuf, msgid, strerror(errno)); return -1; } t = octstr_format("%S-%s_%s", mmc_id, msgid, rtype); /* Put mmc id into name. */ octstr_replace(t, octstr_imm("/"), octstr_imm("$")); /* XXX safe in all cases?? */ s = octstr_format("%s/%S", fbuf, t); octstr_destroy(t); p = octstr_get_cstr(s); i = 0; do if ((fd = open(p, O_RDWR|O_CREAT, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP)) < 0) { error(0, "Failed to open DLR URL store file [%s], error: %s!", p, strerror(errno)); break; } else if (mm_lockfile(fd, p, 1) != 0) { close(fd); fd = -1; } while (i++ < MAXTRIES && fd < 0); if (efname) *efname = s; else octstr_destroy(s); return fd; } void mms_dlr_url_put(Octstr *msgid, char *rtype, Octstr *mmc_id, Octstr *dlr_url) { int fd = dlr_entry_fname(octstr_get_cstr(msgid), rtype, mmc_id, NULL); if (fd >= 0) { octstr_write_data(dlr_url, fd, 0); close(fd); } } Octstr *mms_dlr_url_get(Octstr *msgid, char *rtype, Octstr *mmc_id) { int fd = dlr_entry_fname(octstr_get_cstr(msgid), rtype, mmc_id, NULL); FILE *f; if (fd >= 0 && (f = fdopen(fd, "r+")) != NULL) { Octstr *s = octstr_read_pipe(f); fclose(f); if (s && octstr_len(s) == 0) { octstr_destroy(s); return NULL; } else return s; } else if (fd >= 0) close(fd); return NULL; } void mms_dlr_url_remove(Octstr *msgid, char *rtype, Octstr *mmc_id) { Octstr *fname = NULL; int fd = dlr_entry_fname(octstr_get_cstr(msgid), rtype, mmc_id, &fname); if (fname) unlink(octstr_get_cstr(fname)); if (fd >= 0) close(fd); }