Permalink
Browse files

(get_lftp_{config,data,cache}_dir) new functions; use them.

  • Loading branch information...
lavv17 committed Jan 24, 2013
1 parent b3cad69 commit 21dc40022a12e5a9a0739b8bc14f64b1b8979001
Showing with 102 additions and 21 deletions.
  1. +6 −0 src/ChangeLog
  2. +1 −1 src/FileCopy.cc
  3. +1 −1 src/Torrent.cc
  4. +1 −1 src/attach.h
  5. +1 −1 src/bookmark.cc
  6. +1 −1 src/history.cc
  7. +2 −2 src/lftp.cc
  8. +2 −2 src/lftp_rl.c
  9. +83 −11 src/misc.cc
  10. +4 −1 src/misc.h
@@ -1,3 +1,9 @@
2013-01-24 Alexander V. Lukyanov <lav@yars.free.net>
* FileCopy.cc, Torrent.cc, attach.h, bookmark.cc, history.cc,
lftp.cc, lftp_rl.c, misc.cc, misc.h:
(get_lftp_{config,data,cache}_dir) new functions; use them.
2012-12-14 Alexander V. Lukyanov <lav@yars.free.net>
* CmdExec.cc, CmdExec.h, commands.cc, lftp.cc: make cmd:interactive tri-bool.
@@ -630,7 +630,7 @@ void FileCopy::LogTransfer()
{
const char *fname = ResMgr::Query("xfer:log-file", 0);
if(!fname || !*fname)
fname = dir_file(get_lftp_home(),"transfer_log");
fname = dir_file(get_lftp_data_dir(),"transfer_log");
int fd = open(fname,O_WRONLY|O_APPEND|O_CREAT,0600);
if(fd==-1)
return;
@@ -153,7 +153,7 @@ void Torrent::StartDHT()
StartListenerUDP();
const char *home=get_lftp_home();
const char *home=get_lftp_cache_dir();
const char *nodename=get_nodename();
mkdir(xstring::format("%s/DHT",home),0700);
@@ -141,7 +141,7 @@ class AcceptTermFD : public SMTask
static xstring& get_sock_path(int pid=0) {
if(!pid)
pid=getpid();
const char *home=get_lftp_home();
const char *home=get_lftp_data_dir();
mkdir(xstring::format("%s/bg",home),0700);
return xstring::format("%s/bg/%s-%d",home,get_nodename(),pid);
}
@@ -38,7 +38,7 @@ ResDecl res_auto_sync("bmk:auto-sync","yes",ResMgr::BoolValidate,ResMgr::NoClosu
Bookmark::Bookmark()
{
const char *home=get_lftp_home();
const char *home=get_lftp_data_dir();
if(home)
bm_file.vset(home,"/bookmarks",NULL);
bm_fd=-1;
@@ -38,7 +38,7 @@ History::History()
stamp=0;
fd=-1;
modified=false;
const char *home=get_lftp_home();
const char *home=get_lftp_data_dir();
if(home)
file.vset(home,"/cwd_history",NULL);
}
@@ -387,7 +387,7 @@ static void detach()
SignalHook::Ignore(SIGHUP);
SignalHook::Ignore(SIGTSTP);
const char *home=get_lftp_home();
const char *home=get_lftp_data_dir();
if(home)
{
xstring& log=xstring::get_tmp(home);
@@ -535,7 +535,7 @@ int main(int argc,char **argv)
const char *home=getenv("HOME");
if(home)
source_if_exist(dir_file(home,".lftprc"));
home=get_lftp_home();
home=get_lftp_config_dir();
if(home)
source_if_exist(dir_file(home,"rc"));
top_exec->WaitDone();
@@ -196,15 +196,15 @@ void lftp_rl_set_prompt(const char *p)
rl_set_prompt(p);
}
extern char *get_lftp_home();
extern char *get_lftp_data_dir();
static char *lftp_history_file;
void lftp_rl_read_history()
{
if(!lftp_history_file)
{
const char *add="/rl_history";
const char *home=get_lftp_home();
const char *home=get_lftp_data_dir();
if(!home)
return;
lftp_history_file=(char*)malloc(strlen(home)+strlen(add)+1);
@@ -32,6 +32,7 @@
#include <ctype.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <pwd.h>
#ifdef HAVE_TERMIOS_H
#include <termios.h>
@@ -769,32 +770,103 @@ bool temporary_network_error(int err)
return false;
}
const char *get_lftp_home()
const char *get_home()
{
static char *home=NULL;
if(home)
return home;
home=getenv("HOME");
if(home)
return home;
struct passwd *pw=getpwuid(getuid());
if(pw && pw->pw_dir)
return home=pw->pw_dir;
return NULL;
}
home=getenv("LFTP_HOME");
if(!home)
const char *get_lftp_home_nocreate()
{
static char *lftp_home=NULL;
if(lftp_home)
return *lftp_home?lftp_home:NULL;
lftp_home=getenv("LFTP_HOME");
if(!lftp_home)
{
home=getenv("HOME");
if(home)
home=xstring::cat(home,"/.lftp",NULL).borrow();
const char *h=get_home();
if(h)
lftp_home=xstring::cat(h,"/.lftp",NULL).borrow();
else
return NULL;
}
else
home=xstrdup(home);
lftp_home=xstrdup(lftp_home);
if(!*home)
return *lftp_home?lftp_home:NULL;
}
const char *get_lftp_home_if_exists()
{
const char *home=get_lftp_home_nocreate();
struct stat st;
if(stat(home,&st)==-1 || !S_ISDIR(st.st_mode))
return NULL;
mkdir(home,0755);
return home;
}
// new XDG directories
const char *get_lftp_dir(char *&cached_dir,const char *env,const char *def)
{
if(cached_dir)
return cached_dir;
// use old existing directory for compatibility
const char *dir=get_lftp_home_if_exists();
if(dir)
return cached_dir=xstrdup(dir);
// use explicit directory if specified, otherwise use default under home
const char *home=getenv(env);
if(home) {
// explicit XDG dir
(void)mkdir(home,0755);
dir=xstring::cat(home,"/lftp",NULL);
} else {
home=get_home();
if(!home)
return NULL;
xstring& path=xstring::get_tmp(home);
path.append('/');
const char *slash=strchr(def,'/');
if(slash) {
path.append(def,slash-def);
(void)mkdir(path,0755);
path.append(slash);
} else {
path.append(def);
}
(void)mkdir(path,0755);
dir=path.append("/lftp");
}
(void)mkdir(dir,0755);
return cached_dir=xstrdup(dir);
}
const char *get_lftp_config_dir()
{
static char *config_dir;
return get_lftp_dir(config_dir,"XDG_CONFIG_HOME",".config");
}
const char *get_lftp_data_dir()
{
static char *data_dir;
return get_lftp_dir(data_dir,"XDG_DATA_HOME",".local/share");
}
const char *get_lftp_cache_dir()
{
static char *cache_dir;
return get_lftp_dir(cache_dir,"XDG_CACHE_HOME",".cache");
}
const char *memrchr(const char *buf,char c,size_t len)
{
buf+=len;
@@ -113,7 +113,10 @@ void base64_encode (const char *s, char *store, int length);
bool temporary_network_error(int e);
CDECL const char *get_lftp_home();
CDECL const char *get_home();
CDECL const char *get_lftp_config_dir();
CDECL const char *get_lftp_data_dir();
CDECL const char *get_lftp_cache_dir();
const char *memrchr(const char *buf,char c,size_t len);
static inline char *memrchr(char *buf,char c,size_t len) {

0 comments on commit 21dc400

Please sign in to comment.