-
-
Notifications
You must be signed in to change notification settings - Fork 211
refactor: Symlink imagefs_shared home in the variants + migrate old home contents #1030
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
Open
unbelievableflavour
wants to merge
1
commit into
utkarshdalal:master
Choose a base branch
from
unbelievableflavour:home-in-imagefs-shared
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
app/src/main/java/com/winlator/xenvironment/ImageFSLegacyMigrator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package com.winlator.xenvironment; | ||
|
|
||
| import android.content.Context; | ||
| import android.util.Log; | ||
|
|
||
| import com.winlator.core.FileUtils; | ||
|
|
||
| import java.io.File; | ||
|
|
||
| public final class ImageFSLegacyMigrator { | ||
| private ImageFSLegacyMigrator() {} | ||
|
|
||
| public static boolean migrateLegacyDirsIfNeeded(Context context, File legacyImageFsRoot) { | ||
| if (!migrateLegacyHomeToShared(context, legacyImageFsRoot)) { | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /** | ||
| * Before deleting legacy files/imagefs, preserve /home contents by moving them into | ||
| * files/imagefs_shared/home so first-run sync can reuse xuser/.wine safely. | ||
| */ | ||
| private static boolean migrateLegacyHomeToShared(Context context, File legacyImageFsRoot) { | ||
| File legacyHome = new File(legacyImageFsRoot, "home"); | ||
| File sharedHomeRoot = new File(ImageFs.getImageFsSharedDir(context), "home"); | ||
|
|
||
| if (FileUtils.isSymlink(legacyHome)) { | ||
| // Already migrated: /imagefs/home is a symlink to imagefs_shared/home. | ||
| return true; | ||
| } | ||
|
|
||
| if (!legacyHome.exists() || !legacyHome.isDirectory()) { | ||
| // No need to migrate. | ||
| return true; | ||
| } | ||
|
|
||
| if (sharedHomeRoot.exists()) { | ||
| Log.w("ImageFSLegacyMigrator", "Shared home already exists; overwriting with legacy home migration."); | ||
| FileUtils.delete(sharedHomeRoot); | ||
| } | ||
|
|
||
| if (!legacyHome.renameTo(sharedHomeRoot)) { | ||
| Log.w("ImageFSLegacyMigrator", "Direct move failed for legacy home; falling back to copy+delete."); | ||
| boolean copied = FileUtils.copy(legacyHome, sharedHomeRoot); | ||
| if (copied) { | ||
| FileUtils.delete(legacyHome); | ||
| Log.i("ImageFSLegacyMigrator", "Migrated legacy home via copy+delete to: " + sharedHomeRoot.getAbsolutePath()); | ||
| return true; | ||
| } else { | ||
| Log.w("ImageFSLegacyMigrator", "Failed to migrate legacy home directory: " + legacyHome.getAbsolutePath()); | ||
| return false; | ||
| } | ||
| } else { | ||
| Log.i("ImageFSLegacyMigrator", "Migrated legacy home via direct move to: " + sharedHomeRoot.getAbsolutePath()); | ||
| return true; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/src/test/java/com/winlator/xenvironment/ImageFSLegacyMigratorTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package com.winlator.xenvironment | ||
|
|
||
| import androidx.test.core.app.ApplicationProvider | ||
| import java.io.File | ||
| import java.nio.file.Files | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertFalse | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Before | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class ImageFSLegacyMigratorTest { | ||
| private lateinit var filesDir: File | ||
| private lateinit var legacyImageFsRoot: File | ||
| private lateinit var sharedDir: File | ||
|
|
||
| @Before | ||
| fun setUp() { | ||
| val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
| filesDir = context.filesDir | ||
| legacyImageFsRoot = File(filesDir, "legacy-imagefs-test-${System.nanoTime()}").apply { mkdirs() } | ||
| sharedDir = File(filesDir, "imagefs_shared").apply { deleteRecursively() } | ||
| } | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| legacyImageFsRoot.deleteRecursively() | ||
| sharedDir.deleteRecursively() | ||
| } | ||
|
|
||
| @Test | ||
| fun migrateLegacyDirsIfNeeded_returnsTrueWhenLegacyHomeMissing() { | ||
| val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
|
|
||
| val migrated = ImageFSLegacyMigrator.migrateLegacyDirsIfNeeded(context, legacyImageFsRoot) | ||
|
|
||
| assertTrue(migrated) | ||
| assertFalse(File(legacyImageFsRoot, "home").exists()) | ||
| } | ||
|
|
||
| @Test | ||
| fun migrateLegacyDirsIfNeeded_movesLegacyHomeToSharedHome() { | ||
| val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
| val legacyHome = File(legacyImageFsRoot, "home").apply { mkdirs() } | ||
| val legacyFile = File(legacyHome, "marker.txt").apply { writeText("legacy-content") } | ||
|
|
||
| val migrated = ImageFSLegacyMigrator.migrateLegacyDirsIfNeeded(context, legacyImageFsRoot) | ||
|
|
||
| assertTrue(migrated) | ||
| assertFalse("Legacy home should have been moved away", legacyHome.exists()) | ||
| val sharedHome = File(sharedDir, "home") | ||
| assertTrue(sharedHome.exists()) | ||
| assertEquals("legacy-content", File(sharedHome, legacyFile.name).readText()) | ||
| } | ||
|
|
||
| @Test | ||
| fun migrateLegacyDirsIfNeeded_overwritesExistingSharedHomeWithLegacyHome() { | ||
| val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
| val sharedHome = File(sharedDir, "home").apply { mkdirs() } | ||
| File(sharedHome, "old.txt").writeText("old-shared") | ||
|
|
||
| val legacyHome = File(legacyImageFsRoot, "home").apply { mkdirs() } | ||
| File(legacyHome, "new.txt").writeText("new-legacy") | ||
|
|
||
| val migrated = ImageFSLegacyMigrator.migrateLegacyDirsIfNeeded(context, legacyImageFsRoot) | ||
|
|
||
| assertTrue(migrated) | ||
| assertFalse(File(sharedHome, "old.txt").exists()) | ||
| assertEquals("new-legacy", File(sharedHome, "new.txt").readText()) | ||
| } | ||
|
|
||
| @Test | ||
| fun migrateLegacyDirsIfNeeded_noopWhenLegacyHomeIsSymlink() { | ||
| val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
| val realHome = File(legacyImageFsRoot, "real-home").apply { mkdirs() } | ||
| val symlinkPath = File(legacyImageFsRoot, "home").toPath() | ||
| Files.createSymbolicLink(symlinkPath, realHome.toPath()) | ||
|
|
||
| val migrated = ImageFSLegacyMigrator.migrateLegacyDirsIfNeeded(context, legacyImageFsRoot) | ||
|
|
||
| assertTrue(migrated) | ||
| assertTrue("Symlink should remain untouched", Files.isSymbolicLink(symlinkPath)) | ||
| } | ||
| } |
120 changes: 120 additions & 0 deletions
120
app/src/test/java/com/winlator/xenvironment/ImageFsInstallerTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package com.winlator.xenvironment | ||
|
|
||
| import androidx.test.core.app.ApplicationProvider | ||
| import com.winlator.core.FileUtils | ||
| import java.io.File | ||
| import java.lang.reflect.Method | ||
| import io.mockk.every | ||
| import io.mockk.mockkStatic | ||
| import io.mockk.unmockkStatic | ||
| import io.mockk.verify | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class ImageFsInstallerTest { | ||
| private val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
| private val filesDir = context.filesDir | ||
| private val sharedDir = File(filesDir, "imagefs_shared") | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| sharedDir.deleteRecursively() | ||
| } | ||
|
|
||
| @Test | ||
| fun ensureSharedHomeRoot_callsSymlinkWhenHomeIsNotSymlink() { | ||
| val rootDir = File(filesDir, "imagefs-root-${System.nanoTime()}").apply { mkdirs() } | ||
| val sharedHome = File(sharedDir, "home") | ||
| val imageFsHome = File(rootDir, "home") | ||
| val expectedTarget = sharedHome.path | ||
| val expectedLink = imageFsHome.path | ||
|
|
||
| mockkStatic(FileUtils::class) | ||
| try { | ||
| every { FileUtils.isSymlink(any()) } returns false | ||
| every { FileUtils.symlink(any<String>(), any<String>()) } returns Unit | ||
|
|
||
| invokeEnsureSharedHomeRoot(context, rootDir) | ||
|
|
||
| assertTrue("Shared home should be created", sharedHome.exists()) | ||
| verify(exactly = 1) { FileUtils.symlink(expectedTarget, expectedLink) } | ||
| } finally { | ||
| unmockkStatic(FileUtils::class) | ||
| } | ||
|
|
||
| rootDir.deleteRecursively() | ||
| } | ||
|
|
||
| @Test | ||
| fun ensureSharedHomeRoot_doesNotCallSymlinkWhenHomeAlreadySymlink() { | ||
| val rootDir = File(filesDir, "imagefs-root-symlink-${System.nanoTime()}").apply { mkdirs() } | ||
| val sharedHome = File(sharedDir, "home") | ||
| val imageFsHome = File(rootDir, "home") | ||
|
|
||
| mockkStatic(FileUtils::class) | ||
| try { | ||
| every { FileUtils.isSymlink(imageFsHome) } returns true | ||
| every { FileUtils.symlink(any<String>(), any<String>()) } returns Unit | ||
|
|
||
| invokeEnsureSharedHomeRoot(context, rootDir) | ||
|
|
||
| assertTrue("Shared home should still be created", sharedHome.exists()) | ||
| verify(exactly = 0) { FileUtils.symlink(any<String>(), any<String>()) } | ||
| } finally { | ||
| unmockkStatic(FileUtils::class) | ||
| } | ||
|
|
||
| rootDir.deleteRecursively() | ||
| } | ||
|
|
||
| @Test | ||
| fun ensureSharedHomeRoot_alwaysCreatesSharedHomeDirectory() { | ||
| val rootDir = File(filesDir, "imagefs-root-shared-home-${System.nanoTime()}").apply { mkdirs() } | ||
| val sharedHome = File(sharedDir, "home") | ||
|
|
||
| invokeEnsureSharedHomeRoot(context, rootDir) | ||
|
|
||
| assertTrue("Shared home should always be created", sharedHome.exists()) | ||
| assertTrue("Shared home should be a directory", sharedHome.isDirectory) | ||
|
|
||
| rootDir.deleteRecursively() | ||
| } | ||
|
|
||
| @Test | ||
| fun ensureSharedHomeRoot_usesExpectedLinkAndTargetPaths() { | ||
| val rootDir = File(filesDir, "imagefs-root-paths-${System.nanoTime()}").apply { mkdirs() } | ||
| val sharedHome = File(sharedDir, "home") | ||
| val imageFsHome = File(rootDir, "home") | ||
|
|
||
| mockkStatic(FileUtils::class) | ||
| try { | ||
| every { FileUtils.isSymlink(any()) } returns false | ||
| every { FileUtils.symlink(any<String>(), any<String>()) } returns Unit | ||
|
|
||
| invokeEnsureSharedHomeRoot(context, rootDir) | ||
|
|
||
| verify(exactly = 1) { FileUtils.symlink(sharedHome.path, imageFsHome.path) } | ||
| assertEquals(sharedHome.path, File(sharedDir, "home").path) | ||
| } finally { | ||
| unmockkStatic(FileUtils::class) | ||
| } | ||
|
|
||
| rootDir.deleteRecursively() | ||
| } | ||
|
|
||
| private fun invokeEnsureSharedHomeRoot(context: android.content.Context, rootDir: File) { | ||
| val method: Method = ImageFsInstaller::class.java.getDeclaredMethod( | ||
| "ensureSharedHomeRoot", | ||
| android.content.Context::class.java, | ||
| File::class.java, | ||
| ) | ||
| method.isAccessible = true | ||
| method.invoke(null, context, rootDir) | ||
| } | ||
|
|
||
| } |
31 changes: 31 additions & 0 deletions
31
app/src/test/java/com/winlator/xenvironment/ImageFsTest.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package com.winlator.xenvironment | ||
|
|
||
| import androidx.test.core.app.ApplicationProvider | ||
| import java.io.File | ||
| import org.junit.After | ||
| import org.junit.Assert.assertEquals | ||
| import org.junit.Assert.assertTrue | ||
| import org.junit.Test | ||
| import org.junit.runner.RunWith | ||
| import org.robolectric.RobolectricTestRunner | ||
|
|
||
| @RunWith(RobolectricTestRunner::class) | ||
| class ImageFsTest { | ||
| private val context = ApplicationProvider.getApplicationContext<android.content.Context>() | ||
| private val sharedDir = File(context.filesDir, "imagefs_shared") | ||
|
|
||
| @After | ||
| fun tearDown() { | ||
| sharedDir.deleteRecursively() | ||
| } | ||
|
|
||
| @Test | ||
| fun getImageFsSharedDir_createsAndReturnsSharedDirectory() { | ||
| val actual = ImageFs.getImageFsSharedDir(context) | ||
| val expected = File(context.filesDir, "imagefs_shared") | ||
|
|
||
| assertTrue("Shared dir should exist after call", actual.exists()) | ||
| assertTrue("Shared dir should be a directory", actual.isDirectory) | ||
| assertEquals(expected.absolutePath, actual.absolutePath) | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.