本文整理汇总了Golang中os.Chtimes函数的典型用法代码示例。如果您正苦于以下问题:Golang Chtimes函数的具体用法?Golang Chtimes怎么用?Golang Chtimes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Chtimes函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
flag.Parse()
mtime := time.Unix(*flagTime, 0)
for _, arg := range flag.Args() {
err := os.Chtimes(arg, mtime, mtime)
if err == nil {
continue
}
if *flagNoCreate {
reportPathError(err)
continue
}
f, err := os.Create(arg)
if err != nil {
reportPathError(err)
continue
}
err = os.Chtimes(arg, mtime, mtime)
f.Close()
if err != nil {
reportPathError(err)
continue
}
}
}
示例2: writeTime
func writeTime(path string, t time.Time) {
err := os.Chtimes(path, t, t)
if os.IsNotExist(err) {
ioutil.WriteFile(path, nil, 0666)
os.Chtimes(path, t, t)
}
}
示例3: TestStaticStale
func TestStaticStale(t *testing.T) {
srv, wd := stubServer(t)
defer os.RemoveAll(wd)
f, err := ioutil.TempFile(wd, "paste")
check(t, err)
name := f.Name()
defer os.Remove(name)
f.Write([]byte("foo"))
f.Close()
asset, err := newStatic(srv, "bar", name)
if err != nil {
t.Errorf("ran into error: %s", err.Error())
}
if asset.Stale() {
t.Errorf("shouldn't be stale when just created")
}
future := time.Now().Add(5 * time.Second)
check(t, os.Chtimes(name, future, future))
if asset.Stale() {
t.Errorf("shouldn't be stale with old contents")
}
check(t, ioutil.WriteFile(name, []byte("bar"), 0644))
check(t, os.Chtimes(name, future, future))
if !asset.Stale() {
t.Errorf("should be stale now with new contents")
}
}
示例4: TestProcessedSingleFile
func TestProcessedSingleFile(t *testing.T) {
srv, wd := stubServer(t)
defer os.RemoveAll(wd)
stubFile(t, wd, "foo.js", "bar")
file := filepath.Join(wd, "foo.js")
asset, err := newProcessed(srv, "foo.js", file)
if err != nil {
t.Fatalf("ran into error: %s", err.Error())
}
bits, err := ioutil.ReadFile(asset.Pathname())
check(t, err)
if string(bits) != "bar" {
t.Errorf("should contain 'bar'")
}
if asset.Stale() {
t.Errorf("shouldn't be stale when just created")
}
future := time.Now().Add(5 * time.Second)
check(t, os.Chtimes(file, future, future))
if asset.Stale() {
t.Errorf("shouldn't be stale with old contents")
}
check(t, ioutil.WriteFile(file, []byte("foo"), 0644))
check(t, os.Chtimes(file, future, future))
if !asset.Stale() {
t.Errorf("should be stale now with new contents")
}
}
示例5: TestProcessedConcatenates
func TestProcessedConcatenates(t *testing.T) {
srv, wd := stubServer(t)
defer os.RemoveAll(wd)
stubFile(t, wd, "foo.js", "//= require bar\nfoo")
stubFile(t, wd, "bar.js", "//= require baz.js\nbar")
stubFile(t, wd, "baz.js", "baz")
file := filepath.Join(wd, "foo.js")
asset, err := newProcessed(srv, "foo.js", file)
if err != nil {
t.Fatalf("ran into error: %s", err.Error())
}
bits, err := ioutil.ReadFile(asset.Pathname())
check(t, err)
if string(bits) != "baz\n//= require baz.js\nbar\n//= require bar\nfoo" {
t.Errorf("wrong contents:\n%s", string(bits))
}
if asset.Stale() {
t.Errorf("shouldn't be stale")
}
future := time.Now().Add(5 * time.Second)
check(t, os.Chtimes(filepath.Join(wd, "bar.js"), future, future))
if asset.Stale() {
t.Errorf("shouldn't be stale with same contents")
}
check(t, ioutil.WriteFile(filepath.Join(wd, "baz.js"), []byte("baz2"), 0644))
check(t, os.Chtimes(filepath.Join(wd, "baz.js"), future, future))
if !asset.Stale() {
t.Errorf("should be stale now with new contents")
}
}
示例6: Run
// Run compiles and links the Go source file on args[0] and
// runs it with arguments args[1:].
func Run(args []string) error {
sourcefile := args[0]
rundir, runfile, err := RunFile(sourcefile)
if err != nil {
return err
}
compile := false
// Now must be called before Stat of sourcefile below,
// so that changing the file between Stat and Chtimes still
// causes the file to be updated on the next run.
now := time.Now()
sstat, err := os.Stat(sourcefile)
if err != nil {
return err
}
rstat, err := os.Stat(runfile)
switch {
case err != nil:
compile = true
case rstat.Mode()&(os.ModeDir|os.ModeSymlink|os.ModeDevice|os.ModeNamedPipe|os.ModeSocket) != 0:
return errors.New("not a file: " + runfile)
case rstat.ModTime().Before(sstat.ModTime()) || rstat.Mode().Perm()&0700 != 0700:
compile = true
default:
// We have spare cycles. Maybe remove old files.
if err := os.Chtimes(runfile, now, now); err == nil {
CleanDir(rundir, now)
}
}
for retry := 3; retry > 0; retry-- {
if compile {
err := Compile(sourcefile, runfile)
if err != nil {
return err
}
// If sourcefile was changed, will be updated on next run.
os.Chtimes(runfile, sstat.ModTime(), sstat.ModTime())
}
err = syscall.Exec(runfile, args, os.Environ())
if os.IsNotExist(err) {
// Got cleaned up under our feet.
compile = true
continue
}
break
}
if err != nil {
panic("exec returned but succeeded")
}
return err
}
示例7: TestServeFUSE_Chtimes
func TestServeFUSE_Chtimes(t *testing.T) {
maybeSkipTest(t)
fs := fusetestFileSystem()
stableT := time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)
fusetestCommon(t, fs, func(mountpoint string) {
dirpath := path.Join(mountpoint, "hokkaido")
if err := os.Mkdir(dirpath, 0755); err != nil {
t.Errorf("Failed to mkdir: %v", err)
return
}
filepath := path.Join(dirpath, "otaru.txt")
if err := ioutil.WriteFile(filepath, HelloWorld, 0644); err != nil {
t.Errorf("failed to write file: %v", err)
return
}
if err := os.Chtimes(dirpath, stableT, stableT); err != nil {
t.Errorf("Failed to chtimes dir: %v", err)
return
}
if err := os.Chtimes(filepath, stableT, stableT); err != nil {
t.Errorf("Failed to chtimes file: %v", err)
return
}
fi, err := os.Stat(dirpath)
if err != nil {
t.Errorf("Failed to stat dir: %v", err)
return
}
if !fi.IsDir() {
t.Errorf("dir isn't dir!")
}
if fi.ModTime().Sub(stableT) > time.Second {
t.Errorf("invalid modifiedT! %v", fi.ModTime())
}
fi, err = os.Stat(filepath)
if err != nil {
t.Errorf("Failed to stat file: %v", err)
return
}
if fi.IsDir() {
t.Errorf("file is dir!")
}
if fi.ModTime().Sub(stableT) > time.Second {
t.Errorf("invalid modifiedT! %v", fi.ModTime())
}
})
}
示例8: TestMain
func TestMain(m *testing.M) {
timeStart := time.Unix(0, 0)
time1 := timeStart.Add(time.Duration(1))
os.Chtimes("test/proc/1/status", time1, time1)
time20 := timeStart.Add(time.Duration(20))
os.Chtimes("test/proc/20/status", time20, time20)
time21 := timeStart.Add(time.Duration(21))
os.Chtimes("test/proc/21/status", time21, time21)
time30 := timeStart.Add(time.Duration(30))
os.Chtimes("test/proc/30/status", time30, time30)
os.Exit(m.Run())
}
示例9: buildFS
func buildFS(dir string, files []*buildFileInfo) error {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
}
dirs := []*buildFileInfo{}
for _, f := range files {
p := filepath.Join(dir, f.path)
switch f.typeflag {
case tar.TypeDir:
dirs = append(dirs, f)
if err := os.MkdirAll(p, f.mode); err != nil {
return err
}
dir, err := os.Open(p)
if err != nil {
return err
}
if err := dir.Chmod(f.mode); err != nil {
dir.Close()
return err
}
dir.Close()
case tar.TypeReg:
err := ioutil.WriteFile(p, []byte(f.contents), f.mode)
if err != nil {
return err
}
}
if err := os.Chtimes(p, f.atime, f.mtime); err != nil {
return err
}
}
// Restore dirs atime and mtime. This has to be done after extracting
// as a file extraction will change its parent directory's times.
for _, d := range dirs {
p := filepath.Join(dir, d.path)
if err := os.Chtimes(p, d.atime, d.mtime); err != nil {
return err
}
}
defaultTime := time.Unix(0, 0)
// Restore the base dir time as it will be changed by the previous extractions
if err := os.Chtimes(dir, defaultTime, defaultTime); err != nil {
return err
}
return nil
}
示例10: SaveTarReaderToPath
func SaveTarReaderToPath(logger SimpleLogger, bodyReader io.Reader, savePath string) {
tarReader := tar.NewReader(bodyReader)
foundEndOfTar := false
for {
hdr, err := tarReader.Next()
if err == io.EOF {
// end of tar archive
break
}
CheckError(err) //Check after checking for EOF
if hdr.Name == END_OF_TAR_FILENAME {
foundEndOfTar = true
continue
}
relativePath := hdr.Name
if hdr.FileInfo().IsDir() {
fullDestinationDirPath := filepath.Join(savePath, relativePath)
logger.Debug("(TAR) Creating directory %s", fullDestinationDirPath)
os.MkdirAll(fullDestinationDirPath, os.FileMode(hdr.Mode))
defer os.Chtimes(fullDestinationDirPath, hdr.AccessTime, hdr.ModTime)
} else {
fullDestinationFilePath := filepath.Join(savePath, relativePath)
if val, ok := hdr.Xattrs["SINGLE_FILE_ONLY"]; ok && val == "1" {
fullDestinationFilePath = savePath
}
err = os.MkdirAll(filepath.Dir(fullDestinationFilePath), os.FileMode(hdr.Mode))
CheckError(err)
logger.Debug("(TAR) Saving file %s", fullDestinationFilePath)
file, err := os.OpenFile(fullDestinationFilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(hdr.Mode))
CheckError(err)
defer func() {
file.Close()
os.Chtimes(fullDestinationFilePath, hdr.AccessTime, hdr.ModTime)
}()
_, err = io.Copy(file, tarReader)
CheckError(err)
}
}
if !foundEndOfTar {
panic("TAR stream validation failed, something has gone wrong during the transfer.")
}
}
示例11: Extract
func (e *Export) Extract(r io.Reader) error {
err := os.MkdirAll(e.Path, 0755)
if err != nil {
return err
}
t := tar.NewReader(r)
for {
header, err := t.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if header.Name == "." || header.Name == ".." || header.Name == "./" {
continue
}
fn := path.Join(e.Path, header.Name)
if header.FileInfo().IsDir() {
err = os.Mkdir(fn, header.FileInfo().Mode())
if err != nil {
return err
}
err := os.Chtimes(fn, time.Now().UTC(), header.FileInfo().ModTime())
if err != nil {
return err
}
continue
}
item, err := os.OpenFile(fn, os.O_CREATE|os.O_WRONLY, header.FileInfo().Mode())
if err != nil {
return err
}
if _, err := io.Copy(item, t); err != nil {
log.Fatalln(err)
}
item.Close()
err = os.Chtimes(fn, time.Now().UTC(), header.FileInfo().ModTime())
if err != nil {
return err
}
}
}
示例12: serializeAsDesktopEntry
func (f *File) serializeAsDesktopEntry(destPath string, urlMExt *urlMimeTypeExt) (int, error) {
deskEnt := f.toDesktopEntry(urlMExt)
handle, err := os.Create(destPath)
if err != nil {
return 0, err
}
defer func() {
handle.Close()
chmodErr := os.Chmod(destPath, 0755)
if chmodErr != nil {
fmt.Fprintf(os.Stderr, "%s: [desktopEntry]::chmod %v\n", destPath, chmodErr)
}
chTimeErr := os.Chtimes(destPath, f.ModTime, f.ModTime)
if chTimeErr != nil {
fmt.Fprintf(os.Stderr, "%s: [desktopEntry]::chtime %v\n", destPath, chTimeErr)
}
}()
icon := strings.Replace(deskEnt.icon, UnescapedPathSep, MimeTypeJoiner, -1)
return fmt.Fprintf(handle, "[Desktop Entry]\nIcon=%s\nName=%s\nType=%s\nURL=%s\n",
icon, deskEnt.name, LinkKey, deskEnt.url)
}
示例13: JSONDump
// JSONDump dumps the data structure using JSON format on the filesystem.
func JSONDump(filePath string, data interface{}, modTime time.Time) error {
output, err := json.MarshalIndent(data, "", " ")
if err != nil {
return err
}
dirPath, _ := path.Split(filePath)
err = os.MkdirAll(dirPath, 0755)
if err != nil {
return err
}
fd, err := os.OpenFile(filePath, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer fd.Close()
fd.Write(output)
fd.Write([]byte("\n"))
os.Chtimes(filePath, modTime, modTime)
return nil
}
示例14: generateOneFile
func generateOneFile(fd io.ReadSeeker, p1 string, s int64) error {
src := io.LimitReader(&inifiteReader{fd}, int64(s))
dst, err := os.Create(p1)
if err != nil {
return err
}
_, err = io.Copy(dst, src)
if err != nil {
return err
}
err = dst.Close()
if err != nil {
return err
}
_ = os.Chmod(p1, os.FileMode(rand.Intn(0777)|0400))
t := time.Now().Add(-time.Duration(rand.Intn(30*86400)) * time.Second)
err = os.Chtimes(p1, t, t)
if err != nil {
return err
}
return nil
}
示例15: KillPod
// KillPod invokes 'systemctl kill' to kill the unit that runs the pod.
// TODO(yifan): Handle network plugin.
func (r *Runtime) KillPod(pod *api.Pod, runningPod kubecontainer.Pod) error {
glog.V(4).Infof("Rkt is killing pod: name %q.", runningPod.Name)
serviceName := makePodServiceFileName(runningPod.ID)
r.generateEvents(&runningPod, "Killing", nil)
for _, c := range runningPod.Containers {
r.containerRefManager.ClearRef(c.ID)
}
// Touch the systemd service file to update the mod time so it will
// not be garbage collected too soon.
if err := os.Chtimes(serviceFilePath(serviceName), time.Now(), time.Now()); err != nil {
glog.Errorf("rkt: Failed to change the modification time of the service file %q: %v", serviceName, err)
return err
}
// Since all service file have 'KillMode=mixed', the processes in
// the unit's cgroup will receive a SIGKILL if the normal stop timeouts.
reschan := make(chan string)
_, err := r.systemd.StopUnit(serviceName, "replace", reschan)
if err != nil {
glog.Errorf("rkt: Failed to stop unit %q: %v", serviceName, err)
return err
}
res := <-reschan
if res != "done" {
glog.Errorf("rkt: Failed to stop unit %q: %s", serviceName, res)
return err
}
return nil
}