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 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 }
// Truncate changes the size of the file. // It does not change the I/O offset. // If there is an error, it will be of type *PathError. func (f *File) Truncate(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", f.name, err} } if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { return &PathError{"truncate", f.name, err} } 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 }
// Sync commits the current contents of the file to stable storage. // Typically, this means flushing the file system's in-memory copy // of recently written data to disk. func (f *File) Sync() (err error) { if f == nil { return ErrInvalid } var d syscall.Dir d.Null() var buf [syscall.STATFIXLEN]byte n, err := d.Marshal(buf[:]) if err != nil { return NewSyscallError("fsync", err) } if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { return NewSyscallError("fsync", 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 }
// Chmod changes the mode of the file to mode. // If there is an error, it will be of type *PathError. func (f *File) Chmod(mode FileMode) error { var d syscall.Dir odir, e := dirstat(f) if e != nil { return &PathError{"chmod", f.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", f.name, err} } if err = syscall.Fwstat(f.fd, buf[:n]); err != nil { return &PathError{"chmod", f.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 }