From e7f2935333d0f14433c2a3ce2c6da75b3b939ead Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Tue, 14 Nov 2017 21:00:08 +0000 Subject: [PATCH] vfs: decode flags in Open/OpenFile for debug --- vfs/file.go | 1 + vfs/read_write.go | 3 +-- vfs/vfs.go | 37 +++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/vfs/file.go b/vfs/file.go index 11832e18f..c15992445 100644 --- a/vfs/file.go +++ b/vfs/file.go @@ -319,6 +319,7 @@ func (f *File) VFS() *VFS { // // We ignore O_SYNC and O_EXCL func (f *File) Open(flags int) (fd Handle, err error) { + defer fs.Trace(f, "flags=%s", decodeOpenFlags(flags))("fd=%v, err=%v", &fd, &err) var ( write bool // if set need write support read bool // if set need read support diff --git a/vfs/read_write.go b/vfs/read_write.go index 7301277ca..ed7cfea7b 100644 --- a/vfs/read_write.go +++ b/vfs/read_write.go @@ -95,8 +95,7 @@ func (fh *RWFileHandle) openPending(truncate bool) (err error) { if rdwrMode != os.O_RDONLY { fh.file.addWriters(1) } - - fs.Debugf(fh.remote, "Opening cached copy with flags=0x%02X", fh.flags) + fs.Debugf(fh.remote, "Opening cached copy with flags=%s", decodeOpenFlags(fh.flags)) fd, err := os.OpenFile(fh.osPath, fh.flags|os.O_CREATE, 0600) if err != nil { return errors.Wrap(err, "cache open file failed") diff --git a/vfs/vfs.go b/vfs/vfs.go index b34bcd4ee..0afa293f9 100644 --- a/vfs/vfs.go +++ b/vfs/vfs.go @@ -306,8 +306,45 @@ func (vfs *VFS) StatParent(name string) (dir *Dir, leaf string, err error) { return dir, leaf, nil } +// decodeOpenFlags returns a string representing the open flags +func decodeOpenFlags(flags int) string { + var out []string + rdwrMode := flags & accessModeMask + switch rdwrMode { + case os.O_RDONLY: + out = append(out, "O_RDONLY") + case os.O_WRONLY: + out = append(out, "O_WRONLY") + case os.O_RDWR: + out = append(out, "O_RDWR") + default: + out = append(out, fmt.Sprintf("0x%X", rdwrMode)) + } + if flags&os.O_APPEND != 0 { + out = append(out, "O_APPEND") + } + if flags&os.O_CREATE != 0 { + out = append(out, "O_CREATE") + } + if flags&os.O_EXCL != 0 { + out = append(out, "O_EXCL") + } + if flags&os.O_SYNC != 0 { + out = append(out, "O_SYNC") + } + if flags&os.O_TRUNC != 0 { + out = append(out, "O_TRUNC") + } + flags &^= accessModeMask | os.O_APPEND | os.O_CREATE | os.O_EXCL | os.O_SYNC | os.O_TRUNC + if flags != 0 { + out = append(out, fmt.Sprintf("0x%X", flags)) + } + return strings.Join(out, "|") +} + // OpenFile a file according to the flags and perm provided func (vfs *VFS) OpenFile(name string, flags int, perm os.FileMode) (fd Handle, err error) { + defer fs.Trace(name, "flags=%s, perm=%v", decodeOpenFlags(flags), perm)("fd=%v, err=%v", &fd, &err) node, err := vfs.Stat(name) if err != nil { if err != ENOENT || flags&os.O_CREATE == 0 {