Skip to content

Commit 7814ae6

Browse files
author
Sofia Kopikova
committed
[PBCKP-159] fix false directory init, add test to init.py
Tags: pg_probackup
1 parent 0b5b37e commit 7814ae6

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/init.c

+10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@ do_init(CatalogState *catalogState)
3131
elog(ERROR, "cannot open backup catalog directory \"%s\": %s",
3232
catalogState->catalog_path, strerror(errno_tmp));
3333
}
34+
else if (results == 1)
35+
{
36+
/* Check whether we have all(rw) rights for directory */
37+
if (access(catalogState->catalog_path, 6) == -1) /* == R_OK | W_OK */
38+
{
39+
int errno_tmp = errno;
40+
elog(ERROR, "cannot access backup catalog directory \"%s\": %s",
41+
catalogState->catalog_path, strerror(errno_tmp));
42+
}
43+
}
3444

3545
/* create backup catalog root directory */
3646
dir_create_dir(catalogState->catalog_path, DIR_PERMISSION, false);

tests/init.py

+38
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import os
2+
import stat # for chmod
23
import unittest
34
from .helpers.ptrack_helpers import dir_files, ProbackupTest, ProbackupException
45
import shutil
56

7+
if os.name == 'nt':
8+
import win32security
9+
import ntsecuritycon as con
610

711
module_name = 'init'
812

@@ -86,6 +90,40 @@ def test_already_exist(self):
8690

8791
# Clean after yourself
8892
self.del_test_dir(module_name, fname)
93+
94+
# @unittest.skip("skip")
95+
def test_no_rights_for_directory(self):
96+
"""Failure with backup catalog existed and empty but user has no writing permissions"""
97+
fname = self.id().split(".")[3]
98+
backup_dir = os.path.join(self.tmp_path, module_name, fname, 'backup')
99+
node = self.make_simple_node(base_dir=os.path.join(module_name, fname, 'node'))
100+
os.mkdir(backup_dir)
101+
# changing directory rights
102+
if os.name == 'nt': # windows block
103+
user, domain, type = win32security.LookupAccountName("", os.getlogin())
104+
sd = win32securityGetFileSecurity(backup_dir, win32security.DACL_SECURIRY_INFORMATION)
105+
dacl = sd.GetSecurityDescriptorDacl()
106+
107+
dacl.AddAccessDeniedAce(win32security.ACL_REVISION, con.FILE_WRITE_DATA, user) # deny writing permission
108+
sd.SetSecurityDescriptorDacl(1, dacl, 0)
109+
win32security.SetFileSecurity(backup_dir, win32security.DACL_SECURIRY_INFORMATION, sd)
110+
else:
111+
os.chmod(backup_dir, stat.S_IREAD) # set read-only flag for current user
112+
assert os.access(backup_dir, os.R_OK) and not os.access(backup_dir, os.W_OK)
113+
114+
self.init_pb(backup_dir)
115+
try:
116+
self.show_pb(backup_dir, 'node')
117+
self.assertEqual(1, 0, 'Expecting Error due to initialization in empty directory with no rithts for writing. Output: {0} \n CMD: {1}'.format(
118+
repr(self.output), self.cmd))
119+
except ProbackupException as e:
120+
self.assertIn(
121+
"ERROR: Instance 'node' does not exist in this backup catalog",
122+
e.message,
123+
'\n Unexpected Error Message: {0}\n CMD: {1}'.format(repr(e.message), self.cmd))
124+
125+
# Clean after yourself
126+
self.del_test_dir(module_name, fname)
89127

90128
# @unittest.skip("skip")
91129
def test_abs_path(self):

0 commit comments

Comments
 (0)