1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2013 by Alexander V. Lukyanov (lav@yars.free.net)
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <config.h>
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <stddef.h>
26 #include "trio.h"
27 #include "History.h"
28 #include "url.h"
29 #include "misc.h"
30 
31 #define super KeyValueDB
32 
History()33 History::History()
34 {
35    full=0;
36    stamp=0;
37    fd=-1;
38    modified=false;
39    const char *home=get_lftp_data_dir();
40    if(home)
41       file.vset(home,"/cwd_history",NULL);
42 }
43 
~History()44 History::~History()
45 {
46    Close();
47    if(full)
48       delete full;
49 }
50 
extract_url(const char * res)51 const char *History::extract_url(const char *res)
52 {
53    const char *url=strchr(res,':');
54    if(url)
55       url++;
56    else
57       url=res;
58 
59    if(url::is_url(url))
60       return url;
61 
62    return url::decode(url);
63 }
64 
extract_stamp(const char * res)65 time_t History::extract_stamp(const char *res)
66 {
67    return atol(res);
68 }
69 
Lookup(const FileAccess * s)70 const char *History::Lookup(const FileAccess *s)
71 {
72    const char *url=s->GetConnectURL(s->NO_PATH|s->NO_PASSWORD);
73    if(!url)
74       return 0;
75    const char *res=super::Lookup(url);
76    if(res)
77       return extract_url(res);
78    Refresh();
79    Close();
80    if(!full)
81       return 0;
82    res=full->Lookup(url);
83    if(res)
84       return extract_url(res);
85    return 0;
86 }
87 
Refresh()88 void History::Refresh()
89 {
90    if(!file)
91       return;
92    struct stat st;
93    if((fd==-1 ? stat(file,&st) : fstat(fd,&st)) == -1)
94       return;
95    if(st.st_mtime==stamp)
96       return;
97    Load();
98 }
99 
Load()100 void History::Load()
101 {
102    if(full)
103       full->Empty();
104    if(!file)
105       return;
106    if(fd==-1)
107    {
108       fd=open(file,O_RDONLY);
109       if(fd==-1)
110 	 return;
111       fcntl(fd,F_SETFD,FD_CLOEXEC);
112       if(Lock(fd,F_RDLCK)==-1)
113 	 fprintf(stderr,"%s: lock for reading failed, trying to read anyway\n",file.get());
114    }
115    if(!full)
116       full=new KeyValueDB;
117    struct stat st;
118    fstat(fd,&st);
119    stamp=st.st_mtime;
120    lseek(fd,0,SEEK_SET);
121 
122    full->Read(dup(fd));	// Read	closes fd
123 }
124 
Close()125 void History::Close()
126 {
127    if(fd!=-1)
128    {
129       close(fd);
130       fd=-1;
131    }
132 }
133 
Set(const FileAccess * s,const FileAccess::Path & cwd)134 void History::Set(const FileAccess *s,const FileAccess::Path &cwd)
135 {
136    if(cwd.path==0 || !strcmp(cwd.path,"~") || s->GetHostName()==0)
137       return;
138    xstring res;
139    res.setf("%lu:",(unsigned long)time(0));
140    if(!cwd.url)
141    {
142       res.append_url_encoded(cwd,URL_PATH_UNSAFE);
143       if(!cwd.is_file && url::dir_needs_trailing_slash(s->GetProto()) && res.last_char()!='/')
144 	 res.append('/');
145    }
146    else
147       res.append(cwd.url);
148    super::Add(s->GetConnectURL(s->NO_PATH|s->NO_PASSWORD),res);
149    modified=true;
150 }
151 
Save()152 void History::Save()
153 {
154    Close();
155    if(!file)
156       return;
157    if(!modified)
158       return;
159    fd=open(file,O_RDWR|O_CREAT,0600);
160    if(fd==-1)
161       return;
162    fcntl(fd,F_SETFD,FD_CLOEXEC);
163    if(Lock(fd,F_WRLCK)==-1)
164    {
165       fprintf(stderr,"%s: lock for writing failed\n",file.get());
166       Close();
167       return;
168    }
169    Refresh();
170 
171    // merge
172    int count=0;
173    for(Pair *p=chain; p; p=p->next)
174    {
175       time_t new_stamp=extract_stamp(p->value);
176       time_t old_stamp=0;
177       const char *old_value=full->Lookup(p->key);
178       if(old_value)
179 	 old_stamp=extract_stamp(old_value);
180       if(old_stamp<new_stamp)
181       {
182 	 full->Add(p->key,p->value);
183       	 count++;
184       }
185    }
186 
187    if(count==0)
188    {
189       Close();
190       return;
191    }
192 
193    lseek(fd,0,SEEK_SET);
194 
195 #ifdef HAVE_FTRUNCATE
196    if(ftruncate(fd,0)==-1) // note the following statement
197 #endif
198    close(open(file,O_WRONLY|O_TRUNC));
199 
200    full->Write(fd);
201    fd=-1;   // Write closes file
202 }
203