command_test.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. package main
  2. //
  3. //func TestCommand(t *testing.T) {
  4. // bytes := make([]byte, 0)
  5. // bytes = append(bytes, 0x2a)
  6. // command := "你好1"
  7. // cmd := []byte(command)
  8. // l := util.IntToBytes(int32(len(cmd)))
  9. // bytes = append(bytes, l...)
  10. // bytes = append(bytes, cmd...)
  11. // bytes = append(bytes, util.Int16ToBytes(util.Crc16CCITT(cmd))...)
  12. // fmt.Println(util.ToHexBytes(bytes))
  13. //}
  14. //func TestGetFiles(t *testing.T) {
  15. // // 标准用法
  16. // result, err := GetPathInfo("D:\\project\\my\\file-manger\\frontend\\node_modules")
  17. // if err != nil {
  18. // fmt.Printf("错误: %+v\n", err)
  19. // return
  20. // }
  21. // fmt.Printf("找到 %d 目录\n", len(result.Dirs))
  22. // fmt.Printf("找到 %d 文件\n", len(result.Files))
  23. // time.Sleep(20 * time.Second)
  24. // // 流式处理
  25. // //paths, errs := StreamPathInfo("/bigdata")
  26. // //for {
  27. // // select {
  28. // // case path, ok := <-paths:
  29. // // if !ok {
  30. // // return
  31. // // }
  32. // // fmt.Println(path)
  33. // // case err := <-errs:
  34. // // fmt.Println("错误:", err)
  35. // // return
  36. // // }
  37. // //}
  38. //}
  39. //
  40. //type PathInfo struct {
  41. // Dirs []string // 目录路径(包含所有层级)
  42. // Files []string // 文件路径
  43. //}
  44. //
  45. //var pathPool = sync.Pool{
  46. // New: func() interface{} {
  47. // return &PathInfo{
  48. // Dirs: make([]string, 0, 1024), // 预分配容量
  49. // Files: make([]string, 0, 4096),
  50. // }
  51. // },
  52. //}
  53. //
  54. //func GetPathInfo(root string) (*PathInfo, error) {
  55. // // 复用对象减少内存分配[9](@ref)
  56. // info := pathPool.Get().(*PathInfo)
  57. //
  58. // // 路径有效性校验[5,6](@ref)
  59. // root = filepath.Clean(root)
  60. // stat, err := os.Stat(root)
  61. // if err != nil {
  62. // if errors.Is(err, fs.ErrNotExist) {
  63. // return nil, &os.PathError{Op: "stat", Path: root, Err: err} // 增强错误信息[8](@ref)
  64. // }
  65. // return nil, err
  66. // }
  67. //
  68. // // 文件直接返回[4](@ref)
  69. // if !stat.IsDir() {
  70. // info.Files = append(info.Files, root)
  71. // return info, nil
  72. // }
  73. //
  74. // // 使用WalkDir优化遍历性能[4,9](@ref)
  75. // err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
  76. // if err != nil {
  77. // // 处理遍历错误但继续执行[7](@ref)
  78. // if errors.Is(err, fs.ErrPermission) {
  79. // return nil // 跳过权限错误
  80. // }
  81. // return err
  82. // }
  83. //
  84. // // 排除根目录自身
  85. // if path == root {
  86. // return nil
  87. // }
  88. //
  89. // // 动态扩容检测[9](@ref)
  90. // if len(info.Dirs)+len(info.Files) > 1e6 {
  91. // return errors.New("超出内存安全阈值(1,000,000条路径)")
  92. // }
  93. // fmt.Println(path)
  94. // if d.IsDir() {
  95. // info.Dirs = append(info.Dirs, path)
  96. // } else {
  97. // info.Files = append(info.Files, path)
  98. // }
  99. // return nil
  100. // })
  101. //
  102. // if err != nil {
  103. // return nil, &os.PathError{Op: "walk", Path: root, Err: err} // 错误包装[6](@ref)
  104. // }
  105. //
  106. // return info, nil
  107. //}
  108. //
  109. //// 流式处理版本(适用于超大数据集)
  110. //func StreamPathInfo(root string) (<-chan string, <-chan error) {
  111. // paths := make(chan string, 1000)
  112. // errs := make(chan error, 1)
  113. //
  114. // go func() {
  115. // defer close(paths)
  116. // defer close(errs)
  117. //
  118. // err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
  119. // if err != nil {
  120. // return err
  121. // }
  122. // if path != root {
  123. // select {
  124. // case paths <- path:
  125. // default:
  126. // return errors.New("消费者处理过慢")
  127. // }
  128. // }
  129. // return nil
  130. // })
  131. //
  132. // if err != nil {
  133. // errs <- &os.PathError{Op: "stream", Path: root, Err: err}
  134. // }
  135. // }()
  136. //
  137. // return paths, errs
  138. //}