stdlib fix for Windows file inode (aka File Index Number)

220 views
Skip to first unread message

Liam

unread,
Nov 16, 2019, 8:08:49 PM11/16/19
to golang-dev
Because Go for Windows can't provide a file's inode/index-number, I patched my stdlib as follows.

Comments welcome; let me know if you find it useful.


diff --git a/src/os/stat_windows.go b/src/os/stat_windows.go
index fd22ef2..7e80178 100644
--- a/src/os/stat_windows.go
+++ b/src/os/stat_windows.go
@@ -132,3 +132,29 @@ func lstatNolog(name string) (FileInfo, error) {
        attrs |= syscall.FILE_FLAG_OPEN_REPARSE_POINT
        return stat("Lstat", name, attrs)
 }
+
+// added for Your_Project_Name
+
+func StatWindows(name string) (*fileStat, error) {
+       if len(name) == 0 {
+               return nil, &PathError{"StatWindows", name, syscall.Errno(syscall.ERROR_PATH_NOT_FOUND)}
+       }
+       if isNulName(name) {
+               return &devNullStat, nil
+       }
+       namep, err := syscall.UTF16PtrFromString(fixLongPath(name))
+       if err != nil {
+               return nil, &PathError{"StatWindows", name, err}
+       }
+       h, err := syscall.CreateFile(namep, 0, 0, nil,
+               syscall.OPEN_EXISTING, syscall.FILE_FLAG_BACKUP_SEMANTICS, 0)
+       if err != nil {
+               return nil, &PathError{"CreateFile", name, err}
+       }
+       defer syscall.CloseHandle(h)
+       return newFileStatFromGetFileInformationByHandle(name, h)
+}
+
+func (fs *fileStat) GetIno() (uint32, uint64) {
+       return fs.vol, (uint64(fs.idxhi) << 32) | uint64(fs.idxlo)
+}

Brad Fitzpatrick

unread,
Nov 17, 2019, 12:23:25 AM11/17/19
to Liam, golang-dev
Do you know about https://golang.org/pkg/os/#FileInfo.Sys (the Sys method there)?

Using that seems better than adding an OS-specific thing into a portable package (os), and having an exported func return an unexported type. That won't godoc well.


--
You received this message because you are subscribed to the Google Groups "golang-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-dev+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-dev/fd888c32-e5c7-4806-b46f-b9eae5d706d2%40googlegroups.com.

Liam Breck

unread,
Nov 17, 2019, 6:04:25 AM11/17/19
to Brad Fitzpatrick, golang-dev
This isn't an upstream suggestion. I'm just sharing a fix for an uncommon problem.

os.FileInfo.Sys() provides the inode on Unix, but not Windows, for either Stat() or File.Readdir(). I linked the thread where the idea of fixing that was abandoned.
Reply all
Reply to author
Forward
0 new messages