...
Run Format

Source file src/os/file_posix.go

     1	// Copyright 2009 The Go Authors. All rights reserved.
     2	// Use of this source code is governed by a BSD-style
     3	// license that can be found in the LICENSE file.
     4	
     5	// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris windows
     6	
     7	package os
     8	
     9	import (
    10		"syscall"
    11		"time"
    12	)
    13	
    14	func sigpipe() // implemented in package runtime
    15	
    16	// Readlink returns the destination of the named symbolic link.
    17	// If there is an error, it will be of type *PathError.
    18	func Readlink(name string) (string, error) {
    19		for len := 128; ; len *= 2 {
    20			b := make([]byte, len)
    21			n, e := fixCount(syscall.Readlink(name, b))
    22			if e != nil {
    23				return "", &PathError{"readlink", name, e}
    24			}
    25			if n < len {
    26				return string(b[0:n]), nil
    27			}
    28		}
    29	}
    30	
    31	func rename(oldname, newname string) error {
    32		e := syscall.Rename(oldname, newname)
    33		if e != nil {
    34			return &LinkError{"rename", oldname, newname, e}
    35		}
    36		return nil
    37	}
    38	
    39	// syscallMode returns the syscall-specific mode bits from Go's portable mode bits.
    40	func syscallMode(i FileMode) (o uint32) {
    41		o |= uint32(i.Perm())
    42		if i&ModeSetuid != 0 {
    43			o |= syscall.S_ISUID
    44		}
    45		if i&ModeSetgid != 0 {
    46			o |= syscall.S_ISGID
    47		}
    48		if i&ModeSticky != 0 {
    49			o |= syscall.S_ISVTX
    50		}
    51		// No mapping for Go's ModeTemporary (plan9 only).
    52		return
    53	}
    54	
    55	// Chmod changes the mode of the named file to mode.
    56	// If the file is a symbolic link, it changes the mode of the link's target.
    57	// If there is an error, it will be of type *PathError.
    58	func Chmod(name string, mode FileMode) error {
    59		if e := syscall.Chmod(name, syscallMode(mode)); e != nil {
    60			return &PathError{"chmod", name, e}
    61		}
    62		return nil
    63	}
    64	
    65	// Chmod changes the mode of the file to mode.
    66	// If there is an error, it will be of type *PathError.
    67	func (f *File) Chmod(mode FileMode) error {
    68		if f == nil {
    69			return ErrInvalid
    70		}
    71		if e := syscall.Fchmod(f.fd, syscallMode(mode)); e != nil {
    72			return &PathError{"chmod", f.name, e}
    73		}
    74		return nil
    75	}
    76	
    77	// Chown changes the numeric uid and gid of the named file.
    78	// If the file is a symbolic link, it changes the uid and gid of the link's target.
    79	// If there is an error, it will be of type *PathError.
    80	func Chown(name string, uid, gid int) error {
    81		if e := syscall.Chown(name, uid, gid); e != nil {
    82			return &PathError{"chown", name, e}
    83		}
    84		return nil
    85	}
    86	
    87	// Lchown changes the numeric uid and gid of the named file.
    88	// If the file is a symbolic link, it changes the uid and gid of the link itself.
    89	// If there is an error, it will be of type *PathError.
    90	func Lchown(name string, uid, gid int) error {
    91		if e := syscall.Lchown(name, uid, gid); e != nil {
    92			return &PathError{"lchown", name, e}
    93		}
    94		return nil
    95	}
    96	
    97	// Chown changes the numeric uid and gid of the named file.
    98	// If there is an error, it will be of type *PathError.
    99	func (f *File) Chown(uid, gid int) error {
   100		if f == nil {
   101			return ErrInvalid
   102		}
   103		if e := syscall.Fchown(f.fd, uid, gid); e != nil {
   104			return &PathError{"chown", f.name, e}
   105		}
   106		return nil
   107	}
   108	
   109	// Truncate changes the size of the file.
   110	// It does not change the I/O offset.
   111	// If there is an error, it will be of type *PathError.
   112	func (f *File) Truncate(size int64) error {
   113		if f == nil {
   114			return ErrInvalid
   115		}
   116		if e := syscall.Ftruncate(f.fd, size); e != nil {
   117			return &PathError{"truncate", f.name, e}
   118		}
   119		return nil
   120	}
   121	
   122	// Sync commits the current contents of the file to stable storage.
   123	// Typically, this means flushing the file system's in-memory copy
   124	// of recently written data to disk.
   125	func (f *File) Sync() (err error) {
   126		if f == nil {
   127			return ErrInvalid
   128		}
   129		if e := syscall.Fsync(f.fd); e != nil {
   130			return NewSyscallError("fsync", e)
   131		}
   132		return nil
   133	}
   134	
   135	// Chtimes changes the access and modification times of the named
   136	// file, similar to the Unix utime() or utimes() functions.
   137	//
   138	// The underlying filesystem may truncate or round the values to a
   139	// less precise time unit.
   140	// If there is an error, it will be of type *PathError.
   141	func Chtimes(name string, atime time.Time, mtime time.Time) error {
   142		var utimes [2]syscall.Timespec
   143		utimes[0] = syscall.NsecToTimespec(atime.UnixNano())
   144		utimes[1] = syscall.NsecToTimespec(mtime.UnixNano())
   145		if e := syscall.UtimesNano(name, utimes[0:]); e != nil {
   146			return &PathError{"chtimes", name, e}
   147		}
   148		return nil
   149	}
   150	

View as plain text