Skip to content

Commit

Permalink
fix: watcher instance, path validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Sep 6, 2024
1 parent 01da1a6 commit 862038b
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 19 deletions.
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/sync",
"version": "0.1.55",
"version": "0.1.56",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down Expand Up @@ -56,6 +56,7 @@
"@parcel/watcher": "^2.4.1",
"fs-extra": "^11.2.0",
"ignore": "^5.3.1",
"node-watch": "^0.7.4",
"pino": "^9.3.2",
"rotating-file-stream": "^3.2.3",
"uuid": "^10.0.0",
Expand Down
40 changes: 32 additions & 8 deletions src/lib/filesystems/local.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { type CloudItem, PauseSignal } from "@filen/sdk"
import { postMessageToMain } from "../ipc"
import { Semaphore } from "../../semaphore"
import { v4 as uuidv4 } from "uuid"
import { type Watcher } from "node-watch"

const pipelineAsync = promisify(pipeline)

Expand All @@ -33,16 +34,19 @@ export type LocalItem = {

export type LocalDirectoryTree = Record<string, LocalItem>
export type LocalDirectoryINodes = Record<number, LocalItem>

export type LocalTree = {
tree: LocalDirectoryTree
inodes: LocalDirectoryINodes
}

export type LocalTreeError = {
localPath: string
relativePath: string
error: Error
uuid: string
}

export type LocalTreeIgnoredReason =
| "dotFile"
| "filenIgnore"
Expand All @@ -52,6 +56,7 @@ export type LocalTreeIgnoredReason =
| "invalidType"
| "duplicate"
| "permissions"

export type LocalTreeIgnored = {
localPath: string
relativePath: string
Expand Down Expand Up @@ -83,7 +88,8 @@ export class LocalFileSystem {
errors: []
}
public watcherRunning = false
private watcherInstance: watcher.AsyncSubscription | null = null
private watcherInstanceParcel: watcher.AsyncSubscription | null = null
private watcherInstanceNode: Watcher | null = null
public readonly itemsMutex = new Semaphore(1)
public readonly mutex = new Semaphore(1)
public readonly mkdirMutex = new Semaphore(1)
Expand Down Expand Up @@ -301,19 +307,18 @@ export class LocalFileSystem {
await this.watcherMutex.acquire()

try {
if (this.watcherInstance) {
if (this.watcherInstanceParcel || this.watcherInstanceNode) {
return
}

this.watcherInstance = await watcher.subscribe(
this.watcherInstanceParcel = await watcher.subscribe(
this.sync.syncPair.localPath,
(err, events) => {
if (!err && events && events.length > 0) {
this.lastDirectoryChangeTimestamp = Date.now()
}
},
{
ignore: [".filen.trash.local"],
backend:
process.platform === "win32"
? "windows"
Expand All @@ -325,6 +330,21 @@ export class LocalFileSystem {
}
)

/*
this.watcherInstanceNode = nodeWatch(
this.sync.syncPair.localPath,
{
persistent: true,
recursive: true,
encoding: "utf8",
delay: 1000
},
() => {
this.lastDirectoryChangeTimestamp = Date.now()
}
)
*/

clearInterval(this.watcherInstanceFallbackInterval)
} catch (e) {
this.sync.worker.logger.log("error", e, "startDirectoryWatcher")
Expand Down Expand Up @@ -367,13 +387,17 @@ export class LocalFileSystem {
clearInterval(this.watcherInstanceFallbackInterval)

try {
if (!this.watcherInstance) {
return
if (this.watcherInstanceParcel) {
await this.watcherInstanceParcel.unsubscribe()

this.watcherInstanceParcel = null
}

await this.watcherInstance.unsubscribe()
if (this.watcherInstanceNode && !this.watcherInstanceNode.isClosed()) {
this.watcherInstanceNode.close()

this.watcherInstance = null
this.watcherInstanceNode = null
}
} finally {
this.watcherMutex.release()
}
Expand Down
3 changes: 3 additions & 0 deletions src/lib/filesystems/remote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ import writeFileAtomic from "write-file-atomic"
export type RemoteItem = Prettify<DistributiveOmit<CloudItemTree, "parent" | "color" | "favorited" | "timestamp"> & { path: string }>
export type RemoteDirectoryTree = Record<string, RemoteItem>
export type RemoteDirectoryUUIDs = Record<string, RemoteItem>

export type RemoteTree = {
tree: RemoteDirectoryTree
uuids: RemoteDirectoryUUIDs
}

export type RemoteTreeIgnoredReason =
| "dotFile"
| "invalidPath"
Expand All @@ -38,6 +40,7 @@ export type RemoteTreeIgnoredReason =
| "defaultIgnore"
| "empty"
| "duplicate"

export type RemoteTreeIgnored = {
localPath: string
relativePath: string
Expand Down
23 changes: 13 additions & 10 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,22 +98,25 @@ export function isNameOverMaxLength(name: string): boolean {
return name.length + 1 > 255
}

export function isValidPath(inputPath: string): boolean {
// eslint-disable-next-line no-control-regex
const illegalCharsWindows = /[<>:"/\\|?*\x00-\x1F]/
const reservedNamesWindows = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i
// eslint-disable-next-line no-control-regex, no-useless-escape
const illegalCharsMacOS = /[\/:\x00]/
// eslint-disable-next-line no-control-regex
const illegalCharsLinux = /[\x00]/
// eslint-disable-next-line no-control-regex
export const illegalCharsWindows = /[<>:"/\\|?*\x00-\x1F]/
export const reservedNamesWindows = /^(con|prn|aux|nul|com[1-9]|lpt[1-9])$/i
// eslint-disable-next-line no-control-regex, no-useless-escape
export const illegalCharsMacOS = /[\/:\x00]/
// eslint-disable-next-line no-control-regex
export const illegalCharsLinux = /[\x00]/

export function isValidPath(inputPath: string): boolean {
if (process.platform === "win32") {
inputPath = inputPath.replace(/\\/g, "/")
}

if (inputPath.includes("..")) {
/*if (
(process.platform === "win32" && inputPath.includes("\\..")) ||
((process.platform === "linux" || process.platform === "darwin") && inputPath.includes("/.."))
) {
return false
}
}*/

const parts = inputPath.split("/")

Expand Down

0 comments on commit 862038b

Please sign in to comment.