func touch(filename string, nacc, nmod int64) (errcnt int) { var st syscall.Stat_t var ut syscall.Utimbuf if e := syscall.Stat(filename, &st); e != 0 { if e == syscall.ENOENT { if *cflag { errcnt++ return } var fd int defer syscall.Close(fd) if fd, e = syscall.Creat(filename, 0666); e != 0 { fmt.Fprintf(os.Stderr, "touch: can not create %s\n", filename) errcnt += 1 return } if e = syscall.Fstat(fd, &st); e != 0 { fmt.Fprintf(os.Stderr, "touch: can't stat %s\n", filename) errcnt += 1 return } } else { fmt.Fprintf(os.Stderr, "touch: can't stat %s\n", filename) errcnt += 1 return } } if *aflag { ut.Actime = nacc } else { ut.Actime = st.Atim.Sec } if *mflag { ut.Modtime = nmod } else { ut.Modtime = st.Mtim.Sec } if nulltime { if e := syscall.Utime(filename, nil); e != 0 { fmt.Fprintf(os.Stderr, "touch: unable to touch %s", filename) errcnt += 1 } } else { if e := syscall.Utime(filename, &ut); e != 0 { fmt.Fprintf(os.Stderr, "touch: unable to touch %s", filename) errcnt += 1 } } return }
func main() { flag.Parse() if flag.NArg() < 1 { usage() } for i := 0; i < flag.NArg(); i++ { var name = flag.Arg(i) var tb syscall.Utimbuf tb.Actime = (int32)(*newTime) tb.Modtime = (int32)(*newTime) var e = syscall.Utime(name, &tb) if (e != 0) && *create { fmt.Fprintf( os.Stderr, "touch: cannot touch `%s'\n", name) os.Exit(1) } f, err := os.Open(name, os.O_CREAT, 0666) if err != nil { fmt.Fprintf( os.Stderr, "touch: cannot touch `%s': %s\n", name, err.String()) os.Exit(1) } f.Close() e = syscall.Utime(name, &tb) if e != 0 { fmt.Fprintf( os.Stderr, "touch: cannot touch `%s'\n", name) os.Exit(1) } } os.Exit(0) }
// TestPutTouch // Test that when applying PUT to a block that already exists, // the block's modification time is updated. func TestPutTouch(t *testing.T) { v := TempUnixVolume(t, false, false) defer _teardown(v) if err := v.Put(TEST_HASH, TEST_BLOCK); err != nil { t.Error(err) } // We'll verify { t0 < threshold < t1 }, where t0 is the // existing block's timestamp on disk before Put() and t1 is // its timestamp after Put(). threshold := time.Now().Add(-time.Second) // Set the stored block's mtime far enough in the past that we // can see the difference between "timestamp didn't change" // and "timestamp granularity is too low". { oldtime := time.Now().Add(-20 * time.Second).Unix() if err := syscall.Utime(v.blockPath(TEST_HASH), &syscall.Utimbuf{oldtime, oldtime}); err != nil { t.Error(err) } // Make sure v.Mtime() agrees the above Utime really worked. if t0, err := v.Mtime(TEST_HASH); err != nil || t0.IsZero() || !t0.Before(threshold) { t.Errorf("Setting mtime failed: %v, %v", t0, err) } } // Write the same block again. if err := v.Put(TEST_HASH, TEST_BLOCK); err != nil { t.Error(err) } // Verify threshold < t1 t1, err := v.Mtime(TEST_HASH) if err != nil { t.Error(err) } if t1.Before(threshold) { t.Errorf("t1 %v must be >= threshold %v after v.Put ", t1, threshold) } }
func utimeHandle(tConn *TcfsConn, msgbuf []byte) { rootdir := tConn.RootDir buf := tConn.Buf atime := binary.BigEndian.Uint64(msgbuf[0:8]) mtime := binary.BigEndian.Uint64(msgbuf[8:16]) fixpath := rootdir + string(msgbuf[16:]) err := syscall.Utime(fixpath, &syscall.Utimbuf{int64(atime), int64(mtime)}) if err != nil { log.Print("Can't create", err) binary.BigEndian.PutUint32(buf[0:4], 4) ret := -13 binary.BigEndian.PutUint32(buf[4:8], uint32(ret)) tConn.Write(buf[:8]) } else { binary.BigEndian.PutUint32(buf[0:4], 4) binary.BigEndian.PutUint32(buf[4:8], 0) tConn.Write(buf[:8]) } }
// extractFile is a helper function for Extract which extracts a file and // matches the new file's mtime and atime to the original archive entry. func (srcpkg *SrcPkg) extractFile(newpath string, hdr *tar.Header) os.Error { file, err := os.Create(newpath) if err != nil { return err } _, err = io.Copy(file, srcpkg.reader) file.Close() if err != nil { return err } ubuf := &syscall.Utimbuf{int32(hdr.Atime), int32(hdr.Mtime)} if errno := syscall.Utime(newpath, ubuf); errno != 0 { return os.NewError("Failed to set modification time for " + newpath) } return nil }
// Touch sets the timestamp for the given locator to the current time func (v *UnixVolume) Touch(loc string) error { if v.readonly { return MethodDisabledError } p := v.blockPath(loc) f, err := os.OpenFile(p, os.O_RDWR|os.O_APPEND, 0644) if err != nil { return err } defer f.Close() if v.locker != nil { v.locker.Lock() defer v.locker.Unlock() } if e := lockfile(f); e != nil { return e } defer unlockfile(f) now := time.Now().Unix() utime := syscall.Utimbuf{now, now} return syscall.Utime(p, &utime) }
func (v *TestableUnixVolume) TouchWithDate(locator string, lastPut time.Time) { err := syscall.Utime(v.blockPath(locator), &syscall.Utimbuf{lastPut.Unix(), lastPut.Unix()}) if err != nil { v.t.Fatal(err) } }