From c58e392d547e30732fc27b834f5adffaee0e74fb Mon Sep 17 00:00:00 2001 From: Ian Kaneshiro Date: Mon, 1 Apr 2024 09:27:57 -0700 Subject: [PATCH] Beskar Mirror: Fix link handling during web index generation --- .../mirror/pkg/mirrorrepository/api.go | 19 +++++++++++++++++++ .../mirror/pkg/mirrorrepository/index_html.go | 14 ++++++++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/internal/plugins/mirror/pkg/mirrorrepository/api.go b/internal/plugins/mirror/pkg/mirrorrepository/api.go index a04ce73..47ec92f 100644 --- a/internal/plugins/mirror/pkg/mirrorrepository/api.go +++ b/internal/plugins/mirror/pkg/mirrorrepository/api.go @@ -470,6 +470,25 @@ func (h *Handler) GetRepositoryFile(ctx context.Context, name string) (repositor return toRepositoryFileAPI(fileDB), nil } +func (h *Handler) GetRepositoryFileRaw(ctx context.Context, name string) (repositoryFile *mirrordb.RepositoryFile, err error) { + if !h.Started() { + return nil, werror.Wrap(gcode.ErrUnavailable, err) + } + + db, err := h.getRepositoryDB(ctx) + if err != nil { + return nil, werror.Wrap(gcode.ErrInternal, err) + } + defer db.Close(false) + + fileDB, err := db.GetFileByName(ctx, name) + if err != nil { + return nil, werror.Wrap(gcode.ErrInternal, err) + } + + return fileDB, nil +} + func (h *Handler) GetRepositoryFileByReferenceRaw(ctx context.Context, reference string) (repositoryFile *mirrordb.RepositoryFile, err error) { if !h.Started() { return nil, werror.Wrap(gcode.ErrUnavailable, err) diff --git a/internal/plugins/mirror/pkg/mirrorrepository/index_html.go b/internal/plugins/mirror/pkg/mirrorrepository/index_html.go index 8389edc..14dfc28 100644 --- a/internal/plugins/mirror/pkg/mirrorrepository/index_html.go +++ b/internal/plugins/mirror/pkg/mirrorrepository/index_html.go @@ -88,8 +88,14 @@ func (h *Handler) GenerateIndexes() error { return err } - h.logger.Debug("Processing symlink", "file", fileInfo.Name, "link", file.Link) - targetInfo, err := h.GetRepositoryFileByReferenceRaw(context.Background(), file.Link) + // Ensure link directory is preserved and only the link is replaced. + targetName := file.Link + if strings.Contains(file.Name, "/") { + targetName = path.Join(path.Dir(file.Name), file.Link) + } + + h.logger.Debug("Processing symlink", "file", fileInfo.Name, "link", file.Link, "target", targetName) + targetInfo, err := h.GetRepositoryFileRaw(context.Background(), targetName) if err != nil { h.logger.Error("Failed to get target info", "error", err.Error(), "link", file.Link) return err @@ -98,13 +104,13 @@ func (h *Handler) GenerateIndexes() error { if rsync.FileMode(targetInfo.Mode).IsDIR() { c.Directories = append(c.Directories, index.Directory{ Name: filepath.Base(file.Name), - Ref: path.Join(webPrefix, strings.TrimPrefix(file.Link, h.Repository)), + Ref: path.Join(webPrefix, targetName), MTime: time.Unix(file.ModifiedTime, 0), }) } else { c.Files = append(c.Files, index.File{ Name: filepath.Base(fileInfo.Name), - Ref: path.Join(webPrefix, filepath.Clean(targetInfo.Name)), + Ref: path.Join(webPrefix, targetName), MTime: time.Unix(targetInfo.ModifiedTime, 0), Size: targetInfo.Size, })