Skip to content

Commit

Permalink
feat: add more local path checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Dwynr committed Sep 19, 2024
1 parent 5928a92 commit e1a7cec
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@filen/sync",
"version": "0.1.62",
"version": "0.1.63",
"description": "Filen Sync",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 9 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import FilenSDK, { type FilenSDKConfig } from "@filen/sdk"
import { Semaphore } from "./semaphore"
import { SYNC_INTERVAL } from "./constants"
import Logger from "./lib/logger"
import { tryingToSyncDesktop, isPathSyncedByICloud } from "./utils"

/**
* SyncWorker
Expand Down Expand Up @@ -150,6 +151,14 @@ export class SyncWorker {

for (const pair of pairs) {
if (!this.syncs[pair.uuid]) {
if (await isPathSyncedByICloud(pair.localPath)) {
throw new Error(`Cannot sync local path "${pair.localPath}": Already synced by iCloud.`)
}

if (tryingToSyncDesktop(pair.localPath)) {
throw new Error(`Cannot sync local path "${pair.localPath}": Desktop sync not allowed.`)
}

this.syncs[pair.uuid] = new Sync({
syncPair: pair,
worker: this
Expand Down
41 changes: 41 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { DEFAULT_IGNORED } from "./constants"
import pathModule from "path"
import crypto from "crypto"
import { exec } from "child_process"

/**
* Chunk large Promise.all executions.
Expand Down Expand Up @@ -301,3 +302,43 @@ export function normalizeLastModifiedMsForComparison(time: number): number {
export function fastHash(input: string): string {
return crypto.createHash("md5").update(input).digest("hex")
}

export function tryingToSyncDesktop(path: string): boolean {
if (process.platform !== "darwin") {
return false
}

return (
path.trim().toLowerCase() === `/users/${process.env.USER ?? "user"}/desktop` ||
path.trim().toLowerCase() === `/users/${process.env.USER ?? "user"}/desktop/`
)
}

export async function isPathSyncedByICloud(path: string): Promise<boolean> {
if (process.platform !== "darwin") {
return false
}

return await new Promise<boolean>((resolve, reject) => {
exec(`xattr "${path}"`, (err, stdout, stderr) => {
if (err) {
reject(err)

return
}

if (stderr) {
reject(stderr)

return
}

resolve(
stdout.toLowerCase().includes("com.apple.cloud") ||
stdout.toLowerCase().includes("com.apple.icloud") ||
stdout.toLowerCase().includes("com.apple.fileprovider") ||
stdout.toLowerCase().includes("com.apple.file-provider")
)
})
})
}

0 comments on commit e1a7cec

Please sign in to comment.