Hello all,
I've been working on an extension that tightly integrates
postgres with underlying filesystem . I need to customize
how postgres copies directories for new databases.
I first looked at the ProcessUtility_hook. This would require
me to copy or rewrite most of the createdb() function. This is
less than ideal of course. Someone on the IRC channel
suggested I could add a hook for copydir().
I implemented the hook similar to how the
ProcessUtility_hook is implemented. I couldn't find any tests
for any of the existing hooks. I've been looking at the regression
tests, but I am not entirely sure how to proceed on that front.
I tested my patch extensively against master and
the REL_12_STABLE branch. All tests pass and the patch has
been working great with my extension.
I attached a first draft of the patch against master.
---
Swen Kooij
[1mdiff --git a/src/backend/storage/file/copydir.c b/src/backend/storage/file/copydir.c[m
[1mindex ca1c9cd765..e44bebf947 100644[m
[1m--- a/src/backend/storage/file/copydir.c[m
[1m+++ b/src/backend/storage/file/copydir.c[m
[36m@@ -27,6 +27,9 @@[m
#include "miscadmin.h"[m
#include "pgstat.h"[m
[m
[32m+[m[32m/* Hook for plugins to get control in copydir() */[m
[32m+[m[32mcopydir_hook_type copydir_hook = NULL;[m
[32m+[m
/*[m
* copydir: copy a directory[m
*[m
[36m@@ -36,6 +39,22 @@[m
void[m
copydir(char *fromdir, char *todir, bool recurse)[m
{[m
[32m+[m [32mif (copydir_hook)[m
[32m+[m [32m(*copydir_hook) (fromdir, todir, recurse);[m
[32m+[m [32melse[m
[32m+[m [32mstandard_copydir(fromdir, todir, recurse);[m
[32m+[m[32m}[m
[32m+[m
[32m+[m[32m/*[m
[32m+[m[32m * copydir: copy a directory[m
[32m+[m[32m *[m
[32m+[m[32m * If recurse is false, subdirectories are ignored. Anything that's not[m
[32m+[m[32m * a directory or a regular file is ignored.[m
[32m+[m[32m */[m
[32m+[m[32mvoid[m
[32m+[m[32mstandard_copydir(char *fromdir, char *todir, bool recurse)[m
[32m+[m[32m{[m
[32m+[m
DIR *xldir;[m
struct dirent *xlde;[m
char fromfile[MAXPGPATH * 2];[m
[1mdiff --git a/src/include/storage/copydir.h b/src/include/storage/copydir.h[m
[1mindex 525cc6203e..8b73184177 100644[m
[1m--- a/src/include/storage/copydir.h[m
[1m+++ b/src/include/storage/copydir.h[m
[36m@@ -13,7 +13,12 @@[m
#ifndef COPYDIR_H[m
#define COPYDIR_H[m
[m
[32m+[m[32m/* Hook for plugins to get control in copydir() */[m
[32m+[m[32mtypedef void (*copydir_hook_type) (char *fromdir, char *todir, bool recurse);[m
[32m+[m[32mextern PGDLLIMPORT copydir_hook_type copydir_hook;[m
[32m+[m
extern void copydir(char *fromdir, char *todir, bool recurse);[m
[32m+[m[32mextern void standard_copydir(char *fromdir, char *todir, bool recurse);[m
extern void copy_file(char *fromfile, char *tofile);[m
[m
#endif /* COPYDIR_H */[m