From 67dbeee5fe9e2a855384048615bafe100e876f4c Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 10 Oct 2017 13:49:37 -0400 Subject: acme: check file content before declaring file "modified since last read" Bad remote file systems can change mtime unexpectedly, and then there is the problem that git rebase and similar operations like to change the files and then change them back, modifying the mtimes but not the content. Avoid spurious Put errors on both of those by checking file content. (False positive "modified since last read" make the real ones difficult to notice.) --- src/cmd/acme/acme.c | 1 + src/cmd/acme/addr.c | 1 + src/cmd/acme/buff.c | 9 ++++++--- src/cmd/acme/cols.c | 1 + src/cmd/acme/dat.h | 7 ++++--- src/cmd/acme/disk.c | 1 + src/cmd/acme/ecmd.c | 3 ++- src/cmd/acme/edit.c | 1 + src/cmd/acme/elog.c | 1 + src/cmd/acme/exec.c | 37 +++++++++++++++++++++++++++++++++++-- src/cmd/acme/file.c | 5 +++-- src/cmd/acme/fns.h | 2 +- src/cmd/acme/fsys.c | 1 + src/cmd/acme/logf.c | 1 + src/cmd/acme/look.c | 1 + src/cmd/acme/regx.c | 1 + src/cmd/acme/rows.c | 1 + src/cmd/acme/scrl.c | 1 + src/cmd/acme/text.c | 13 ++++++++++++- src/cmd/acme/time.c | 1 + src/cmd/acme/util.c | 1 + src/cmd/acme/wind.c | 1 + src/cmd/acme/xfid.c | 1 + 23 files changed, 79 insertions(+), 13 deletions(-) diff --git a/src/cmd/acme/acme.c b/src/cmd/acme/acme.c index 2f4fe3ab..10dcdcc8 100644 --- a/src/cmd/acme/acme.c +++ b/src/cmd/acme/acme.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" /* for generating syms in mkfile only: */ diff --git a/src/cmd/acme/addr.c b/src/cmd/acme/addr.c index f09688dc..861e5e01 100644 --- a/src/cmd/acme/addr.c +++ b/src/cmd/acme/addr.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/buff.c b/src/cmd/acme/buff.c index 875bb0b0..bb938ca4 100644 --- a/src/cmd/acme/buff.c +++ b/src/cmd/acme/buff.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" @@ -231,7 +232,7 @@ bufloader(void *v, uint q0, Rune *r, int nr) } uint -loadfile(int fd, uint q0, int *nulls, int(*f)(void*, uint, Rune*, int), void *arg) +loadfile(int fd, uint q0, int *nulls, int(*f)(void*, uint, Rune*, int), void *arg, DigestState *h) { char *p; Rune *r; @@ -253,6 +254,8 @@ loadfile(int fd, uint q0, int *nulls, int(*f)(void*, uint, Rune*, int), void *ar warning(nil, "read error in Buffer.load"); break; } + if(h != nil) + sha1((uchar*)p+m, n, nil, h); m += n; p[m] = 0; l = m; @@ -269,11 +272,11 @@ loadfile(int fd, uint q0, int *nulls, int(*f)(void*, uint, Rune*, int), void *ar } uint -bufload(Buffer *b, uint q0, int fd, int *nulls) +bufload(Buffer *b, uint q0, int fd, int *nulls, DigestState *h) { if(q0 > b->nc) error("internal error: bufload"); - return loadfile(fd, q0, nulls, bufloader, b); + return loadfile(fd, q0, nulls, bufloader, b, h); } void diff --git a/src/cmd/acme/cols.c b/src/cmd/acme/cols.c index 5e16c21a..10e014ee 100644 --- a/src/cmd/acme/cols.c +++ b/src/cmd/acme/cols.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/dat.h b/src/cmd/acme/dat.h index df1a6422..8c4b14ee 100644 --- a/src/cmd/acme/dat.h +++ b/src/cmd/acme/dat.h @@ -105,7 +105,7 @@ struct Buffer }; void bufinsert(Buffer*, uint, Rune*, uint); void bufdelete(Buffer*, uint, uint); -uint bufload(Buffer*, uint, int, int*); +uint bufload(Buffer*, uint, int, int*, DigestState*); void bufread(Buffer*, uint, Rune*, uint); void bufclose(Buffer*); void bufreset(Buffer*); @@ -135,8 +135,9 @@ struct File Rune *name; /* name of associated file */ int nname; /* size of name */ uvlong qidpath; /* of file when read */ - ulong mtime; /* of file when read */ + ulong mtime; /* of file when read */ int dev; /* of file when read */ + uchar sha1[20]; /* of file when read */ int unread; /* file has not been read from disk */ int editclean; /* mark clean after edit command */ @@ -152,7 +153,7 @@ void fileclose(File*); void filedelete(File*, uint, uint); void filedeltext(File*, Text*); void fileinsert(File*, uint, Rune*, uint); -uint fileload(File*, uint, int, int*); +uint fileload(File*, uint, int, int*, DigestState*); void filemark(File*); void filereset(File*); void filesetname(File*, Rune*, int); diff --git a/src/cmd/acme/disk.c b/src/cmd/acme/disk.c index 8d95fa1d..c3ada9c3 100644 --- a/src/cmd/acme/disk.c +++ b/src/cmd/acme/disk.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/ecmd.c b/src/cmd/acme/ecmd.c index bdcaf9f5..9bedae4c 100644 --- a/src/cmd/acme/ecmd.c +++ b/src/cmd/acme/ecmd.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "edit.h" #include "fns.h" @@ -336,7 +337,7 @@ e_cmd(Text *t, Cmd *cp) } elogdelete(f, q0, q1); nulls = 0; - loadfile(fd, q1, &nulls, readloader, f); + loadfile(fd, q1, &nulls, readloader, f, nil); free(s); close(fd); if(nulls) diff --git a/src/cmd/acme/edit.c b/src/cmd/acme/edit.c index 4495bf69..d3f82059 100644 --- a/src/cmd/acme/edit.c +++ b/src/cmd/acme/edit.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "edit.h" #include "fns.h" diff --git a/src/cmd/acme/elog.c b/src/cmd/acme/elog.c index 4ec3e3b2..c5650f03 100644 --- a/src/cmd/acme/elog.c +++ b/src/cmd/acme/elog.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" #include "edit.h" diff --git a/src/cmd/acme/exec.c b/src/cmd/acme/exec.c index 2a259533..75737d80 100644 --- a/src/cmd/acme/exec.c +++ b/src/cmd/acme/exec.c @@ -9,6 +9,7 @@ #include #include #include +#include #include <9pclient.h> #include "dat.h" #include "fns.h" @@ -635,6 +636,30 @@ get(Text *et, Text *t, Text *argt, int flag1, int _0, Rune *arg, int narg) xfidlog(w, "get"); } +static void +checksha1(char *name, File *f, Dir *d) +{ + int fd, n; + DigestState *h; + uchar out[20]; + uchar *buf; + + fd = open(name, OREAD); + if(fd < 0) + return; + h = sha1(nil, 0, nil, nil); + buf = emalloc(8192); + while((n = read(fd, buf, 8192)) > 0) + sha1(buf, n, nil, h); + close(fd); + sha1(nil, 0, out, h); + if(memcmp(out, f->sha1, sizeof out) == 0) { + f->dev = d->dev; + f->qidpath = d->qid.path; + f->mtime = d->mtime; + } +} + void putfile(File *f, int q0, int q1, Rune *namer, int nname) { @@ -646,13 +671,15 @@ putfile(File *f, int q0, int q1, Rune *namer, int nname) Dir *d, *d1; Window *w; int isapp; + DigestState *h; w = f->curtext->w; name = runetobyte(namer, nname); d = dirstat(name); if(d!=nil && runeeq(namer, nname, f->name, f->nname)){ - /* f->mtime+1 because when talking over NFS it's often off by a second */ - if(f->dev!=d->dev || f->qidpath!=d->qid.path || labs((long)(f->mtime-d->mtime)) > 1){ + if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime != d->mtime) + checksha1(name, f, d); + if(f->dev!=d->dev || f->qidpath!=d->qid.path || f->mtime != d->mtime) { if(f->unread) warning(nil, "%s not written; file already exists\n", name); else @@ -679,6 +706,7 @@ putfile(File *f, int q0, int q1, Rune *namer, int nname) s = fbufalloc(); free(d); d = dirfstat(fd); + h = sha1(nil, 0, nil, nil); isapp = (d!=nil && d->length>0 && (d->qid.type&QTAPPEND)); if(isapp){ warning(nil, "%s not written; file is append only\n", name); @@ -691,6 +719,7 @@ putfile(File *f, int q0, int q1, Rune *namer, int nname) n = BUFSIZE/UTFmax; bufread(&f->b, q, r, n); m = snprint(s, BUFSIZE+1, "%.*S", n, r); + sha1((uchar*)s, m, nil, h); if(Bwrite(b, s, m) != m){ warning(nil, "can't write file %s: %r\n", name); goto Rescue2; @@ -730,6 +759,8 @@ putfile(File *f, int q0, int q1, Rune *namer, int nname) f->qidpath = d->qid.path; f->dev = d->dev; f->mtime = d->mtime; + sha1(nil, 0, f->sha1, h); + h = nil; f->mod = FALSE; w->dirty = FALSE; f->unread = FALSE; @@ -741,6 +772,7 @@ putfile(File *f, int q0, int q1, Rune *namer, int nname) } fbuffree(s); fbuffree(r); + free(h); free(d); free(namer); free(name); @@ -753,6 +785,7 @@ putfile(File *f, int q0, int q1, Rune *namer, int nname) Bterm(b); free(b); } + free(h); fbuffree(s); fbuffree(r); close(fd); diff --git a/src/cmd/acme/file.c b/src/cmd/acme/file.c index 00d5d54c..e1eddc46 100644 --- a/src/cmd/acme/file.c +++ b/src/cmd/acme/file.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" @@ -163,11 +164,11 @@ fileunsetname(File *f, Buffer *delta) } uint -fileload(File *f, uint p0, int fd, int *nulls) +fileload(File *f, uint p0, int fd, int *nulls, DigestState *h) { if(f->seq > 0) error("undo in file.load unimplemented"); - return bufload(&f->b, p0, fd, nulls); + return bufload(&f->b, p0, fd, nulls, h); } /* return sequence number of pending redo */ diff --git a/src/cmd/acme/fns.h b/src/cmd/acme/fns.h index af063445..1cd35257 100644 --- a/src/cmd/acme/fns.h +++ b/src/cmd/acme/fns.h @@ -25,7 +25,7 @@ void savemouse(Window*); int restoremouse(Window*); void clearmouse(void); void allwindows(void(*)(Window*, void*), void*); -uint loadfile(int, uint, int*, int(*)(void*, uint, Rune*, int), void*); +uint loadfile(int, uint, int*, int(*)(void*, uint, Rune*, int), void*, DigestState*); void movetodel(Window*); Window* errorwin(Mntdir*, int); diff --git a/src/cmd/acme/fsys.c b/src/cmd/acme/fsys.c index 549213d7..4c395eb2 100644 --- a/src/cmd/acme/fsys.c +++ b/src/cmd/acme/fsys.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/logf.c b/src/cmd/acme/logf.c index 82b3d80c..567b8383 100644 --- a/src/cmd/acme/logf.c +++ b/src/cmd/acme/logf.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/look.c b/src/cmd/acme/look.c index 3c5765a8..cbbc71bf 100644 --- a/src/cmd/acme/look.c +++ b/src/cmd/acme/look.c @@ -10,6 +10,7 @@ #include #include <9pclient.h> #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/regx.c b/src/cmd/acme/regx.c index 9ad07d3c..56ea900c 100644 --- a/src/cmd/acme/regx.c +++ b/src/cmd/acme/regx.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/rows.c b/src/cmd/acme/rows.c index 965088e1..2d6cbcca 100644 --- a/src/cmd/acme/rows.c +++ b/src/cmd/acme/rows.c @@ -9,6 +9,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/scrl.c b/src/cmd/acme/scrl.c index baf7ec79..6504699d 100644 --- a/src/cmd/acme/scrl.c +++ b/src/cmd/acme/scrl.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/text.c b/src/cmd/acme/text.c index c53694c6..4832de8e 100644 --- a/src/cmd/acme/text.c +++ b/src/cmd/acme/text.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "dat.h" #include "fns.h" @@ -198,6 +199,7 @@ textload(Text *t, uint q0, char *file, int setqid) Dir *d, *dbuf; char *tmp; Text *u; + DigestState *h; if(t->ncache!=0 || t->file->b.nc || t->w==nil || t!=&t->w->body) error("text.load"); @@ -220,6 +222,7 @@ textload(Text *t, uint q0, char *file, int setqid) goto Rescue; } nulls = FALSE; + h = nil; if(d->qid.type & QTDIR){ /* this is checked in get() but it's possible the file changed underfoot */ if(t->file->ntext > 1){ @@ -264,9 +267,17 @@ textload(Text *t, uint q0, char *file, int setqid) }else{ t->w->isdir = FALSE; t->w->filemenu = TRUE; - q1 = q0 + fileload(t->file, q0, fd, &nulls); + if(q0 == 0) + h = sha1(nil, 0, nil, nil); + q1 = q0 + fileload(t->file, q0, fd, &nulls, h); } if(setqid){ + if(h != nil) { + sha1(nil, 0, t->file->sha1, h); + h = nil; + } else { + memset(t->file->sha1, 0, sizeof t->file->sha1); + } t->file->dev = d->dev; t->file->mtime = d->mtime; t->file->qidpath = d->qid.path; diff --git a/src/cmd/acme/time.c b/src/cmd/acme/time.c index e1777131..38d70579 100644 --- a/src/cmd/acme/time.c +++ b/src/cmd/acme/time.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/util.c b/src/cmd/acme/util.c index 28c99ad3..d694634f 100644 --- a/src/cmd/acme/util.c +++ b/src/cmd/acme/util.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/wind.c b/src/cmd/acme/wind.c index 712eb1dc..19b52f5c 100644 --- a/src/cmd/acme/wind.c +++ b/src/cmd/acme/wind.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" diff --git a/src/cmd/acme/xfid.c b/src/cmd/acme/xfid.c index 33732def..5aa4a180 100644 --- a/src/cmd/acme/xfid.c +++ b/src/cmd/acme/xfid.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "dat.h" #include "fns.h" -- cgit v1.2.3