Skip to content

Commit bce65f7

Browse files
committed
Setup $HOME/storage symlink for external symlink
At startup Termux now checks if there is external storage available, and creates a private area on the external storage if one exists as well as creating a symlink to the private are at $HOME/storage.
1 parent e185791 commit bce65f7

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

app/src/main/java/com/termux/app/TermuxActivity.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,8 @@ public void onClick(View v) {
293293

294294
mTerminalView.checkForTypeface();
295295
mTerminalView.checkForColors();
296+
297+
TermuxInstaller.setupStorageSymlink(this);
296298
}
297299

298300
/**

app/src/main/java/com/termux/app/TermuxInstaller.java

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
package com.termux.app;
22

3-
import java.io.BufferedReader;
4-
import java.io.File;
5-
import java.io.FileOutputStream;
6-
import java.io.InputStreamReader;
7-
import java.net.MalformedURLException;
8-
import java.net.URL;
9-
import java.util.ArrayList;
10-
import java.util.List;
11-
import java.util.zip.ZipEntry;
12-
import java.util.zip.ZipInputStream;
13-
143
import android.app.Activity;
154
import android.app.AlertDialog;
165
import android.app.ProgressDialog;
@@ -26,6 +15,17 @@
2615
import com.termux.R;
2716
import com.termux.terminal.EmulatorDebug;
2817

18+
import java.io.BufferedReader;
19+
import java.io.File;
20+
import java.io.FileOutputStream;
21+
import java.io.InputStreamReader;
22+
import java.net.MalformedURLException;
23+
import java.net.URL;
24+
import java.util.ArrayList;
25+
import java.util.List;
26+
import java.util.zip.ZipEntry;
27+
import java.util.zip.ZipInputStream;
28+
2929
/**
3030
* Install the Termux bootstrap packages if necessary by following the below steps:
3131
*
@@ -200,4 +200,36 @@ static void deleteFolder(File fileOrDirectory) {
200200
}
201201
}
202202

203+
public static void setupStorageSymlink(final Context context) {
204+
final File[] dirs = context.getExternalFilesDirs(null);
205+
if (dirs == null || dirs.length < 2) return;
206+
new Thread() {
207+
public void run() {
208+
try {
209+
final File externalDir = dirs[1];
210+
File homeDir = new File(TermuxService.HOME_PATH);
211+
homeDir.mkdirs();
212+
File externalLink = new File(homeDir, "storage");
213+
214+
if (externalLink.exists()) {
215+
if (externalLink.getCanonicalPath().equals(externalDir.getPath())) {
216+
// Keeping existing link.
217+
return;
218+
} else {
219+
// Removing old link to give place to new.
220+
if (!externalLink.delete()) {
221+
Log.e("termux", "Unable to remove old $HOME/storage to give place for new");
222+
return;
223+
}
224+
}
225+
}
226+
227+
Os.symlink(externalDir.getAbsolutePath(), externalLink.getAbsolutePath());
228+
} catch (Exception e) {
229+
Log.e("termux", "Error setting up link", e);
230+
}
231+
}
232+
}.start();
233+
}
234+
203235
}

0 commit comments

Comments
 (0)