Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Beskar Mirror: Fix link handling during web index generation #51

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading