func rename(oldname, newname string) error { dirname := oldname[:lastIndex(oldname, '/')+1] if hasPrefix(newname, dirname) { newname = newname[len(dirname):] } else { return &LinkError{"rename", oldname, newname, ErrInvalid} } // If newname still contains slashes after removing the oldname // prefix, the rename is cross-directory and must be rejected. // This case is caught by d.Marshal below. var d syscall.Dir d.Null() d.Name = newname buf := make([]byte, syscall.STATFIXLEN+len(d.Name)) n, err := d.Marshal(buf[:]) if err != nil { return &LinkError{"rename", oldname, newname, err} } if err = syscall.Wstat(oldname, buf[:n]); err != nil { return &LinkError{"rename", oldname, newname, err} } return nil }
// Rename renames a file. func Rename(oldname, newname string) error { var d Dir d.Null() d.Name = newname if e := syscall.Wstat(oldname, pdir(nil, &d)); e != nil { return &PathError{"rename", oldname, e} } return nil }
// Truncate changes the size of the named file. // If the file is a symbolic link, it changes the size of the link's target. func Truncate(name string, size int64) error { var d Dir d.Null() d.Length = uint64(size) if e := syscall.Wstat(name, pdir(nil, &d)); e != nil { return &PathError{"truncate", name, e} } return nil }
// Chmod changes the mode of the named file to mode. func Chmod(name string, mode uint32) Error { var d Dir d.Null() d.Mode = mode & 0777 if e := syscall.Wstat(name, pdir(nil, &d)); iserror(e) { return &PathError{"chmod", name, e} } return nil }
// Chtimes changes the access and modification times of the named // file, similar to the Unix utime() or utimes() functions. // // The underlying filesystem may truncate or round the values to a // less precise time unit. func Chtimes(name string, atime time.Time, mtime time.Time) error { var d Dir d.Null() d.Atime = uint32(atime.Unix()) d.Mtime = uint32(mtime.Unix()) if e := syscall.Wstat(name, pdir(nil, &d)); e != nil { return &PathError{"chtimes", name, e} } return nil }
// Chtimes changes the access and modification times of the named // file, similar to the Unix utime() or utimes() functions. // // The argument times are in nanoseconds, although the underlying // filesystem may truncate or round the values to a more // coarse time unit. func Chtimes(name string, atimeNs int64, mtimeNs int64) Error { var d Dir d.Null() d.Atime = uint32(atimeNs / 1e9) d.Mtime = uint32(mtimeNs / 1e9) if e := syscall.Wstat(name, pdir(nil, &d)); iserror(e) { return &PathError{"chtimes", name, e} } return nil }
// ChownPlan9 changes the uid and gid strings of the named file. func ChownPlan9(name, uid, gid string) Error { var d Dir d.Null() d.Uid = uid d.Gid = gid if e := syscall.Wstat(name, pdir(nil, &d)); iserror(e) { return &PathError{"chown_plan9", name, e} } return nil }
// Chmod changes the mode of the named file to mode. func Chmod(name string, mode FileMode) error { var d Dir odir, e := dirstat(name) if e != nil { return &PathError{"chmod", name, e} } d.Null() d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask if e := syscall.Wstat(name, pdir(nil, &d)); e != nil { return &PathError{"chmod", name, e} } return nil }
// Rename renames a file. func Rename(oldname, newname string) error { var d syscall.Dir d.Null() d.Name = newname buf := make([]byte, syscall.STATFIXLEN+len(d.Name)) n, err := d.Marshal(buf[:]) if err != nil { return &PathError{"rename", oldname, err} } if err = syscall.Wstat(oldname, buf[:n]); err != nil { return &PathError{"rename", oldname, err} } return nil }
// Truncate changes the size of the named file. // If the file is a symbolic link, it changes the size of the link's target. // If there is an error, it will be of type *PathError. func Truncate(name string, size int64) error { var d syscall.Dir d.Null() d.Length = size var buf [syscall.STATFIXLEN]byte n, err := d.Marshal(buf[:]) if err != nil { return &PathError{"truncate", name, err} } if err = syscall.Wstat(name, buf[:n]); err != nil { return &PathError{"truncate", name, err} } return nil }
// Chmod changes the mode of the named file to mode. func Chmod(name string, mode uint32) error { var d Dir var mask = ^uint32(0777) d.Null() odir, e := dirstat(name) if iserror(e) { return &PathError{"chmod", name, e} } d.Mode = (odir.Mode & mask) | (mode &^ mask) if e := syscall.Wstat(name, pdir(nil, &d)); iserror(e) { return &PathError{"chmod", name, e} } return nil }
// Chtimes changes the access and modification times of the named // file, similar to the Unix utime() or utimes() functions. // // The underlying filesystem may truncate or round the values to a // less precise time unit. // If there is an error, it will be of type *PathError. func Chtimes(name string, atime time.Time, mtime time.Time) error { var d syscall.Dir d.Null() d.Atime = uint32(atime.Unix()) d.Mtime = uint32(mtime.Unix()) var buf [syscall.STATFIXLEN]byte n, err := d.Marshal(buf[:]) if err != nil { return &PathError{"chtimes", name, err} } if err = syscall.Wstat(name, buf[:n]); err != nil { return &PathError{"chtimes", name, err} } return nil }
// Chmod changes the mode of the named file to mode. // If the file is a symbolic link, it changes the mode of the link's target. // If there is an error, it will be of type *PathError. func Chmod(name string, mode FileMode) error { var d syscall.Dir odir, e := dirstat(name) if e != nil { return &PathError{"chmod", name, e} } d.Null() d.Mode = odir.Mode&^chmodMask | syscallMode(mode)&chmodMask var buf [syscall.STATFIXLEN]byte n, err := d.Marshal(buf[:]) if err != nil { return &PathError{"chmod", name, err} } if err = syscall.Wstat(name, buf[:n]); err != nil { return &PathError{"chmod", name, err} } return nil }
func rename(oldname, newname string) error { dirname := oldname[:lastIndex(oldname, '/')+1] if hasPrefix(newname, dirname) { newname = newname[len(dirname):] } else { return &LinkError{"rename", oldname, newname, ErrInvalid} } // If newname still contains slashes after removing the oldname // prefix, the rename is cross-directory and must be rejected. if lastIndex(newname, '/') >= 0 { return &LinkError{"rename", oldname, newname, ErrInvalid} } var d syscall.Dir d.Null() d.Name = newname buf := make([]byte, syscall.STATFIXLEN+len(d.Name)) n, err := d.Marshal(buf[:]) if err != nil { return &LinkError{"rename", oldname, newname, err} } // If newname already exists and is not a directory, rename replaces it. f, err := Stat(dirname + newname) if err == nil && !f.IsDir() { Remove(dirname + newname) } if err = syscall.Wstat(oldname, buf[:n]); err != nil { return &LinkError{"rename", oldname, newname, err} } return nil }