| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- package main
- //
- //func TestCommand(t *testing.T) {
- // bytes := make([]byte, 0)
- // bytes = append(bytes, 0x2a)
- // command := "你好1"
- // cmd := []byte(command)
- // l := util.IntToBytes(int32(len(cmd)))
- // bytes = append(bytes, l...)
- // bytes = append(bytes, cmd...)
- // bytes = append(bytes, util.Int16ToBytes(util.Crc16CCITT(cmd))...)
- // fmt.Println(util.ToHexBytes(bytes))
- //}
- //func TestGetFiles(t *testing.T) {
- // // 标准用法
- // result, err := GetPathInfo("D:\\project\\my\\file-manger\\frontend\\node_modules")
- // if err != nil {
- // fmt.Printf("错误: %+v\n", err)
- // return
- // }
- // fmt.Printf("找到 %d 目录\n", len(result.Dirs))
- // fmt.Printf("找到 %d 文件\n", len(result.Files))
- // time.Sleep(20 * time.Second)
- // // 流式处理
- // //paths, errs := StreamPathInfo("/bigdata")
- // //for {
- // // select {
- // // case path, ok := <-paths:
- // // if !ok {
- // // return
- // // }
- // // fmt.Println(path)
- // // case err := <-errs:
- // // fmt.Println("错误:", err)
- // // return
- // // }
- // //}
- //}
- //
- //type PathInfo struct {
- // Dirs []string // 目录路径(包含所有层级)
- // Files []string // 文件路径
- //}
- //
- //var pathPool = sync.Pool{
- // New: func() interface{} {
- // return &PathInfo{
- // Dirs: make([]string, 0, 1024), // 预分配容量
- // Files: make([]string, 0, 4096),
- // }
- // },
- //}
- //
- //func GetPathInfo(root string) (*PathInfo, error) {
- // // 复用对象减少内存分配[9](@ref)
- // info := pathPool.Get().(*PathInfo)
- //
- // // 路径有效性校验[5,6](@ref)
- // root = filepath.Clean(root)
- // stat, err := os.Stat(root)
- // if err != nil {
- // if errors.Is(err, fs.ErrNotExist) {
- // return nil, &os.PathError{Op: "stat", Path: root, Err: err} // 增强错误信息[8](@ref)
- // }
- // return nil, err
- // }
- //
- // // 文件直接返回[4](@ref)
- // if !stat.IsDir() {
- // info.Files = append(info.Files, root)
- // return info, nil
- // }
- //
- // // 使用WalkDir优化遍历性能[4,9](@ref)
- // err = filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
- // if err != nil {
- // // 处理遍历错误但继续执行[7](@ref)
- // if errors.Is(err, fs.ErrPermission) {
- // return nil // 跳过权限错误
- // }
- // return err
- // }
- //
- // // 排除根目录自身
- // if path == root {
- // return nil
- // }
- //
- // // 动态扩容检测[9](@ref)
- // if len(info.Dirs)+len(info.Files) > 1e6 {
- // return errors.New("超出内存安全阈值(1,000,000条路径)")
- // }
- // fmt.Println(path)
- // if d.IsDir() {
- // info.Dirs = append(info.Dirs, path)
- // } else {
- // info.Files = append(info.Files, path)
- // }
- // return nil
- // })
- //
- // if err != nil {
- // return nil, &os.PathError{Op: "walk", Path: root, Err: err} // 错误包装[6](@ref)
- // }
- //
- // return info, nil
- //}
- //
- //// 流式处理版本(适用于超大数据集)
- //func StreamPathInfo(root string) (<-chan string, <-chan error) {
- // paths := make(chan string, 1000)
- // errs := make(chan error, 1)
- //
- // go func() {
- // defer close(paths)
- // defer close(errs)
- //
- // err := filepath.WalkDir(root, func(path string, d fs.DirEntry, err error) error {
- // if err != nil {
- // return err
- // }
- // if path != root {
- // select {
- // case paths <- path:
- // default:
- // return errors.New("消费者处理过慢")
- // }
- // }
- // return nil
- // })
- //
- // if err != nil {
- // errs <- &os.PathError{Op: "stream", Path: root, Err: err}
- // }
- // }()
- //
- // return paths, errs
- //}
|