utils.go 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package fsdriver
  15. import (
  16. "fmt"
  17. "os"
  18. "syscall"
  19. "time"
  20. )
  21. // SFileInfo implements os.FileInfo interface
  22. type SFileInfo struct {
  23. name string
  24. size int64
  25. mode os.FileMode
  26. isDir bool
  27. stat *syscall.Stat_t
  28. }
  29. func NewFileInfo(name string, size int64, mode os.FileMode, isDir bool, stat *syscall.Stat_t) *SFileInfo {
  30. return &SFileInfo{name, size, mode, isDir, stat}
  31. }
  32. func (info SFileInfo) Name() string {
  33. return info.name
  34. }
  35. func (info SFileInfo) Size() int64 {
  36. return info.size
  37. }
  38. func (info SFileInfo) Mode() os.FileMode {
  39. return info.mode
  40. }
  41. func (info SFileInfo) IsDir() bool {
  42. return info.isDir
  43. }
  44. func (info SFileInfo) ModTime() time.Time {
  45. // TODO: impl
  46. return time.Now()
  47. }
  48. func (info SFileInfo) Sys() interface{} {
  49. return info.stat
  50. }
  51. func ModeStr2Bin(mode string) (uint32, error) {
  52. table := []map[byte]uint32{
  53. {'-': syscall.S_IRUSR, 'd': syscall.S_IFDIR, 'l': syscall.S_IFLNK},
  54. {'r': syscall.S_IRUSR},
  55. {'w': syscall.S_IWUSR},
  56. {'x': syscall.S_IXUSR, 's': syscall.S_ISUID},
  57. {'r': syscall.S_IRGRP},
  58. {'w': syscall.S_IWGRP},
  59. {'x': syscall.S_IXGRP, 's': syscall.S_ISGID},
  60. {'r': syscall.S_IROTH},
  61. {'w': syscall.S_IWOTH},
  62. {'x': syscall.S_IXOTH},
  63. }
  64. if len(mode) != len(table) {
  65. return 0, fmt.Errorf("Invalid mod %q", mode)
  66. }
  67. var ret uint32 = 0
  68. for i := 0; i < len(table); i++ {
  69. ret |= table[i][mode[i]]
  70. }
  71. return ret, nil
  72. }