| 114 | } |
| 115 | |
| 116 | func selected[CacheT cache.CacheEntity](repo *cache.RepoCache, resolver Resolver[CacheT], namespace string) (*CacheT, error) { |
| 117 | filename := selectFileName(namespace) |
| 118 | f, err := repo.LocalStorage().Open(filename) |
| 119 | if err != nil { |
| 120 | if os.IsNotExist(err) { |
| 121 | return nil, nil |
| 122 | } else { |
| 123 | return nil, err |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | buf, err := io.ReadAll(io.LimitReader(f, 100)) |
| 128 | if err != nil { |
| 129 | _ = f.Close() |
| 130 | return nil, err |
| 131 | } |
| 132 | |
| 133 | err = f.Close() |
| 134 | if err != nil { |
| 135 | return nil, err |
| 136 | } |
| 137 | |
| 138 | if len(buf) >= 100 { |
| 139 | return nil, fmt.Errorf("the select file should be < 100 bytes") |
| 140 | } |
| 141 | |
| 142 | id := entity.Id(buf) |
| 143 | if err := id.Validate(); err != nil { |
| 144 | err = repo.LocalStorage().Remove(filename) |
| 145 | if err != nil { |
| 146 | return nil, errors.Wrap(err, "error while removing invalid select file") |
| 147 | } |
| 148 | |
| 149 | return nil, fmt.Errorf("select file in invalid, removing it") |
| 150 | } |
| 151 | |
| 152 | cached, err := resolver.Resolve(id) |
| 153 | if err != nil { |
| 154 | return nil, err |
| 155 | } |
| 156 | |
| 157 | return &cached, nil |
| 158 | } |