Skip to content

Commit

Permalink
Merge pull request #51 from ctrliq/mirror-fix-web-links
Browse files Browse the repository at this point in the history
Beskar Mirror: Fix link handling during web index generation
  • Loading branch information
kyleishie authored Apr 1, 2024
2 parents 9618061 + c58e392 commit 22386d0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
19 changes: 19 additions & 0 deletions internal/plugins/mirror/pkg/mirrorrepository/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions internal/plugins/mirror/pkg/mirrorrepository/index_html.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
})
Expand Down

0 comments on commit 22386d0

Please sign in to comment.