Skip to content

Commit 53e00ca

Browse files
committed
fix: prevent relocation to root directories
1 parent b0b84b7 commit 53e00ca

File tree

8 files changed

+50
-0
lines changed

8 files changed

+50
-0
lines changed

web-app/src/locales/en/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,8 @@
207207
"showInFileExplorer": "Show in File Explorer",
208208
"openContainingFolder": "Open Containing Folder",
209209
"failedToRelocateDataFolder": "Failed to relocate data folder",
210+
"couldNotRelocateToRoot": "Cannot relocate data folder to root directory. Please choose another location.",
211+
"couldNotResetRootDirectory": "Cannot reset data folder when it's set to a root directory. Please delete the data folder manually.",
210212
"failedToRelocateDataFolderDesc": "Failed to relocate data folder. Please try again.",
211213
"devVersion": "Development version detected",
212214
"noUpdateAvailable": "You're running the latest version",

web-app/src/locales/id/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
"showInFinder": "Tampilkan di Finder",
204204
"showInFileExplorer": "Tampilkan di File Explorer",
205205
"openContainingFolder": "Buka Folder Induk",
206+
"couldNotRelocateToRoot": "Tidak dapat memindahkan folder data ke direktori root. Silakan pilih lokasi lain.",
207+
"couldNotResetRootDirectory": "Tidak dapat mengatur ulang folder data saat diatur ke direktori root. Silakan hapus folder data secara manual.",
206208
"failedToRelocateDataFolder": "Gagal memindahkan folder data",
207209
"failedToRelocateDataFolderDesc": "Gagal memindahkan folder data. Silakan coba lagi.",
208210
"devVersion": "Versi pengembangan terdeteksi",

web-app/src/locales/pl/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@
206206
"showInFinder": "Pokaż w Finderze",
207207
"showInFileExplorer": "Pokaż w Explorerze",
208208
"openContainingFolder": "Otwórz Katalog Nadrzędny",
209+
"couldNotRelocateToRoot": "Nie można przenieść folderu danych do katalogu głównego. Proszę wybrać inną lokalizację.",
210+
"couldNotResetRootDirectory": "Nie można zresetować folderu danych, gdy jest ustawiony na katalog główny. Proszę ręcznie usunąć folder danych.",
209211
"failedToRelocateDataFolder": "Błąd zmiany katalogu danych",
210212
"failedToRelocateDataFolderDesc": "Nie udało się przenieść katalogu danych. Proszę spróbować później.",
211213
"devVersion": "Wykryto wersję deweloperską",

web-app/src/locales/vn/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
"showInFinder": "Hiển thị trong Finder",
204204
"showInFileExplorer": "Hiển thị trong File Explorer",
205205
"openContainingFolder": "Mở Thư mục Chứa",
206+
"couldNotRelocateToRoot": "Không thể di chuyển thư mục dữ liệu đến thư mục gốc. Vui lòng chọn vị trí khác.",
207+
"couldNotResetRootDirectory": "Không thể đặt lại thư mục dữ liệu khi nó được đặt thành thư mục gốc. Vui lòng xóa thư mục dữ liệu thủ công.",
206208
"failedToRelocateDataFolder": "Không thể di chuyển thư mục dữ liệu",
207209
"failedToRelocateDataFolderDesc": "Không thể di chuyển thư mục dữ liệu. Vui lòng thử lại.",
208210
"devVersion": "Đã phát hiện phiên bản phát triển",

web-app/src/locales/zh-CN/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
"showInFinder": "在 Finder 中显示",
204204
"showInFileExplorer": "在文件资源管理器中显示",
205205
"openContainingFolder": "打开包含文件夹",
206+
"couldNotRelocateToRoot": "无法将数据文件夹重新定位到根目录。请选择其他位置。",
207+
"couldNotResetRootDirectory": "当数据文件夹设置为根目录时,无法重置。请手动删除数据文件夹。",
206208
"failedToRelocateDataFolder": "无法重新定位数据文件夹",
207209
"failedToRelocateDataFolderDesc": "无法重新定位数据文件夹。请重试。",
208210
"devVersion": "检测到开发版本",

web-app/src/locales/zh-TW/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,8 @@
203203
"showInFinder": "在 Finder 中顯示",
204204
"showInFileExplorer": "在檔案總管中顯示",
205205
"openContainingFolder": "打開包含資料夾",
206+
"couldNotRelocateToRoot": "無法將資料檔案夾重新定位到根目錄。請選擇其他位置。",
207+
"couldNotResetRootDirectory": "當資料檔案夾設定為根目錄時,無法重置。請手動刪除資料檔案夾。",
206208
"failedToRelocateDataFolder": "無法重新定位資料夾",
207209
"failedToRelocateDataFolderDesc": "無法重新定位資料夾。請重試。",
208210
"devVersion": "檢測到開發版本",

web-app/src/routes/settings/general.tsx

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { useHardware } from '@/hooks/useHardware'
3030
import LanguageSwitcher from '@/containers/LanguageSwitcher'
3131
import { PlatformFeatures } from '@/lib/platform/const'
3232
import { PlatformFeature } from '@/lib/platform/types'
33+
import { isRootDir } from '@/utils/path'
3334

3435
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3536
export const Route = createFileRoute(route.settings.general as any)({
@@ -73,6 +74,11 @@ function General() {
7374
}, [serviceHub])
7475

7576
const resetApp = async () => {
77+
// Prevent resetting if data folder is root directory
78+
if (isRootDir(janDataFolder ?? '/')) {
79+
toast.error(t('settings:general.couldNotResetRootDirectory'))
80+
return
81+
}
7682
pausePolling()
7783
// TODO: Loading indicator
7884
await serviceHub.app().factoryReset()
@@ -117,6 +123,9 @@ function General() {
117123
serviceHub.events().emit(SystemEvent.KILL_SIDECAR)
118124
setTimeout(async () => {
119125
try {
126+
// Prevent relocating to root directory (e.g., C:\ or D:\ on Windows, / on Unix)
127+
if (isRootDir(selectedNewPath))
128+
throw new Error(t('settings:general.couldNotRelocateToRoot'))
120129
await serviceHub.app().relocateJanDataFolder(selectedNewPath)
121130
setJanDataFolder(selectedNewPath)
122131
// Only relaunch if relocation was successful

web-app/src/utils/path.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Determines if the given path is a root directory.
3+
*
4+
* On Windows, this checks for drive roots such as `C:\` or `D:\`.
5+
* On Mac/Linux, this checks if the path is `/`.
6+
*
7+
* @param selectedNewPath - The path to check.
8+
* @returns `true` if the path is a root directory, otherwise `false`.
9+
*/
10+
export const isRootDir = (selectedNewPath: string) => {
11+
// Windows root: C:\, D:\, etc.
12+
if (IS_WINDOWS) {
13+
return /^[a-zA-Z]:\\?$/.test(selectedNewPath)
14+
}
15+
// Linux/Mac root: /, /mnt, /media, etc.
16+
const linuxRoots = [
17+
'/',
18+
'/mnt',
19+
'/media',
20+
'/boot',
21+
'/home',
22+
'/opt',
23+
'/var',
24+
'/usr',
25+
]
26+
const normalized =
27+
selectedNewPath.replace(/\\/g, '/').replace(/\/+$/, '') || '/'
28+
return linuxRoots.some((root) => normalized === root)
29+
}

0 commit comments

Comments
 (0)