Browse Source

Plan 9 from Bell Labs 2006-11-09

David du Colombier 14 years ago
parent
commit
342bc96d5a
6 changed files with 108 additions and 60 deletions
  1. 5 6
      dist/replica/_plan9.db
  2. 4 4
      dist/replica/plan9.db
  3. 5 0
      dist/replica/plan9.log
  4. 4 1
      sys/man/2/wait
  5. 23 8
      sys/src/9/port/devsrv.c
  6. 67 41
      sys/src/cmd/mv.c

+ 5 - 6
dist/replica/_plan9.db

@@ -15,7 +15,7 @@
 386/_9pcdisk.gz - 664 sys sys 1039764191 695837
 386/bin - 20000000775 sys sys 1018897690 0
 386/bin/8a - 775 sys sys 1148500566 116698
-386/bin/8c - 775 sys sys 1157916201 365445
+386/bin/8c - 775 sys sys 1162959070 365562
 386/bin/8l - 775 sys sys 1148500567 115711
 386/bin/9660srv - 775 sys sys 1148500567 104375
 386/bin/aan - 775 sys sys 1148500568 131392
@@ -335,7 +335,7 @@
 386/bin/ms2html - 775 sys sys 1148500672 104279
 386/bin/mtime - 775 sys sys 1148500673 59470
 386/bin/mug - 775 sys sys 1157570246 176473
-386/bin/mv - 775 sys sys 1148500673 65825
+386/bin/mv - 775 sys sys 1163044781 66723
 386/bin/ndb - 20000000775 sys sys 985743147 0
 386/bin/ndb/cs - 775 sys sys 1162241044 150965
 386/bin/ndb/csquery - 775 sys sys 1148500674 61796
@@ -7559,7 +7559,7 @@ sys/man/2/symbol - 664 sys sys 950892874 9423
 sys/man/2/thread - 664 sys sys 1117034202 11640
 sys/man/2/time - 664 sys sys 1015091527 739
 sys/man/2/tmpfile - 664 sys sys 1048637161 1157
-sys/man/2/wait - 664 sys sys 1162948415 2507
+sys/man/2/wait - 664 sys sys 1163004064 2525
 sys/man/2/window - 664 sys sys 950593499 5522
 sys/man/3 - 20000000775 sys sys 1017251174 0
 sys/man/3/0intro - 664 sys sys 984709634 1873
@@ -8108,7 +8108,7 @@ sys/src/9/port/devroot.c - 664 sys sys 1146151714 4242
 sys/src/9/port/devsd.c - 664 sys sys 1155995769 30859
 sys/src/9/port/devsdp.c - 664 sys sys 1057323393 44800
 sys/src/9/port/devsegment.c - 664 sys sys 1131289891 9610
-sys/src/9/port/devsrv.c - 664 sys sys 1107232208 5418
+sys/src/9/port/devsrv.c - 664 sys sys 1163030369 5728
 sys/src/9/port/devssl.c - 664 sys sys 1146151714 26121
 sys/src/9/port/devtinyfs.c - 664 sys sys 1015278339 15347
 sys/src/9/port/devtls.c - 664 sys sys 1146151715 45214
@@ -12651,7 +12651,7 @@ sys/src/cmd/mount.c - 664 sys sys 1138061862 1633
 sys/src/cmd/ms2html.c - 664 sys sys 1143695278 41022
 sys/src/cmd/mtime.c - 664 sys sys 1036172302 450
 sys/src/cmd/mug.c - 664 sys sys 1157143847 24888
-sys/src/cmd/mv.c - 664 sys sys 1126278115 4241
+sys/src/cmd/mv.c - 664 sys sys 1163029439 4682
 sys/src/cmd/ndb - 20000000775 sys sys 988249988 0
 sys/src/cmd/ndb/convDNS2M.c - 664 sys sys 1119276409 6866
 sys/src/cmd/ndb/convM2DNS.c - 664 sys sys 1060612175 7248
