udf_reader.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 isoutils
  15. import (
  16. "fmt"
  17. "io"
  18. "strings"
  19. "github.com/mogaika/udf"
  20. "yunion.io/x/log"
  21. )
  22. // isUdfFile 检测是否为UDF格式
  23. func isUdfFile(readerAt io.ReaderAt) bool {
  24. defer func() {
  25. recover()
  26. }()
  27. img := udf.NewUdfFromReader(readerAt)
  28. files := img.ReadDir(nil)
  29. if len(files) == 0 {
  30. return false
  31. }
  32. return true
  33. }
  34. // findUdfDir 在UDF中查找目录,返回目录的 FileEntry
  35. func (r *ISOFileReader) findUdfDir(path string) ([]udf.File, error) {
  36. if r.format != ISOFormatUDF || r.img == nil {
  37. return nil, fmt.Errorf("UDF格式未初始化")
  38. }
  39. // 规范化路径
  40. path = strings.Trim(path, "/")
  41. if path == "" {
  42. // 根目录
  43. return r.img.ReadDir(nil), nil
  44. }
  45. // 查找目录路径
  46. parts := strings.Split(path, "/")
  47. var entry *udf.FileEntry = nil
  48. currentDirEntry := r.img.ReadDir(entry) // 从根目录开始
  49. for i, part := range parts {
  50. if part == "" {
  51. continue
  52. }
  53. var found *udf.File
  54. // 在当前目录中查找
  55. for idx := range currentDirEntry {
  56. child := &currentDirEntry[idx]
  57. childName := child.Name()
  58. // 匹配文件名(不区分大小写)
  59. if strings.EqualFold(childName, part) {
  60. found = child
  61. break
  62. }
  63. }
  64. if found == nil {
  65. return nil, fmt.Errorf("目录不存在: %s", part)
  66. }
  67. // 如果是最后一个部分,返回该目录的内容
  68. if i == len(parts)-1 {
  69. return found.ReadDir(), nil
  70. }
  71. // 继续查找下一级目录
  72. currentDirEntry = found.ReadDir()
  73. }
  74. return nil, fmt.Errorf("未找到目录: %s", path)
  75. }
  76. // listUdfDir 列出UDF格式指定目录下的所有文件和子目录
  77. func (r *ISOFileReader) listUdfDir(path string) ([]ISO9660FileInfo, error) {
  78. if r.format != ISOFormatUDF {
  79. return nil, fmt.Errorf("此方法仅支持UDF格式")
  80. }
  81. // 获取目录内容
  82. children, err := r.findUdfDir(path)
  83. if err != nil {
  84. return nil, err
  85. }
  86. var files []ISO9660FileInfo
  87. for _, child := range children {
  88. fileInfo := ISO9660FileInfo{
  89. Name: child.Name(),
  90. IsDir: child.IsDir(),
  91. Size: child.Size(),
  92. Location: 0, // 使用库时不需要直接访问位置
  93. }
  94. files = append(files, fileInfo)
  95. }
  96. return files, nil
  97. }
  98. // GetFile 在UDF中查找指定路径的文件
  99. func (r *ISOFileReader) GetFile(path string) (*udf.File, error) {
  100. if r.format != ISOFormatUDF {
  101. return nil, fmt.Errorf("此方法仅支持UDF格式")
  102. }
  103. // UDF格式的原有逻辑
  104. parts := strings.Split(strings.Trim(path, "/"), "/")
  105. var entry *udf.FileEntry = nil
  106. currentDirEntry := r.img.ReadDir(entry) // 从根目录开始
  107. for i, part := range parts {
  108. if part == "" {
  109. continue
  110. }
  111. var found *udf.File
  112. // 在当前目录中查找
  113. for idx := range currentDirEntry {
  114. child := &currentDirEntry[idx]
  115. childName := child.Name()
  116. // 匹配文件名(UDF 文件名通常不包含版本号后缀)
  117. if childName == part {
  118. found = child
  119. break
  120. }
  121. }
  122. if found == nil {
  123. return nil, fmt.Errorf("文件或目录不存在: %s", part)
  124. }
  125. // 如果是最后一个部分,返回文件
  126. if i == len(parts)-1 {
  127. return found, nil
  128. }
  129. currentDirEntry = found.ReadDir()
  130. }
  131. return nil, fmt.Errorf("未找到文件: %s", path)
  132. }
  133. // readUdfFileContent 读取UDF格式文件内容
  134. func (r *ISOFileReader) readUdfFileContent(path string) (string, error) {
  135. file, err := r.GetFile(path)
  136. if err != nil {
  137. return "", fmt.Errorf("文件%s不存在: %v", path, err)
  138. }
  139. if file.IsDir() {
  140. return "", fmt.Errorf("路径%s是目录,不是文件", path)
  141. }
  142. reader := file.NewReader()
  143. if reader == nil {
  144. return "", fmt.Errorf("无法读取文件%s", path)
  145. }
  146. // 读取前10KB内容(足够识别特征)
  147. buf := make([]byte, 10*1024)
  148. n, err := reader.Read(buf)
  149. if err != nil && err != io.EOF {
  150. log.Errorf("读取UDF文件%s失败: %v", path, err)
  151. return "", fmt.Errorf("读取文件%s失败: %v", path, err)
  152. }
  153. return strings.TrimSpace(string(buf[:n])), nil
  154. }