3131import java .util .zip .ZipEntry ;
3232import java .util .zip .ZipInputStream ;
3333
34+ import static com .termux .shared .termux .TermuxConstants .TERMUX_PREFIX_DIR ;
35+ import static com .termux .shared .termux .TermuxConstants .TERMUX_PREFIX_DIR_PATH ;
36+ import static com .termux .shared .termux .TermuxConstants .TERMUX_STAGING_PREFIX_DIR ;
37+ import static com .termux .shared .termux .TermuxConstants .TERMUX_STAGING_PREFIX_DIR_PATH ;
38+
3439/**
3540 * Install the Termux bootstrap packages if necessary by following the below steps:
3641 * <p/>
@@ -67,7 +72,7 @@ static void setupBootstrapIfNeeded(final Activity activity, final Runnable whenD
6772 // Termux can only be run as the primary user (device owner) since only that
6873 // account has the expected file system paths. Verify that:
6974 if (!PackageUtils .isCurrentUserThePrimaryUser (activity )) {
70- bootstrapErrorMessage = activity .getString (R .string .bootstrap_error_not_primary_user_message , MarkdownUtils .getMarkdownCodeForString (TermuxConstants . TERMUX_PREFIX_DIR_PATH , false ));
75+ bootstrapErrorMessage = activity .getString (R .string .bootstrap_error_not_primary_user_message , MarkdownUtils .getMarkdownCodeForString (TERMUX_PREFIX_DIR_PATH , false ));
7176 Logger .logError (LOG_TAG , "isFilesDirectoryAccessible: " + isFilesDirectoryAccessible );
7277 Logger .logError (LOG_TAG , bootstrapErrorMessage );
7378 sendBootstrapCrashReportNotification (activity , bootstrapErrorMessage );
@@ -87,21 +92,18 @@ static void setupBootstrapIfNeeded(final Activity activity, final Runnable whenD
8792 return ;
8893 }
8994
90- final String PREFIX_FILE_PATH = TermuxConstants .TERMUX_PREFIX_DIR_PATH ;
91- final File PREFIX_FILE = TermuxConstants .TERMUX_PREFIX_DIR ;
92-
9395 // If prefix directory exists, even if its a symlink to a valid directory and symlink is not broken/dangling
94- if (FileUtils .directoryFileExists (PREFIX_FILE_PATH , true )) {
95- File [] PREFIX_FILE_LIST = PREFIX_FILE .listFiles ();
96+ if (FileUtils .directoryFileExists (TERMUX_PREFIX_DIR_PATH , true )) {
97+ File [] PREFIX_FILE_LIST = TERMUX_PREFIX_DIR .listFiles ();
9698 // If prefix directory is empty or only contains the tmp directory
9799 if (PREFIX_FILE_LIST == null || PREFIX_FILE_LIST .length == 0 || (PREFIX_FILE_LIST .length == 1 && TermuxConstants .TERMUX_TMP_PREFIX_DIR_PATH .equals (PREFIX_FILE_LIST [0 ].getAbsolutePath ()))) {
98- Logger .logInfo (LOG_TAG , "The prefix directory \" " + PREFIX_FILE_PATH + "\" exists but is empty or only contains the tmp directory." );
100+ Logger .logInfo (LOG_TAG , "The termux prefix directory \" " + TERMUX_PREFIX_DIR_PATH + "\" exists but is empty or only contains the tmp directory." );
99101 } else {
100102 whenDone .run ();
101103 return ;
102104 }
103- } else if (FileUtils .fileExists (PREFIX_FILE_PATH , false )) {
104- Logger .logInfo (LOG_TAG , "The prefix directory \" " + PREFIX_FILE_PATH + "\" does not exist but another file exists at its destination." );
105+ } else if (FileUtils .fileExists (TERMUX_PREFIX_DIR_PATH , false )) {
106+ Logger .logInfo (LOG_TAG , "The termux prefix directory \" " + TERMUX_PREFIX_DIR_PATH + "\" does not exist but another file exists at its destination." );
105107 }
106108
107109 final ProgressDialog progress = ProgressDialog .show (activity , null , activity .getString (R .string .bootstrap_installer_body ), true , false );
@@ -113,24 +115,35 @@ public void run() {
113115
114116 Error error ;
115117
116- final String STAGING_PREFIX_PATH = TermuxConstants .TERMUX_STAGING_PREFIX_DIR_PATH ;
117- final File STAGING_PREFIX_FILE = new File (STAGING_PREFIX_PATH );
118-
119118 // Delete prefix staging directory or any file at its destination
120- error = FileUtils .deleteFile ("prefix staging directory" , STAGING_PREFIX_PATH , true );
119+ error = FileUtils .deleteFile ("termux prefix staging directory" , TERMUX_STAGING_PREFIX_DIR_PATH , true );
121120 if (error != null ) {
122- showBootstrapErrorDialog (activity , PREFIX_FILE_PATH , whenDone , Error .getErrorMarkdownString (error ));
121+ showBootstrapErrorDialog (activity , whenDone , Error .getErrorMarkdownString (error ));
123122 return ;
124123 }
125124
126125 // Delete prefix directory or any file at its destination
127- error = FileUtils .deleteFile ("prefix directory" , PREFIX_FILE_PATH , true );
126+ error = FileUtils .deleteFile ("termux prefix directory" , TERMUX_PREFIX_DIR_PATH , true );
127+ if (error != null ) {
128+ showBootstrapErrorDialog (activity , whenDone , Error .getErrorMarkdownString (error ));
129+ return ;
130+ }
131+
132+ // Create prefix staging directory if it does not already exist and set required permissions
133+ error = TermuxFileUtils .isTermuxPrefixStagingDirectoryAccessible (true , true );
134+ if (error != null ) {
135+ showBootstrapErrorDialog (activity , whenDone , Error .getErrorMarkdownString (error ));
136+ return ;
137+ }
138+
139+ // Create prefix directory if it does not already exist and set required permissions
140+ error = TermuxFileUtils .isTermuxPrefixDirectoryAccessible (true , true );
128141 if (error != null ) {
129- showBootstrapErrorDialog (activity , PREFIX_FILE_PATH , whenDone , Error .getErrorMarkdownString (error ));
142+ showBootstrapErrorDialog (activity , whenDone , Error .getErrorMarkdownString (error ));
130143 return ;
131144 }
132145
133- Logger .logInfo (LOG_TAG , "Extracting bootstrap zip to prefix staging directory \" " + STAGING_PREFIX_PATH + "\" ." );
146+ Logger .logInfo (LOG_TAG , "Extracting bootstrap zip to prefix staging directory \" " + TERMUX_STAGING_PREFIX_DIR_PATH + "\" ." );
134147
135148 final byte [] buffer = new byte [8096 ];
136149 final List <Pair <String , String >> symlinks = new ArrayList <>(50 );
@@ -147,23 +160,23 @@ public void run() {
147160 if (parts .length != 2 )
148161 throw new RuntimeException ("Malformed symlink line: " + line );
149162 String oldPath = parts [0 ];
150- String newPath = STAGING_PREFIX_PATH + "/" + parts [1 ];
163+ String newPath = TERMUX_STAGING_PREFIX_DIR_PATH + "/" + parts [1 ];
151164 symlinks .add (Pair .create (oldPath , newPath ));
152165
153166 error = ensureDirectoryExists (new File (newPath ).getParentFile ());
154167 if (error != null ) {
155- showBootstrapErrorDialog (activity , PREFIX_FILE_PATH , whenDone , Error .getErrorMarkdownString (error ));
168+ showBootstrapErrorDialog (activity , whenDone , Error .getErrorMarkdownString (error ));
156169 return ;
157170 }
158171 }
159172 } else {
160173 String zipEntryName = zipEntry .getName ();
161- File targetFile = new File (STAGING_PREFIX_PATH , zipEntryName );
174+ File targetFile = new File (TERMUX_STAGING_PREFIX_DIR_PATH , zipEntryName );
162175 boolean isDirectory = zipEntry .isDirectory ();
163176
164177 error = ensureDirectoryExists (isDirectory ? targetFile : targetFile .getParentFile ());
165178 if (error != null ) {
166- showBootstrapErrorDialog (activity , PREFIX_FILE_PATH , whenDone , Error .getErrorMarkdownString (error ));
179+ showBootstrapErrorDialog (activity , whenDone , Error .getErrorMarkdownString (error ));
167180 return ;
168181 }
169182
@@ -189,17 +202,17 @@ public void run() {
189202 Os .symlink (symlink .first , symlink .second );
190203 }
191204
192- Logger .logInfo (LOG_TAG , "Moving prefix staging to prefix directory." );
205+ Logger .logInfo (LOG_TAG , "Moving termux prefix staging to prefix directory." );
193206
194- if (!STAGING_PREFIX_FILE .renameTo (PREFIX_FILE )) {
195- throw new RuntimeException ("Moving prefix staging to prefix directory failed" );
207+ if (!TERMUX_STAGING_PREFIX_DIR .renameTo (TERMUX_PREFIX_DIR )) {
208+ throw new RuntimeException ("Moving termux prefix staging to prefix directory failed" );
196209 }
197210
198211 Logger .logInfo (LOG_TAG , "Bootstrap packages installed successfully." );
199212 activity .runOnUiThread (whenDone );
200213
201214 } catch (final Exception e ) {
202- showBootstrapErrorDialog (activity , PREFIX_FILE_PATH , whenDone , Logger .getStackTracesMarkdownString (null , Logger .getStackTracesStringArray (e )));
215+ showBootstrapErrorDialog (activity , whenDone , Logger .getStackTracesMarkdownString (null , Logger .getStackTracesStringArray (e )));
203216
204217 } finally {
205218 activity .runOnUiThread (() -> {
@@ -214,7 +227,7 @@ public void run() {
214227 }.start ();
215228 }
216229
217- public static void showBootstrapErrorDialog (Activity activity , String PREFIX_FILE_PATH , Runnable whenDone , String message ) {
230+ public static void showBootstrapErrorDialog (Activity activity , Runnable whenDone , String message ) {
218231 Logger .logErrorExtended (LOG_TAG , "Bootstrap Error:\n " + message );
219232
220233 // Send a notification with the exception so that the user knows why bootstrap setup failed
@@ -229,7 +242,7 @@ public static void showBootstrapErrorDialog(Activity activity, String PREFIX_FIL
229242 })
230243 .setPositiveButton (R .string .bootstrap_error_try_again , (dialog , which ) -> {
231244 dialog .dismiss ();
232- FileUtils .deleteFile ("prefix directory" , PREFIX_FILE_PATH , true );
245+ FileUtils .deleteFile ("termux prefix directory" , TERMUX_PREFIX_DIR_PATH , true );
233246 TermuxInstaller .setupBootstrapIfNeeded (activity , whenDone );
234247 }).show ();
235248 } catch (WindowManager .BadTokenException e1 ) {
0 commit comments