diff --git a/app/src/main/java/com/httrack/android/HTTrackActivity.java b/app/src/main/java/com/httrack/android/HTTrackActivity.java index ab7829a..3e2e93b 100755 --- a/app/src/main/java/com/httrack/android/HTTrackActivity.java +++ b/app/src/main/java/com/httrack/android/HTTrackActivity.java @@ -903,18 +903,25 @@ protected static boolean isIPv6Enabled() { } /** - * Emergency dump. - */ - protected static void emergencyDump(final Throwable e) { - try { - final File dumpFile = new File(new File( - Environment.getExternalStorageState(), "HTTrack"), "error.txt"); - final FileWriter writer = new FileWriter(dumpFile); - final PrintWriter print = new PrintWriter(writer); + * Emergency dump, into storage of our own: this runs on a crash path, so it must not depend + * on a permission, nor on a volume being mounted. + * + * @param context + * null while the activity is being torn down, in which case nothing is written + * @param e + * the throwable to record + */ + protected static void emergencyDump(final Context context, final Throwable e) { + if (context == null) { + return; + } + final File external = context.getExternalFilesDir(null); + final File dumpFile = new File( + external != null ? external : context.getFilesDir(), "error.txt"); + try (final PrintWriter print = new PrintWriter(new FileWriter(dumpFile))) { e.printStackTrace(print); - writer.close(); - HTTrackActivity.setFileReadWrite(dumpFile); } catch (final IOException io) { + Log.w(HTTrackActivity.class.getSimpleName(), "could not write " + dumpFile, io); } } @@ -1118,7 +1125,7 @@ protected Void doInBackground(final Void... arg0) { try { runInternal(); } catch (final RuntimeException e) { - HTTrackActivity.emergencyDump(e); + HTTrackActivity.emergencyDump(parent, e); throw e; } finally { ended = true; diff --git a/app/src/main/jni/htslibjni.c b/app/src/main/jni/htslibjni.c index 0884d5b..9ac4d2a 100755 --- a/app/src/main/jni/htslibjni.c +++ b/app/src/main/jni/htslibjni.c @@ -29,6 +29,8 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #include #include +#include +#include #include @@ -62,15 +64,19 @@ static char *rootPath = NULL; /* log assertion failure. */ static void log_assert_failure(const char* exp, const char* file, int line) { __android_log_print(ANDROID_LOG_VERBOSE, "httrack", "assertion '%s' failed at %s:%d", exp, file, line); -#define MKDIR_MODE (S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH) - /* FIXME TODO: pass the getExternalStorageDirectory() in init. */ - FILE *dumpFile; - const char *const filename = emergencyLog != NULL ? emergencyLog - : "/mnt/sdcard/Download/HTTrack/error.txt"; - dumpFile = fopen(filename, "wb"); - if (dumpFile != NULL) { - fprintf(dumpFile, "assertion '%s' failed at %s:%d\n", exp, file, line); - fclose(dumpFile); + /* Nothing is writable before initRootPath(); logcat above carries it either way. */ + if (emergencyLog != NULL) { + /* Owner-only (0600): a crash log created with fopen's default 0666 is world-writable. */ + const int fd = open(emergencyLog, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR); + if (fd != -1) { + FILE *const dumpFile = fdopen(fd, "wb"); + if (dumpFile != NULL) { + fprintf(dumpFile, "assertion '%s' failed at %s:%d\n", exp, file, line); + fclose(dumpFile); + } else { + close(fd); + } + } } }