From: Pierre Habouzit Date: Mon, 28 May 2007 09:52:52 +0000 (+0200) Subject: Have a centralized cache directory. X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=da04d632587a221112a0f065e4802e1ea5393e21;p=apps%2Fmadmutt.git Have a centralized cache directory. No more header_cache and nntp_cache_dir and whatever else. Everything is now in .cache/madmutt/. Signed-off-by: Pierre Habouzit --- diff --git a/globals.h b/globals.h index bdb44d5..46473fc 100644 --- a/globals.h +++ b/globals.h @@ -47,7 +47,6 @@ WHERE char *Locale; WHERE char *Maildir; #ifdef USE_HCACHE -WHERE char *HeaderCache; #if defined(HAVE_GDBM) WHERE char *HeaderCachePageSize; #endif /* HAVE_GDBM */ @@ -74,7 +73,6 @@ WHERE char *MixEntryFormat; WHERE char *Muttrc INITVAL (NULL); #ifdef USE_NNTP -WHERE char *NewsCacheDir; WHERE char *GroupFormat; WHERE char *NewsServer; WHERE char *NntpUser; diff --git a/imap/message.c b/imap/message.c index 62f4b21..275d84c 100644 --- a/imap/message.c +++ b/imap/message.c @@ -96,7 +96,7 @@ int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend) idata->newMailCount = 0; #ifdef USE_HCACHE - if ((hc = mutt_hcache_open (HeaderCache, ctx->path))) { + if ((hc = mutt_hcache_open(ctx->path))) { snprintf (buf, sizeof (buf), "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1); diff --git a/init.h b/init.h index 63fffbe..6f008bc 100644 --- a/init.h +++ b/init.h @@ -884,19 +884,6 @@ struct option_t MuttVars[] = { ** maximum line length is determined by the terminal width and $$wrapmargin. */ #ifdef USE_HCACHE - {"header_cache", DT_PATH, R_NONE, UL &HeaderCache, "~/.cache/madmutt/" }, - /* - ** .pp - ** Availability: Header Cache - ** - ** .pp - ** The $$header_cache variable points to the header cache database. - ** .pp - ** If $$header_cache points to a directory it will contain a header cache - ** database per folder. If $$header_cache points to a file that file will - ** be a single global header cache. By default it is \fIunset\fP so no - ** header caching will be used. - */ #if defined(HAVE_GDBM) {"header_cache_pagesize", DT_STR, R_NONE, UL &HeaderCachePageSize, "16384"}, /* @@ -2135,19 +2122,6 @@ struct option_t MuttVars[] = { ** does not support posting to that newsgroup or a group is totally read-only, that ** posting will not have any effect. */ - {"nntp_cache_dir", DT_PATH, R_NONE, UL &NewsCacheDir, "~/.cache/madmutt/"}, - /* - ** .pp - ** Availability: NNTP - ** - ** .pp - ** This variable points to directory where Madmutt will cache news - ** article headers. If \fIunset\fP, headers will not be saved at all - ** and will be reloaded each time when you enter a newsgroup. - ** .pp - ** As for the header caching in connection with IMAP and/or Maildir, - ** this drastically increases speed and lowers traffic. - */ {"nntp_host", DT_STR, R_NONE, UL &NewsServer, "" }, /* ** .pp diff --git a/lib-lua/lua-token.gperf b/lib-lua/lua-token.gperf index b223dc1..b0e47a4 100644 --- a/lib-lua/lua-token.gperf +++ b/lib-lua/lua-token.gperf @@ -123,6 +123,7 @@ exit 0 ## beep_new ## bindir ## ca_certificates_file +## cachedir ## cert_file ## charset ## docdir diff --git a/lib-lua/madmutt.cpkg b/lib-lua/madmutt.cpkg index 7752312..87c5c26 100644 --- a/lib-lua/madmutt.cpkg +++ b/lib-lua/madmutt.cpkg @@ -24,6 +24,7 @@ #include #include +#include #include #include "../mutt.h" @@ -81,6 +82,37 @@ static char *madmutt_init_hostname(void) return m_strdup(NONULL(mod_core.shorthost)); } +static void madmutt_update_cachedir(const char *dir) +{ + static char *cachedir = NULL; + char path[_POSIX_PATH_MAX]; + char buf[HUGE_STRING]; + struct stat st; + + _mutt_expand_path(path, sizeof(path), dir, 0); + if (lstat(path, &st) || !S_ISDIR(st.st_mode)) { + snprintf(buf, sizeof(buf), _("Directory %s not exist. Create it?"), + dir); + if (mutt_yesorno(buf, M_YES) == M_YES) + mkdir(path, 0750); + } + + if (lstat(path, &st) || !S_ISDIR(st.st_mode)) { + mutt_error(_("Cache directory not created!")); + return; + } + if (st.st_mode & 0027) { + snprintf(buf, sizeof(buf), + _("Directory %s is unsafe, do you want to use it ?"), dir); + if (mutt_yesorno(buf, M_YES) != M_YES) + return; + } + + m_strreplace(&cachedir, path); + mod_core.cachedir = cachedir; +} + + #if defined(HAVE_QDBM) # define HCACHE_BACKEND "qdbm" #elif defined(HAVE_GDBM) @@ -91,6 +123,12 @@ static char *madmutt_init_hostname(void) # define HCACHE_BACKEND NULL #endif +static void mod_core_init2(void) +{ + madmutt_update_cachedir("~/.cache/madmutt"); +} +#define mod_core_init() do { (mod_core_init)(); mod_core_init2(); } while (0) + @package mod_core { /* ** .pp @@ -273,6 +311,13 @@ static char *madmutt_init_hostname(void) RETURN(p ? p + 1 : (CurrentFolder ?: "")); } }; + + + const string_t cachedir = NULL; + void setcachedir(const string_t path) { + madmutt_update_cachedir(path); + RETURN(); + }; }; @package MTransport { diff --git a/lib-mx/hcache.c b/lib-mx/hcache.c index e59e43a..c3fca8d 100644 --- a/lib-mx/hcache.c +++ b/lib-mx/hcache.c @@ -375,20 +375,20 @@ HEADER *mutt_hcache_restore(const void *_d, HEADER **oh) /* }}} */ -hcache_t *mutt_hcache_open(const char *path, const char *folder) +hcache_t *mutt_hcache_open(const char *folder) { - hcache_t *h = p_new(hcache_t, 1); + const char *path; + hcache_t *h; - h->folder = m_strdup(folder); - h->crc = generate_crc32(); - - if (m_strisempty(path)) { - p_delete(&h->folder); - p_delete(&h); + if (m_strisempty(mod_core.cachedir)) { return NULL; } - path = mutt_hcache_per_folder(path, folder); + h = p_new(hcache_t, 1); + h->folder = m_strdup(folder); + h->crc = generate_crc32(); + + path = mutt_hcache_per_folder(mod_core.cachedir, folder); { #if defined(HAVE_QDBM) diff --git a/lib-mx/hcache.h b/lib-mx/hcache.h index 8df2400..a3495c3 100644 --- a/lib-mx/hcache.h +++ b/lib-mx/hcache.h @@ -14,7 +14,7 @@ #ifdef USE_HCACHE typedef struct hcache_t hcache_t; -hcache_t *mutt_hcache_open(const char *path, const char *folder); +hcache_t *mutt_hcache_open(const char *folder); void mutt_hcache_close(hcache_t **db); HEADER *mutt_hcache_restore(const void *d, HEADER **oh); diff --git a/lib-mx/mh.c b/lib-mx/mh.c index 0d04ea9..f60cfe5 100644 --- a/lib-mx/mh.c +++ b/lib-mx/mh.c @@ -730,7 +730,7 @@ static void maildir_delayed_parsing (CONTEXT * ctx, struct maildir *md) struct stat lastchanged; int ret; - hc = mutt_hcache_open (HeaderCache, ctx->path); + hc = mutt_hcache_open(ctx->path); #endif for (p = md, count = 0; p; p = p->next, count++) { @@ -1242,7 +1242,7 @@ static int mh_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), #ifdef USE_HCACHE if (ctx->magic == M_MAILDIR) - hc = mutt_hcache_open (HeaderCache, ctx->path); + hc = mutt_hcache_open(ctx->path); #endif /* USE_HCACHE */ for (i = 0; i < ctx->msgcount; i++) { diff --git a/nntp.c b/nntp.c index 4fc321e..604b5d1 100644 --- a/nntp.c +++ b/nntp.c @@ -134,44 +134,29 @@ static int slurp_newsrc (nntp_server_t * news) } #define nntp_cache_expand(dst, dlen, fmt, ...) \ - do { \ - snprintf((dst), (dlen), "%s/" fmt, NewsCacheDir, ##__VA_ARGS__); \ - mutt_expand_path((dst), (dlen)); \ + do { \ + snprintf((dst), (dlen), "%s/" fmt, mod_core.cachedir, ##__VA_ARGS__);\ + mutt_expand_path((dst), (dlen)); \ } while (0) /* Loads $news_cache_dir/.index into memory, loads newsserver data * and newsgroup cache names */ static int nntp_parse_cacheindex(nntp_server_t *news) { - struct stat st; - char buf[HUGE_STRING], *cp; - char dir[_POSIX_PATH_MAX], file[_POSIX_PATH_MAX]; - FILE *idx; nntp_data_t *data; + char file[_POSIX_PATH_MAX], buf[HUGE_STRING], *cp; + FILE *idx; int l, m, t; - unset_option (OPTNEWSCACHE); - if (m_strisempty(NewsCacheDir)) + if (m_strisempty(mod_core.cachedir)) { + unset_option(OPTNEWSCACHE); return 0; - - m_strcpy(dir, sizeof(dir), NewsCacheDir); - mutt_expand_path(dir, sizeof(dir)); - - if (lstat (dir, &st) || (st.st_mode & S_IFDIR) == 0) { - snprintf(buf, sizeof(buf), _("Directory %s not exist. Create it?"), dir); - if (mutt_yesorno (buf, M_YES) != M_YES - || mkdir (dir, (S_IRWXU + S_IRWXG + S_IRWXO))) { - mutt_error _("Cache directory not created!"); - - return -1; - } - mutt_clear_error (); + } else { + set_option(OPTNEWSCACHE); } - set_option (OPTNEWSCACHE); - p_delete(&news->cache); - snprintf(buf, sizeof (buf), "%s/%s.index", dir, news->conn->account.host); + nntp_cache_expand(buf, sizeof(buf), "%s.index", news->conn->account.host); if (!(idx = safe_fopen (buf, "a+"))) return 0; rewind (idx);