fileinfo.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package vfs
  15. import (
  16. "os"
  17. "path"
  18. "time"
  19. )
  20. // FileInfo implements os.FileInfo for a Cloud Storage file.
  21. type FileInfo struct {
  22. name string
  23. sizeInBytes int64
  24. modTime time.Time
  25. mode os.FileMode
  26. }
  27. // NewFileInfo creates file info.
  28. func NewFileInfo(name string, isDirectory bool, sizeInBytes int64, modTime time.Time, fullName bool) *FileInfo {
  29. mode := os.FileMode(0644)
  30. if isDirectory {
  31. mode = os.FileMode(0755) | os.ModeDir
  32. }
  33. if !fullName {
  34. // we have always Unix style paths here
  35. name = path.Base(name)
  36. }
  37. return &FileInfo{
  38. name: name,
  39. sizeInBytes: sizeInBytes,
  40. modTime: modTime,
  41. mode: mode,
  42. }
  43. }
  44. // Name provides the base name of the file.
  45. func (fi *FileInfo) Name() string {
  46. return fi.name
  47. }
  48. // Size provides the length in bytes for a file.
  49. func (fi *FileInfo) Size() int64 {
  50. return fi.sizeInBytes
  51. }
  52. // Mode provides the file mode bits
  53. func (fi *FileInfo) Mode() os.FileMode {
  54. return fi.mode
  55. }
  56. // ModTime provides the last modification time.
  57. func (fi *FileInfo) ModTime() time.Time {
  58. return fi.modTime
  59. }
  60. // IsDir provides the abbreviation for Mode().IsDir()
  61. func (fi *FileInfo) IsDir() bool {
  62. return fi.mode&os.ModeDir != 0
  63. }
  64. // SetMode sets the file mode
  65. func (fi *FileInfo) SetMode(mode os.FileMode) {
  66. fi.mode = mode
  67. }
  68. // Sys provides the underlying data source (can return nil)
  69. func (fi *FileInfo) Sys() any {
  70. return nil
  71. }