diff --git a/src/lib9/dirwstat.c b/src/lib9/dirwstat.c index 37d9e0e0..4f7bbab9 100644 --- a/src/lib9/dirwstat.c +++ b/src/lib9/dirwstat.c @@ -1,6 +1,7 @@ #include #define NOPLAN9DEFINES #include +#include #include #include #include @@ -9,9 +10,10 @@ int dirwstat(char *file, Dir *dir) { int ret; + char *newfile, *p; struct utimbuf ub; - /* BUG handle more */ + /* BUG handle more (e.g. uid, gid, atime) */ ret = 0; if(~dir->mode != 0){ if(chmod(file, dir->mode) < 0) @@ -27,5 +29,20 @@ dirwstat(char *file, Dir *dir) if(truncate(file, dir->length) < 0) ret = -1; } + // Note: rename after all other operations so we can use + // the original name in all other calls. + if(dir->name != nil && dir->name[0] != '\0'){ + newfile = nil; + if((p = strrchr(file, '/')) != nil){ + newfile = malloc((p-file) + 1 + strlen(dir->name) + 1); + if(newfile == nil) + return -1; + memmove(newfile, file, p-file+1); + strcpy(newfile+(p-file+1), dir->name); + } + if(rename(file, newfile != nil ? newfile : dir->name) < 0) + ret = -1; + free(newfile); + } return ret; }