Skip to content

Commit

Permalink
pghoard: add xlog filename check to CLI commands
Browse files Browse the repository at this point in the history
  • Loading branch information
egor-voynov-aiven committed Apr 12, 2024
1 parent 7c466c3 commit 2f6cfc2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
20 changes: 12 additions & 8 deletions golang/pghoard_postgres_command_go.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"net/http"
"os"
"path"
"regexp"
"time"
)

Expand Down Expand Up @@ -120,15 +121,18 @@ func restore_command(url string, output string, xlog string) (int, error) {
}
output_path = path.Join(cwd, output)
}
// if file "<xlog>.pghoard.prefetch" exists, just move it to destination
xlogPrefetchPath := path.Join(path.Dir(output_path), xlog+".pghoard.prefetch")
_, err = os.Stat(xlogPrefetchPath)
if err == nil {
err := os.Rename(xlogPrefetchPath, output_path)
if err != nil {
return EXIT_ABORT, err
xlogNameRe := regexp.MustCompile(`^([A-F0-9]{24}|[A-F0-9]{8}\.history)$`)
if xlogNameRe.MatchString(xlog) {
// if file "<xlog>.pghoard.prefetch" exists, just move it to destination
xlogPrefetchPath := path.Join(path.Dir(output_path), xlog+".pghoard.prefetch")
_, err = os.Stat(xlogPrefetchPath)
if err == nil {
err := os.Rename(xlogPrefetchPath, output_path)
if err != nil {
return EXIT_ABORT, err
}
return EXIT_OK, nil
}
return EXIT_OK, nil
}
req, err = http.NewRequest("GET", url, nil)
req.Header.Set("x-pghoard-target-path", output_path)
Expand Down
12 changes: 7 additions & 5 deletions pghoard/postgres_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import time
from http.client import BadStatusLine, HTTPConnection, IncompleteRead

from pghoard.wal import TIMELINE_RE, WAL_RE
from . import version

PGHOARD_HOST = "127.0.0.1"
Expand Down Expand Up @@ -72,11 +73,12 @@ def restore_command(site, xlog, output, host=PGHOARD_HOST, port=PGHOARD_PORT, re
# directory. Note that os.path.join strips preceding components if a new components starts with a
# slash so it's still possible to use this with absolute paths.
output_path = os.path.join(os.getcwd(), output)
# if file "<xlog>.pghoard.prefetch" exists, just move it to destination
prefetch_path = os.path.join(os.path.dirname(output_path), xlog + ".pghoard.prefetch")
if os.path.exists(prefetch_path):
os.rename(prefetch_path, output_path)
return
if WAL_RE.match(xlog) or TIMELINE_RE.match(xlog):
# if file "<xlog>.pghoard.prefetch" exists, just move it to destination
prefetch_path = os.path.join(os.path.dirname(output_path), xlog + ".pghoard.prefetch")
if os.path.exists(prefetch_path):
os.rename(prefetch_path, output_path)
return
headers = {"x-pghoard-target-path": output_path}
method = "GET"
path = "/{}/archive/{}".format(site, xlog)
Expand Down

0 comments on commit 2f6cfc2

Please sign in to comment.