77"""
88
99import os
10- import platform
1110import stat
1211import subprocess
1312from test_framework .test_framework import BitcoinTestFramework
@@ -34,11 +33,13 @@ def reindex_readonly(self):
3433 filename = self .nodes [0 ].chain_path / "blocks" / "blk00000.dat"
3534 filename .chmod (stat .S_IREAD )
3635
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 )
3940 try :
4041 subprocess .run (['chattr' , '+i' , filename ], capture_output = True , check = True )
41- used_chattr = True
42+ undo_immutable = lambda : subprocess . check_call ([ 'chattr' , '-i' , filename ])
4243 self .log .info ("Made file immutable with chattr" )
4344 except subprocess .CalledProcessError as e :
4445 self .log .warning (str (e ))
@@ -50,15 +51,33 @@ def reindex_readonly(self):
5051 self .log .warning ("Return early on Linux under root, because chattr failed." )
5152 self .log .warning ("This should only happen due to missing capabilities in a container." )
5253 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
5974
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 ()
6281
6382 filename .chmod (0o777 )
6483
0 commit comments