main
1//go:build plan9
2// +build plan9
3
4package osfs
5
6import (
7 "io"
8 "os"
9 "path/filepath"
10 "syscall"
11)
12
13func (f *file) Lock() error {
14 // Plan 9 uses a mode bit instead of explicit lock/unlock syscalls.
15 //
16 // Per http://man.cat-v.org/plan_9/5/stat: “Exclusive use files may be open
17 // for I/O by only one fid at a time across all clients of the server. If a
18 // second open is attempted, it draws an error.”
19 //
20 // There is no obvious way to implement this function using the exclusive use bit.
21 // See https://golang.org/src/cmd/go/internal/lockedfile/lockedfile_plan9.go
22 // for how file locking is done by the go tool on Plan 9.
23 return nil
24}
25
26func (f *file) Unlock() error {
27 return nil
28}
29
30func rename(from, to string) error {
31 // If from and to are in different directories, copy the file
32 // since Plan 9 does not support cross-directory rename.
33 if filepath.Dir(from) != filepath.Dir(to) {
34 fi, err := os.Stat(from)
35 if err != nil {
36 return &os.LinkError{"rename", from, to, err}
37 }
38 if fi.Mode().IsDir() {
39 return &os.LinkError{"rename", from, to, syscall.EISDIR}
40 }
41 fromFile, err := os.Open(from)
42 if err != nil {
43 return &os.LinkError{"rename", from, to, err}
44 }
45 toFile, err := os.OpenFile(to, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, fi.Mode())
46 if err != nil {
47 return &os.LinkError{"rename", from, to, err}
48 }
49 _, err = io.Copy(toFile, fromFile)
50 if err != nil {
51 return &os.LinkError{"rename", from, to, err}
52 }
53
54 // Copy mtime and mode from original file.
55 // We need only one syscall if we avoid os.Chmod and os.Chtimes.
56 dir := fi.Sys().(*syscall.Dir)
57 var d syscall.Dir
58 d.Null()
59 d.Mtime = dir.Mtime
60 d.Mode = dir.Mode
61 if err = dirwstat(to, &d); err != nil {
62 return &os.LinkError{"rename", from, to, err}
63 }
64
65 // Remove original file.
66 err = os.Remove(from)
67 if err != nil {
68 return &os.LinkError{"rename", from, to, err}
69 }
70 return nil
71 }
72 return os.Rename(from, to)
73}
74
75func dirwstat(name string, d *syscall.Dir) error {
76 var buf [syscall.STATFIXLEN]byte
77
78 n, err := d.Marshal(buf[:])
79 if err != nil {
80 return &os.PathError{"dirwstat", name, err}
81 }
82 if err = syscall.Wstat(name, buf[:n]); err != nil {
83 return &os.PathError{"dirwstat", name, err}
84 }
85 return nil
86}
87
88func umask(new int) func() {
89 return func() {
90 }
91}