7
7
"""
8
8
9
9
import os
10
- import platform
11
10
import stat
12
11
import subprocess
13
12
from test_framework .test_framework import BitcoinTestFramework
@@ -34,11 +33,13 @@ def reindex_readonly(self):
34
33
filename = self .nodes [0 ].chain_path / "blocks" / "blk00000.dat"
35
34
filename .chmod (stat .S_IREAD )
36
35
37
- used_chattr = False
38
- if platform .system () == "Linux" :
36
+ undo_immutable = lambda : None
37
+ # Linux
38
+ try :
39
+ subprocess .run (['chattr' ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
39
40
try :
40
41
subprocess .run (['chattr' , '+i' , filename ], capture_output = True , check = True )
41
- used_chattr = True
42
+ undo_immutable = lambda : subprocess . check_call ([ 'chattr' , '-i' , filename ])
42
43
self .log .info ("Made file immutable with chattr" )
43
44
except subprocess .CalledProcessError as e :
44
45
self .log .warning (str (e ))
@@ -50,15 +51,33 @@ def reindex_readonly(self):
50
51
self .log .warning ("Return early on Linux under root, because chattr failed." )
51
52
self .log .warning ("This should only happen due to missing capabilities in a container." )
52
53
self .log .warning ("Make sure to --cap-add LINUX_IMMUTABLE if you want to run this test." )
53
- return
54
-
55
- self .log .debug ("Attempt to restart and reindex the node with the unwritable block file" )
56
- with self .nodes [0 ].assert_debug_log (expected_msgs = ['FlushStateToDisk' , 'failed to open file' ], unexpected_msgs = []):
57
- self .nodes [0 ].assert_start_raises_init_error (extra_args = ['-reindex' , '-fastprune' ],
58
- expected_msg = "Error: A fatal internal error occurred, see debug.log for details" )
54
+ undo_immutable = False
55
+ except Exception :
56
+ # macOS, and *BSD
57
+ try :
58
+ subprocess .run (['chflags' ], stdout = subprocess .DEVNULL , stderr = subprocess .DEVNULL )
59
+ try :
60
+ subprocess .run (['chflags' , 'uchg' , filename ], capture_output = True , check = True )
61
+ undo_immutable = lambda : subprocess .check_call (['chflags' , 'nouchg' , filename ])
62
+ self .log .info ("Made file immutable with chflags" )
63
+ except subprocess .CalledProcessError as e :
64
+ self .log .warning (str (e ))
65
+ if e .stdout :
66
+ self .log .warning (f"stdout: { e .stdout } " )
67
+ if e .stderr :
68
+ self .log .warning (f"stderr: { e .stderr } " )
69
+ if os .getuid () == 0 :
70
+ self .log .warning ("Return early on BSD under root, because chflags failed." )
71
+ undo_immutable = False
72
+ except Exception :
73
+ pass
59
74
60
- if used_chattr :
61
- subprocess .check_call (['chattr' , '-i' , filename ])
75
+ if undo_immutable :
76
+ self .log .info ("Attempt to restart and reindex the node with the unwritable block file" )
77
+ with self .nodes [0 ].assert_debug_log (expected_msgs = ['FlushStateToDisk' , 'failed to open file' ], unexpected_msgs = []):
78
+ self .nodes [0 ].assert_start_raises_init_error (extra_args = ['-reindex' , '-fastprune' ],
79
+ expected_msg = "Error: A fatal internal error occurred, see debug.log for details" )
80
+ undo_immutable ()
62
81
63
82
filename .chmod (0o777 )
64
83
0 commit comments