Repository navigation
feat(terminal): upload files into selected distro #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| package org.randomcoder.udroid.runtime | ||
|
|
||
| import java.io.File | ||
| import java.io.FileOutputStream | ||
| import java.io.InputStream | ||
| import java.nio.file.AtomicMoveNotSupportedException | ||
| import java.nio.file.Files | ||
| import java.nio.file.LinkOption | ||
| import java.nio.file.StandardCopyOption | ||
| import java.nio.file.attribute.PosixFilePermission | ||
| import java.nio.file.attribute.PosixFilePermissions | ||
| import java.util.UUID | ||
|
|
||
| internal object TerminalFileImporter { | ||
| fun import( | ||
| rootfs: File, | ||
| sourceName: String?, | ||
| source: InputStream, | ||
| ): String { | ||
| val root = rootfs.toPath().toAbsolutePath().normalize() | ||
| require(Files.isDirectory(root, LinkOption.NOFOLLOW_LINKS)) { | ||
| "The selected Linux system is unavailable" | ||
| } | ||
|
|
||
| val rootHome = root.resolve("root") | ||
| check(!Files.isSymbolicLink(rootHome)) { | ||
| "The Linux home directory cannot be a symbolic link" | ||
| } | ||
| val home = if (Files.isDirectory(rootHome, LinkOption.NOFOLLOW_LINKS)) rootHome else root | ||
| val uploads = home.resolve("Uploads") | ||
| if (Files.exists(uploads, LinkOption.NOFOLLOW_LINKS)) { | ||
| check(Files.isDirectory(uploads, LinkOption.NOFOLLOW_LINKS)) { | ||
| "~/Uploads is not a directory" | ||
| } | ||
| } else { | ||
| Files.createDirectory(uploads) | ||
| } | ||
|
|
||
| val fileName = safeFileName(sourceName) | ||
| val destination = uploads.resolve(fileName) | ||
| check(!Files.exists(destination, LinkOption.NOFOLLOW_LINKS)) { | ||
| "~/Uploads/$fileName already exists" | ||
| } | ||
|
Comment on lines
+41
to
+43
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The check for an existing file is done only before the part upload; this does not guarantee that it won't exist or will be created in between the time when files are being copied from Android to the Linux path and moved to Uploads path. |
||
|
|
||
| val staging = uploads.resolve(".upload-${UUID.randomUUID()}.part") | ||
| try { | ||
| Files.createFile( | ||
| staging, | ||
| PosixFilePermissions.asFileAttribute( | ||
| setOf( | ||
| PosixFilePermission.OWNER_READ, | ||
| PosixFilePermission.OWNER_WRITE, | ||
| ), | ||
| ), | ||
| ) | ||
| FileOutputStream(staging.toFile()).use { output -> | ||
| source.copyTo(output) | ||
| output.fd.sync() | ||
| } | ||
| try { | ||
| Files.move(staging, destination, StandardCopyOption.ATOMIC_MOVE) | ||
| } catch (_: AtomicMoveNotSupportedException) { | ||
| Files.move(staging, destination) | ||
| } | ||
| } finally { | ||
| Files.deleteIfExists(staging) | ||
| } | ||
| return "~/Uploads/$fileName" | ||
| } | ||
|
|
||
| internal fun safeFileName(sourceName: String?): String { | ||
| val sanitized = | ||
| sourceName | ||
| .orEmpty() | ||
| .substringAfterLast('/') | ||
| .substringAfterLast('\\') | ||
| .map { character -> | ||
| if (character.code < 32 || character.code == 127) '_' else character | ||
| }.joinToString("") | ||
| .trim() | ||
| .take(MAX_FILE_NAME_CHARS) | ||
| return sanitized.takeUnless { it.isBlank() || it == "." || it == ".." } ?: "upload" | ||
| } | ||
|
Comment on lines
+71
to
+83
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any specific reason |
||
|
|
||
| private const val MAX_FILE_NAME_CHARS = 64 | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This implementation needs rewriting. Here, the
importresolves the absolute path of rootfs/then proceeds to consider home as/root. If it does exist, then fall backs to/but at the end returns~/Uploads/$fileName?Here, the user can be logged in as any user; the return
~/resolves to the home path of the currently logged-in user, which is not one of the possible places where this function stores the file, so unless the user is root and has/rootthe return would be invalid.