updateOwnerIndexFile handles the logic of reading, modifying, and writing the MessagePack index file.
(basePath string, obsoleteIDs []string)
| 468 | |
| 469 | // updateOwnerIndexFile handles the logic of reading, modifying, and writing the MessagePack index file. |
| 470 | func updateOwnerIndexFile(basePath string, obsoleteIDs []string) error { |
| 471 | fmt.Printf(" Rewriting index file '%s'\n", basePath) |
| 472 | |
| 473 | ownerID, err := xattr.Get(basePath, ownerIDAttrName) |
| 474 | if err != nil { |
| 475 | return fmt.Errorf("could not get owner ID from oldest entry '%s' to find index: %w", basePath, err) |
| 476 | } |
| 477 | |
| 478 | indexPath := filepath.Join(basePath, "../../indexes/by-user-id", string(ownerID)+".mpk") |
| 479 | indexPath = filepath.Clean(indexPath) |
| 480 | |
| 481 | // Read the MessagePack file |
| 482 | fileData, err := os.ReadFile(indexPath) |
| 483 | if err != nil { |
| 484 | if os.IsNotExist(err) { |
| 485 | return fmt.Errorf("index file does not exist, skipping update") |
| 486 | } |
| 487 | return fmt.Errorf("could not read index file: %w", err) |
| 488 | } |
| 489 | var indexMap map[string]string |
| 490 | if err := msgpack.Unmarshal(fileData, &indexMap); err != nil { |
| 491 | return fmt.Errorf("failed to parse MessagePack index file (is it corrupt?): %w", err) |
| 492 | } |
| 493 | |
| 494 | // Remove obsolete IDs from the map |
| 495 | itemsRemoved := 0 |
| 496 | for _, id := range obsoleteIDs { |
| 497 | if _, exists := indexMap[id]; exists { |
| 498 | fmt.Printf(" - Removing obsolete ID '%s' from index.\n", id) |
| 499 | delete(indexMap, id) |
| 500 | itemsRemoved++ |
| 501 | } else { |
| 502 | fmt.Printf(" - Obsolete ID '%s' not found in index\n", id) |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | if itemsRemoved == 0 { |
| 507 | return nil |
| 508 | } |
| 509 | |
| 510 | // Write the data back to the file |
| 511 | updatedData, err := msgpack.Marshal(&indexMap) |
| 512 | if err != nil { |
| 513 | return fmt.Errorf("failed to marshal updated index map: %w", err) |
| 514 | } |
| 515 | if err := os.WriteFile(indexPath, updatedData, 0644); err != nil { |
| 516 | return fmt.Errorf("failed to write updated index file: %w", err) |
| 517 | } |
| 518 | |
| 519 | fmt.Printf(" ✓ Successfully removed %d item(s) and saved index file.\n", itemsRemoved) |
| 520 | return nil |
| 521 | } |
| 522 | |
| 523 | func removeAttributes(path string) error { |
| 524 | attrNames, err := xattr.List(path) |
no test coverage detected