@@ -15673,4 +15673,3 @@ usr/glenda/lib/profile - 664 glenda glenda 1105128663 890
 usr/glenda/readme.acme - 664 glenda glenda 1019860628 4753
 usr/glenda/readme.rio - 664 glenda glenda 1019860628 6370
 usr/glenda/tmp - 20000000775 glenda glenda 1018802620 0
-386/bin/8c - 775 sys sys 1162959070 365562

+ 4 - 4
dist/replica/plan9.db

@@ -335,7 +335,7 @@
 386/bin/ms2html - 775 sys sys 1148500672 104279
 386/bin/mtime - 775 sys sys 1148500673 59470
 386/bin/mug - 775 sys sys 1157570246 176473
-386/bin/mv - 775 sys sys 1148500673 65825
+386/bin/mv - 775 sys sys 1163044781 66723
 386/bin/ndb - 20000000775 sys sys 985743147 0
 386/bin/ndb/cs - 775 sys sys 1162241044 150965
 386/bin/ndb/csquery - 775 sys sys 1148500674 61796
@@ -7559,7 +7559,7 @@ sys/man/2/symbol - 664 sys sys 950892874 9423
 sys/man/2/thread - 664 sys sys 1117034202 11640
 sys/man/2/time - 664 sys sys 1015091527 739
 sys/man/2/tmpfile - 664 sys sys 1048637161 1157
-sys/man/2/wait - 664 sys sys 1162948415 2507
+sys/man/2/wait - 664 sys sys 1163004064 2525
 sys/man/2/window - 664 sys sys 950593499 5522
 sys/man/3 - 20000000775 sys sys 1017251174 0
 sys/man/3/0intro - 664 sys sys 984709634 1873
@@ -8108,7 +8108,7 @@ sys/src/9/port/devroot.c - 664 sys sys 1146151714 4242
 sys/src/9/port/devsd.c - 664 sys sys 1155995769 30859
 sys/src/9/port/devsdp.c - 664 sys sys 1057323393 44800
 sys/src/9/port/devsegment.c - 664 sys sys 1131289891 9610
-sys/src/9/port/devsrv.c - 664 sys sys 1107232208 5418
+sys/src/9/port/devsrv.c - 664 sys sys 1163030369 5728
 sys/src/9/port/devssl.c - 664 sys sys 1146151714 26121
 sys/src/9/port/devtinyfs.c - 664 sys sys 1015278339 15347
 sys/src/9/port/devtls.c - 664 sys sys 1146151715 45214
@@ -12651,7 +12651,7 @@ sys/src/cmd/mount.c - 664 sys sys 1138061862 1633
 sys/src/cmd/ms2html.c - 664 sys sys 1143695278 41022
 sys/src/cmd/mtime.c - 664 sys sys 1036172302 450
 sys/src/cmd/mug.c - 664 sys sys 1157143847 24888
-sys/src/cmd/mv.c - 664 sys sys 1126278115 4241
+sys/src/cmd/mv.c - 664 sys sys 1163029439 4682
 sys/src/cmd/ndb - 20000000775 sys sys 988249988 0
 sys/src/cmd/ndb/convDNS2M.c - 664 sys sys 1119276409 6866
 sys/src/cmd/ndb/convM2DNS.c - 664 sys sys 1060612175 7248

+ 5 - 0
dist/replica/plan9.log

@@ -31717,3 +31717,8 @@
 1162953007 6 c sys/src/cmd/vac/vtdump.c - 664 sys sys 1162951782 8007
 1162953007 7 c sys/src/cmd/vac/vtread.c - 664 sys sys 1162951740 2016
 1162960205 0 c 386/bin/8c - 775 sys sys 1162959070 365562
