commit 5d92917eeb6fbebc10115f26a122ecb5cae38e6a
parent 49e2c27ee21290a1ddb7035e648da6067485edc3
Author: ukai <ukai>
Date: Sat, 14 Dec 2002 15:18:37 +0000
[w3m-dev 03568] Re: preserve timestamp
* etc.c (USE_COOKIE): moved
* file.c (utime.h): include
(setModtime): added
(loadGeneralFile): set f.modtime
(_doFileCopy): setModtime()
(doFileSave): setModtime()
* ftp.c (getFtpModtime): added
(openFTP): pass URLFile, set modtime
* html.h (URLFile): add modtime
* proto.h (openFTP): arg URLFile *uf
(mymktime): always
* url.c (init_stream): initialize modtime
(openFTPStream): pass URLFile
(openURL): openFTPStream
From: Takahashi Youichirou <nikuq@hk.airnet.ne.jp>
Diffstat:
7 files changed, 85 insertions(+), 11 deletions(-)
diff --git a/ChangeLog b/ChangeLog
@@ -1,3 +1,21 @@
+2002-12-15 Takahashi Youichirou <nikuq@hk.airnet.ne.jp>
+
+ * [w3m-dev 03568] Re: preserve timestamp
+ * etc.c (USE_COOKIE): moved
+ * file.c (utime.h): include
+ (setModtime): added
+ (loadGeneralFile): set f.modtime
+ (_doFileCopy): setModtime()
+ (doFileSave): setModtime()
+ * ftp.c (getFtpModtime): added
+ (openFTP): pass URLFile, set modtime
+ * html.h (URLFile): add modtime
+ * proto.h (openFTP): arg URLFile *uf
+ (mymktime): always
+ * url.c (init_stream): initialize modtime
+ (openFTPStream): pass URLFile
+ (openURL): openFTPStream
+
2002-12-13 Hironori SAKAMOTO <hsaka@mth.biglobe.ne.jp>
* [w3m-dev 03567] default keybinding
diff --git a/etc.c b/etc.c
@@ -1518,7 +1518,6 @@ tmpfname(int type, char *ext)
return tmpf;
}
-#ifdef USE_COOKIE
static char *monthtbl[] = {
"Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
@@ -1728,6 +1727,7 @@ mymktime(char *timestr)
(hour * 60 * 60) + (min * 60) + sec);
}
+#ifdef USE_COOKIE
#ifdef INET6
#include <sys/socket.h>
#endif /* INET6 */
diff --git a/file.c b/file.c
@@ -11,6 +11,7 @@
#include <time.h>
#include <sys/stat.h>
#include <fcntl.h>
+#include <utime.h>
/* foo */
#include "html.h"
@@ -331,6 +332,19 @@ uncompressed_file_type(char *path, char **ext)
return t0;
}
+static int setModtime(char *path, time_t modtime)
+{
+ struct utimbuf t;
+ struct stat st;
+
+ if (stat(path, &st) == 0)
+ t.actime = st.st_atime;
+ else
+ t.actime = time(NULL);
+ t.modtime = modtime;
+ return utime(path, &t);
+}
+
void
examineFile(char *path, URLFile *uf)
{
@@ -1748,6 +1762,8 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer,
of = &f;
goto load_doc;
}
+
+ f.modtime = mymktime(checkHeader(t_buf, "Last-Modified:"));
}
#ifdef USE_NNTP
else if (pu.scheme == SCM_NEWS || pu.scheme == SCM_NNTP) {
@@ -1876,9 +1892,12 @@ loadGeneralFile(char *path, ParsedURL *volatile current, char *referer,
signal(SIGINT, prevtrap);
if (DecodeCTE && IStype(f.stream) != IST_ENCODED)
f.stream = newEncodedStream(f.stream, f.encoding);
- if (pu.scheme == SCM_LOCAL)
+ if (pu.scheme == SCM_LOCAL) {
+ struct stat st;
+ if (stat(pu.real_file, &st) == 0)
+ f.modtime = st.st_mtime;
file = conv_from_system(guess_save_name(NULL, pu.real_file));
- else
+ } else
file = guess_save_name(t_buf, pu.file);
doFileSave(f, file);
if (f.scheme == SCM_FTP)
@@ -7410,6 +7429,8 @@ _doFileCopy(char *tmpf, char *defstr, int download)
QuietMessage = TRUE;
fmInitialized = FALSE;
_MoveFile(tmpf, p);
+ if (stat(tmpf, &st) == 0)
+ setModtime(p, st.st_mtime);
unlink(lock);
exit(0);
}
@@ -7443,7 +7464,8 @@ _doFileCopy(char *tmpf, char *defstr, int download)
}
if (_MoveFile(tmpf, p) < 0) {
printf("Can't save to %s\n", p);
- }
+ } else if (stat(tmpf, &st) == 0)
+ setModtime(p, st.st_mtime);
}
}
@@ -7508,6 +7530,8 @@ doFileSave(URLFile uf, char *defstr)
QuietMessage = TRUE;
fmInitialized = FALSE;
save2tmp(uf, p);
+ if (uf.modtime != -1)
+ setModtime(p, uf.modtime);
UFclose(&uf);
unlink(lock);
exit(0);
@@ -7537,7 +7561,8 @@ doFileSave(URLFile uf, char *defstr)
}
if (save2tmp(uf, p) < 0) {
printf("Can't save to %s\n", p);
- }
+ } else if (uf.modtime != -1)
+ setModtime(p, uf.modtime);
}
}
diff --git a/ftp.c b/ftp.c
@@ -351,6 +351,34 @@ FtpData(FTP ftp, char *cmd, char *arg, char *mode)
return FtpDataBody(ftp, cmd, arg, mode);
}
+time_t
+getFtpModtime(FTP ftp, char *path)
+{
+ Str tmp;
+ char *p;
+ struct tm tm;
+ time_t t;
+
+ memset(&tm, 0, sizeof(struct tm));
+ tmp = Sprintf("MDTM %s\r\n", path);
+ fwrite(tmp->ptr, tmp->length, sizeof(char), ftp->wcontrol);
+ fflush(ftp->wcontrol);
+ tmp = read_response(ftp);
+ if (atoi(tmp->ptr) != 213)
+ return -1;
+ for (p = tmp->ptr + 4; *p && *p == ' '; p++)
+ ;
+ if (sscanf(p, "%04d%02d%02d%02d%02d%02d",
+ &tm.tm_year, &tm.tm_mon, &tm.tm_mday,
+ &tm.tm_hour, &tm.tm_min, &tm.tm_sec) < 6)
+ return -1;
+ tm.tm_year -= 1900;
+ tm.tm_mon--;
+ t = mktime(&tm);
+ t += mktime(localtime(&t)) - mktime(gmtime(&t));
+ return t;
+}
+
int
FtpClose(FTP ftp)
{
@@ -394,7 +422,7 @@ static int ftp_system(FTP);
#define FTPDIR_FILE 3
FILE *
-openFTP(ParsedURL *pu)
+openFTP(ParsedURL *pu, URLFile *uf)
{
Str tmp2 = Strnew();
Str tmp3 = Strnew();
@@ -472,6 +500,7 @@ openFTP(ParsedURL *pu)
goto ftp_dir;
/* Get file */
+ uf->modtime = getFtpModtime(current_ftp, realpathname);
FtpBinary(current_ftp);
if (ftp_pasv(current_ftp) < 0) {
FtpBye(current_ftp);
diff --git a/html.h b/html.h
@@ -73,6 +73,7 @@ typedef struct {
char *ssl_certificate;
#endif
char *url;
+ time_t modtime;
} URLFile;
#define CMP_NOCOMPRESS 0
diff --git a/proto.h b/proto.h
@@ -504,7 +504,7 @@ extern Str unquote_mailcap(char *qstr, char *type, char *name, char *attr,
extern char *guessContentType(char *filename);
extern TextList *make_domain_list(char *domain_list);
extern int check_no_proxy(char *domain);
-extern FILE *openFTP(ParsedURL *pu);
+extern FILE *openFTP(ParsedURL *pu, URLFile *uf);
extern Str readFTPDir(ParsedURL *pu);
extern void closeFTP(FILE * f);
extern int Ftpfclose(FILE * f);
@@ -588,8 +588,8 @@ extern Str myEditor(char *cmd, char *file, int line);
extern char *file_to_url(char *file);
extern char *expandName(char *name);
extern Str tmpfname(int type, char *ext);
-#ifdef USE_COOKIE
extern time_t mymktime(char *timestr);
+#ifdef USE_COOKIE
extern char *FQDN(char *host);
extern Str find_cookie(ParsedURL *pu);
extern int add_cookie(ParsedURL *pu, Str name, Str value, time_t expires,
diff --git a/url.c b/url.c
@@ -1412,12 +1412,13 @@ init_stream(URLFile *uf, int scheme, InputStream stream)
uf->compression = 0;
uf->guess_type = NULL;
uf->ext = NULL;
+ uf->modtime = -1;
}
static InputStream
-openFTPStream(ParsedURL *pu)
+openFTPStream(ParsedURL *pu, URLFile *uf)
{
- return newFileStream(openFTP(pu), closeFTP);
+ return newFileStream(openFTP(pu, uf), closeFTP);
}
URLFile
@@ -1587,7 +1588,7 @@ openURL(char *url, ParsedURL *pu, ParsedURL *current,
write(sock, tmp->ptr, tmp->length);
}
else {
- uf.stream = openFTPStream(pu);
+ uf.stream = openFTPStream(pu, &uf);
uf.scheme = pu->scheme;
return uf;
}