rotation_windows.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //go:build windows
  2. /*
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at https://mozilla.org/MPL/2.0/.
  6. */
  7. package main
  8. import (
  9. "golang.org/x/sys/windows"
  10. )
  11. /*
  12. See
  13. * https://cs.opensource.google/go/go/+/refs/tags/go1.22.3:src/os/stat_windows.go;l=102;drc=1d3088effdcb03612dd03eb24feee4aa92070a63
  14. * https://cs.opensource.google/go/go/+/master:src/os/types_windows.go;l=46;drc=dc548bb322387039a12000c04e5b9083a0511639
  15. * https://cs.opensource.google/go/x/sys/+/refs/tags/v0.20.0:windows/zsyscall_windows.go;l=2245;drc=1a50d9738bb732c5dc3de113b13fb9ac06da1369
  16. * https://cs.opensource.google/go/x/sys/+/refs/tags/v0.20.0:windows/zsyscall_windows.go;l=261;drc=1a50d9738bb732c5dc3de113b13fb9ac06da1369
  17. */
  18. type FileSysInfo = *windows.ByHandleFileInformation
  19. const FileSysInfoStr = "*windows.ByHandleFileInformation"
  20. func fileWasRotated(newSysInfo, curSysInfo FileSysInfo) bool {
  21. /*
  22. There may very well be corner cases where the following
  23. test triggers a frivolous reopen, but this should occur
  24. infrequently.
  25. If rotation uses ReplaceFileA/W, no change in file ID
  26. will occur, and we will be entirely reliant on detecting
  27. a sudden drop in file size.
  28. If the rotation condition is based on time rather than
  29. size, it is not inconceivable that one empty file could
  30. be swapped for another without detection. In this case,
  31. comparing creation times becomes compulsory.
  32. See
  33. * https://learn.microsoft.com/en-us/windows/win32/api/fileapi/ns-fileapi-by_handle_file_information#remarks
  34. * https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-replacefilew#remarks
  35. * https://cs.opensource.google/go/go/+/master:src/syscall/types_windows.go;l=348;drc=d42cd452dcca76819dd385a7775f8453d6255dbd
  36. */
  37. return newSysInfo.FileIndexHigh != curSysInfo.FileIndexHigh ||
  38. newSysInfo.FileIndexLow != curSysInfo.FileIndexLow ||
  39. newSysInfo.FileSizeHigh < curSysInfo.FileSizeHigh ||
  40. (newSysInfo.FileSizeHigh == curSysInfo.FileSizeHigh &&
  41. newSysInfo.FileSizeLow < curSysInfo.FileSizeLow) ||
  42. newSysInfo.CreationTime.Nanoseconds() > curSysInfo.CreationTime.Nanoseconds()
  43. }