+1163005205 0 c sys/man/2/wait - 664 sys sys 1163004064 2525
+1163030405 0 c 386/bin/mv - 775 sys sys 1163029466 66723
+1163030405 1 c sys/src/9/port/devsrv.c - 664 sys sys 1163030369 5728
+1163030405 2 c sys/src/cmd/mv.c - 664 sys sys 1163029439 4682
+1163044804 0 c 386/bin/mv - 775 sys sys 1163044781 66723

+ 4 - 1
sys/man/2/wait

@@ -99,9 +99,12 @@ of the truncated message.
 In other words, each call to
 .I await
 returns the information about one child, blocking if necessary if no child has exited.
+.PP
 If the calling process has no living children,
 .I await
-returns
+and
+.I waitpid
+return
 .BR -1 .
 .SH SOURCE
 .B /sys/src/libc/9syscall

+ 23 - 8
sys/src/9/port/devsrv.c

@@ -139,6 +139,7 @@ srvopen(Chan *c, int omode)
 static void
 srvcreate(Chan *c, char *name, int omode, ulong perm)
 {
+	char *sname;
 	Srv *sp;
 
 	if(openmode(omode) != OWRITE)
@@ -147,9 +148,13 @@ srvcreate(Chan *c, char *name, int omode, ulong perm)
 	if(omode & OCEXEC)	/* can't happen */
 		panic("someone broke namec");
 
-	sp = malloc(sizeof(Srv)+strlen(name)+1);
-	if(sp == 0)
+	sp = smalloc(sizeof *sp);
+	sname = smalloc(strlen(name)+1);
+	if(sp == nil || sname == nil) {
+		free(sp);
+		free(sname);
 		error(Enomem);
+	}
 
 	qlock(&srvlk);
 	if(waserror()){
@@ -162,8 +167,8 @@ srvcreate(Chan *c, char *name, int omode, ulong perm)
 
 	sp->path = qidpath++;
 	sp->link = srv;
-	sp->name = (char*)(sp+1);
-	strcpy(sp->name, name);
+	strcpy(sname, name);
+	sp->name = sname;
 	c->qid.type = QTFILE;
 	c->qid.path = sp->path;
 	srv = sp;
@@ -221,21 +226,25 @@ srvremove(Chan *c)
 
 	if(sp->chan)
 		cclose(sp->chan);
+	free(sp->name);
 	free(sp);
 }
 
 static int
 srvwstat(Chan *c, uchar *dp, int n)
 {
+	char *strs;
 	Dir d;
 	Srv *sp;
 
 	if(c->qid.type & QTDIR)
 		error(Eperm);
 
+	strs = nil;
 	qlock(&srvlk);
 	if(waserror()){
 		qunlock(&srvlk);
+		free(strs);
 		nexterror();
 	}
 
@@ -243,18 +252,24 @@ srvwstat(Chan *c, uchar *dp, int n)
 	if(sp == 0)
 		error(Enonexist);
 
-	if(strcmp(sp->owner, up->user) && !iseve())
+	if(strcmp(sp->owner, up->user) != 0 && !iseve())
 		error(Eperm);
 
-	n = convM2D(dp, n, &d, nil);
+	strs = smalloc(n);
+	n = convM2D(dp, n, &d, strs);
 	if(n == 0)
-		error (Eshortstat);
+		error(Eshortstat);
 	if(d.mode != ~0UL)
 		sp->perm = d.mode & 0777;
 	if(d.uid && *d.uid)
 		kstrdup(&sp->owner, d.uid);
-
+	if(d.name && *d.name && strcmp(sp->name, d.name) != 0) {
+		if(strchr(d.name, '/') != nil)
+			error(Ebadchar);
+		kstrdup(&sp->name, d.name);
+	}
 	qunlock(&srvlk);
+	free(strs);
 	poperror();
 	return n;
 }

+ 67 - 41
sys/src/cmd/mv.c

@@ -1,17 +1,17 @@
 #include <u.h>
 #include <libc.h>
 
-void	split(char *, char **, char **);
-int	samefile(char *, char *);
-int	mv(char *from, char *todir, char *toelem);
 int	copy1(int fdf, int fdt, char *from, char *to);
 void	hardremove(char *);
+int	mv(char *from, char *todir, char *toelem);
+int	mv1(char *from, Dir *dirb, char *todir, char *toelem);
+int	samefile(char *, char *);
+void	split(char *, char **, char **);
 
 void
 main(int argc, char *argv[])
 {
-	int i;
-	int failed;
+	int i, failed;
 	Dir *dirto, *dirfrom;
 	char *todir, *toelem;
 
@@ -20,21 +20,30 @@ main(int argc, char *argv[])
 		fprint(2, "	  mv fromfile ... todir\n");
 		exits("bad usage");
 	}
+
+	/* prepass to canonicalise names before splitting, etc. */
+	for(i=1; i < argc; i++)
+		cleanname(argv[i]);
+
 	if((dirto = dirstat(argv[argc-1])) != nil && (dirto->mode&DMDIR)){
+		dirfrom = nil;
 		if(argc == 3
-		&& (dirfrom = dirstat(argv[1])) !=nil
-		&& (dirfrom->mode & DMDIR))
-			split(argv[argc-1], &todir, &toelem);
-		else{
+		&& (dirfrom = dirstat(argv[1])) != nil
+		&& (dirfrom->mode & DMDIR)) 
+			split(argv[argc-1], &todir, &toelem); /* mv dir1 dir2 */
+		else{				/* mv file... dir */
 			todir = argv[argc-1];
-			toelem = 0;	/* toelem will be fromelem */
+			toelem = nil;		/* toelem will be fromelem */
 		}
+		free(dirfrom);
 	}else
-		split(argv[argc-1], &todir, &toelem);
-	if(argc>3 && toelem != 0){
+		split(argv[argc-1], &todir, &toelem);	/* mv file1 file2 */
+	free(dirto);
+	if(argc>3 && toelem != nil){
 		fprint(2, "mv: %s not a directory\n", argv[argc-1]);
 		exits("bad usage");
 	}
+
 	failed = 0;
 	for(i=1; i < argc-1; i++)
 		if(mv(argv[i], todir, toelem) < 0)
@@ -47,17 +56,27 @@ main(int argc, char *argv[])
 int
 mv(char *from, char *todir, char *toelem)
 {
-	Dir *dirb, *dirt, null;
-	char toname[4096], fromname[4096];
-	int fdf, fdt, i, j;
 	int stat;
-	char *fromdir, *fromelem;
+	Dir *dirb;
 
 	dirb = dirstat(from);
 	if(dirb == nil){
 		fprint(2, "mv: can't stat %s: %r\n", from);
 		return -1;
 	}
+	stat = mv1(from, dirb, todir, toelem);
+	free(dirb);
+	return stat;
+}
+
+int
+mv1(char *from, Dir *dirb, char *todir, char *toelem)
+{
+	int fdf, fdt, i, j, stat;
+	char toname[4096], fromname[4096];
+	char *fromdir, *fromelem;
+	Dir *dirt, null;
+
 	strncpy(fromname, from, sizeof fromname);
 	split(from, &fromdir, &fromelem);
 	if(toelem == 0)
@@ -69,29 +88,37 @@ mv(char *from, char *todir, char *toelem)
 	}
 	j = strlen(todir);
 	if(i + j + 2 > sizeof toname){
-		fprint(2, "mv: path too big (max %d): %s/%s\n", sizeof toname, todir, toelem);
+		fprint(2, "mv: path too big (max %d): %s/%s\n",
+			sizeof toname, todir, toelem);
 		return -1;
 	}
 	memmove(toname, todir, j);
 	toname[j] = '/';
 	memmove(toname+j+1, toelem, i);
 	toname[i+j+1] = 0;
+
 	if(samefile(fromdir, todir)){
 		if(samefile(fromname, toname)){
-			fprint(2, "mv: %s and %s are the same\n", fromname, toname);
+			fprint(2, "mv: %s and %s are the same\n",
+				fromname, toname);
 			return -1;
 		}
+
+		/* remove target if present */
 		dirt = dirstat(toname);
-		if(dirt != nil){
-			free(dirt);
+		if(dirt != nil) {
 			hardremove(toname);
+			free(dirt);
 		}
+
+		/* try wstat */
 		nulldir(&null);
 		null.name = toelem;
 		if(dirwstat(fromname, &null) >= 0)
 			return 0;
 		if(dirb->mode & DMDIR){
-			fprint(2, "mv: can't rename directory %s: %r\n", fromname);
+			fprint(2, "mv: can't rename directory %s: %r\n",
+				fromname);
 			return -1;
 		}
 	}
@@ -99,7 +126,8 @@ mv(char *from, char *todir, char *toelem)
 	 * Renaming won't work --- must copy
 	 */
 	if(dirb->mode & DMDIR){
-		fprint(2, "mv: %s is a directory, not copied to %s\n", fromname, toname);
+		fprint(2, "mv: %s is a directory, not copied to %s\n",
+			fromname, toname);
 		return -1;
 	}
 	fdf = open(fromname, OREAD);
@@ -107,10 +135,12 @@ mv(char *from, char *todir, char *toelem)
 		fprint(2, "mv: can't open %s: %r\n", fromname);
 		return -1;
 	}
+
 	dirt = dirstat(toname);
 	if(dirt != nil && (dirt->mode & DMAPPEND))
-		hardremove(toname);	/* because create() won't truncate file */
+		hardremove(toname);  /* because create() won't truncate file */
 	free(dirt);
+
 	fdt = create(toname, OWRITE, dirb->mode);
 	if(fdt < 0){
 		fprint(2, "mv: can't create %s: %r\n", toname);
@@ -119,6 +149,7 @@ mv(char *from, char *todir, char *toelem)
 	}
 	stat = copy1(fdf, fdt, fromname, toname);
 	close(fdf);
+
 	if(stat >= 0){
 		nulldir(&null);
 		null.mtime = dirb->mtime;
@@ -139,16 +170,11 @@ copy1(int fdf, int fdt, char *from, char *to)
 	char buf[8192];
 	long n, n1;
 
-	for(;;){
-		n = read(fdf, buf, sizeof buf);
-		if(n >= 0){
-			if(n == 0)
-				break;
-			n1 = write(fdt, buf, n);
-			if(n1 != n){
-				fprint(2, "mv: error writing %s: %r\n", to);
-				return -1;
-			}
+	while ((n = read(fdf, buf, sizeof buf)) > 0) {
+		n1 = write(fdt, buf, n);
+		if(n1 != n){
+			fprint(2, "mv: error writing %s: %r\n", to);
+			return -1;
 		}
 	}
 	if(n < 0){
@@ -187,13 +213,12 @@ samefile(char *a, char *b)
 		return 1;
 	da = dirstat(a);
 	db = dirstat(b);
-	ret = (da !=nil ) &&
-		(db != nil) &&
-		(da->qid.type==db->qid.type) &&
-		(da->qid.path==db->qid.path) &&
-		(da->qid.vers==db->qid.vers) &&
-		(da->dev==db->dev) &&
-		da->type==db->type;
+	ret = (da != nil && db != nil &&
+		da->qid.type==db->qid.type &&
+		da->qid.path==db->qid.path &&
+		da->qid.vers==db->qid.vers &&
+		da->dev==db->dev &&
+		da->type==db->type);
 	free(da);
 	free(db);
 	return ret;
@@ -206,5 +231,6 @@ hardremove(char *a)
 		fprint(2, "mv: can't remove %s: %r\n", a);
 		exits("mv");
 	}
-	do; while(remove(a) != -1);
+	while(remove(a) != -1)
+		;
 }