|
21 | 21 | package common
|
22 | 22 |
|
23 | 23 | import (
|
| 24 | + "encoding/binary" |
| 25 | + "fmt" |
24 | 26 | "os"
|
25 | 27 | "syscall"
|
| 28 | + "time" |
| 29 | + |
| 30 | + "github.com/pkg/xattr" |
| 31 | + "golang.org/x/sys/unix" |
| 32 | +) |
| 33 | + |
| 34 | +// Extended Attribute (xattr) keys for fetching various information from Linux cifs client. |
| 35 | +const ( |
| 36 | + CIFS_XATTR_CREATETIME = "user.cifs.creationtime" // File creation time. |
| 37 | + CIFS_XATTR_ATTRIB = "user.cifs.dosattrib" // FileAttributes. |
| 38 | + CIFS_XATTR_CIFS_ACL = "system.cifs_acl" // DACL only. |
| 39 | + CIFS_XATTR_CIFS_NTSD = "system.cifs_ntsd" // Owner, Group, DACL. |
| 40 | + CIFS_XATTR_CIFS_NTSD_FULL = "system.cifs_ntsd_full" // Owner, Group, DACL, SACL. |
26 | 41 | )
|
27 | 42 |
|
| 43 | +// 100-nanosecond intervals from Windows Epoch (January 1, 1601) to Unix Epoch (January 1, 1970). |
| 44 | +const ( |
| 45 | + TICKS_FROM_WINDOWS_EPOCH_TO_UNIX_EPOCH = 116444736000000000 |
| 46 | +) |
| 47 | + |
| 48 | +// windows.Filetime. |
| 49 | +type Filetime struct { |
| 50 | + LowDateTime uint32 |
| 51 | + HighDateTime uint32 |
| 52 | +} |
| 53 | + |
| 54 | +// windows.ByHandleFileInformation |
| 55 | +type ByHandleFileInformation struct { |
| 56 | + FileAttributes uint32 |
| 57 | + CreationTime Filetime |
| 58 | + LastAccessTime Filetime |
| 59 | + LastWriteTime Filetime |
| 60 | + VolumeSerialNumber uint32 |
| 61 | + FileSizeHigh uint32 |
| 62 | + FileSizeLow uint32 |
| 63 | + NumberOfLinks uint32 |
| 64 | + FileIndexHigh uint32 |
| 65 | + FileIndexLow uint32 |
| 66 | +} |
| 67 | + |
| 68 | +// Nanoseconds converts Filetime (as ticks since Windows Epoch) to nanoseconds since Unix Epoch (January 1, 1970). |
| 69 | +func (ft *Filetime) Nanoseconds() int64 { |
| 70 | + // 100-nanosecond intervals (ticks) since Windows Epoch (January 1, 1601). |
| 71 | + nsec := int64(ft.HighDateTime)<<32 + int64(ft.LowDateTime) |
| 72 | + |
| 73 | + // 100-nanosecond intervals since Unix Epoch (January 1, 1970). |
| 74 | + nsec -= TICKS_FROM_WINDOWS_EPOCH_TO_UNIX_EPOCH |
| 75 | + |
| 76 | + // nanoseconds since Unix Epoch. |
| 77 | + return nsec * 100 |
| 78 | +} |
| 79 | + |
| 80 | +// Convert nanoseconds since Unix Epoch (January 1, 1970) to Filetime since Windows Epoch (January 1, 1601). |
| 81 | +func NsecToFiletime(nsec int64) Filetime { |
| 82 | + // 100-nanosecond intervals since Unix Epoch (January 1, 1970). |
| 83 | + nsec /= 100 |
| 84 | + |
| 85 | + // 100-nanosecond intervals since Windows Epoch (January 1, 1601). |
| 86 | + nsec += TICKS_FROM_WINDOWS_EPOCH_TO_UNIX_EPOCH |
| 87 | + |
| 88 | + return Filetime{LowDateTime: uint32(nsec & 0xFFFFFFFF), HighDateTime: uint32(nsec >> 32)} |
| 89 | +} |
| 90 | + |
| 91 | +// WindowsTicksToUnixNano converts ticks (100-ns intervals) since Windows Epoch to nanoseconds since Unix Epoch. |
| 92 | +func WindowsTicksToUnixNano(ticks int64) int64 { |
| 93 | + // 100-nanosecond intervals since Unix Epoch (January 1, 1970). |
| 94 | + ticks -= TICKS_FROM_WINDOWS_EPOCH_TO_UNIX_EPOCH |
| 95 | + |
| 96 | + // nanoseconds since Unix Epoch (January 1, 1970). |
| 97 | + return ticks * 100 |
| 98 | +} |
| 99 | + |
| 100 | +// UnixNanoToWindowsTicks converts nanoseconds since Unix Epoch to ticks since Windows Epoch. |
| 101 | +func UnixNanoToWindowsTicks(nsec int64) int64 { |
| 102 | + // 100-nanosecond intervals since Unix Epoch (January 1, 1970). |
| 103 | + nsec /= 100 |
| 104 | + |
| 105 | + // 100-nanosecond intervals since Windows Epoch (January 1, 1601). |
| 106 | + nsec += TICKS_FROM_WINDOWS_EPOCH_TO_UNIX_EPOCH |
| 107 | + |
| 108 | + return nsec |
| 109 | +} |
| 110 | + |
| 111 | +// StatxTimestampToFiletime converts the unix StatxTimestamp (sec, nsec) to the Windows' Filetime. |
| 112 | +// Note that StatxTimestamp is from Unix Epoch while Filetime holds time from Windows Epoch. |
| 113 | +func StatxTimestampToFiletime(ts unix.StatxTimestamp) Filetime { |
| 114 | + return NsecToFiletime(ts.Sec*int64(time.Second) + int64(ts.Nsec)) |
| 115 | +} |
| 116 | + |
| 117 | +func GetFileInformation(path string) (ByHandleFileInformation, error) { |
| 118 | + var stx unix.Statx_t |
| 119 | + |
| 120 | + // We want all attributes including Btime (aka creation time). |
| 121 | + // For consistency with Windows implementation we pass flags==0 which causes it to follow symlinks. |
| 122 | + err := unix.Statx(unix.AT_FDCWD, path, 0 /* flags */, unix.STATX_ALL, &stx) |
| 123 | + if err == unix.ENOSYS || err == unix.EPERM { |
| 124 | + panic(fmt.Errorf("statx syscall is not available: %v", err)) |
| 125 | + } else if err != nil { |
| 126 | + return ByHandleFileInformation{}, fmt.Errorf("statx(%s) failed: %v", path, err) |
| 127 | + } |
| 128 | + |
| 129 | + // For getting FileAttributes we need to query the CIFS_XATTR_ATTRIB extended attribute. |
| 130 | + // Note: This doesn't necessarily cause a new QUERY_PATH_INFO call to the SMB server, instead |
| 131 | + // the value cached in the inode (likely as a result of the above Statx call) will be |
| 132 | + // returned. |
| 133 | + xattrbuf, err := xattr.Get(path, CIFS_XATTR_ATTRIB) |
| 134 | + if err != nil { |
| 135 | + return ByHandleFileInformation{}, |
| 136 | + fmt.Errorf("xattr.Get(%s, %s) failed: %v", path, CIFS_XATTR_ATTRIB, err) |
| 137 | + } |
| 138 | + |
| 139 | + var info ByHandleFileInformation |
| 140 | + |
| 141 | + info.FileAttributes = binary.LittleEndian.Uint32(xattrbuf) |
| 142 | + |
| 143 | + info.CreationTime = StatxTimestampToFiletime(stx.Btime) |
| 144 | + info.LastAccessTime = StatxTimestampToFiletime(stx.Atime) |
| 145 | + info.LastWriteTime = StatxTimestampToFiletime(stx.Mtime) |
| 146 | + |
| 147 | + // TODO: Do we need this? |
| 148 | + info.VolumeSerialNumber = 0 |
| 149 | + |
| 150 | + info.FileSizeHigh = uint32(stx.Size >> 32) |
| 151 | + info.FileSizeLow = uint32(stx.Size & 0xFFFFFFFF) |
| 152 | + |
| 153 | + info.NumberOfLinks = stx.Nlink |
| 154 | + |
| 155 | + info.FileIndexHigh = uint32(stx.Ino >> 32) |
| 156 | + info.FileIndexLow = uint32(stx.Ino & 0xFFFFFFFF) |
| 157 | + |
| 158 | + return info, nil |
| 159 | +} |
| 160 | + |
28 | 161 | func CreateFileOfSizeWithWriteThroughOption(destinationPath string, fileSize int64, writeThrough bool, t FolderCreationTracker, forceIfReadOnly bool) (*os.File, error) {
|
29 | 162 | // forceIfReadOnly is not used on this OS
|
30 | 163 |
|
|
0 commit comments