From 18ba4b59b0bebf1282a6b47bd2ac93e0ef6fefb3 Mon Sep 17 00:00:00 2001 From: Emmanuel Raviart Date: Tue, 28 Nov 2023 21:45:07 +0100 Subject: [PATCH] Handle symlinks when unzipping & updating datasets repositories --- src/lib/server/file_systems.ts | 2 +- src/scripts/download_dila_dataset.ts | 25 ++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/lib/server/file_systems.ts b/src/lib/server/file_systems.ts index 9a3fc59..212fc66 100644 --- a/src/lib/server/file_systems.ts +++ b/src/lib/server/file_systems.ts @@ -12,7 +12,7 @@ export function* walkDir( } const filePath = path.join(dir, filename) const relativeSplitPath = [...relativeSplitDir, filename] - if (fs.statSync(filePath).isDirectory()) { + if (fs.lstatSync(filePath).isDirectory()) { yield* walkDir(rootDir, relativeSplitPath) } else { yield relativeSplitPath diff --git a/src/scripts/download_dila_dataset.ts b/src/scripts/download_dila_dataset.ts index 77aa3e2..81139de 100644 --- a/src/scripts/download_dila_dataset.ts +++ b/src/scripts/download_dila_dataset.ts @@ -5,6 +5,8 @@ import path from "path" import sade from "sade" import { $, cd } from "zx" +import { walkDir } from "$lib/server/file_systems" + async function downloadDataset( datasetName: string, dilaDir: string, @@ -99,7 +101,28 @@ async function downloadDataset( ", ", )}`, ) - await $`cp -r ${date}/${datasetName}/* ${datasetName}/` + + // When archive contains a symbolic link and dataset contains at the same path + // something that is not a symbolic link, remove it before copy, otherwise + // copy of symbolic link will fail. + const archiveDatasetDir = path.join(date, datasetName) + for (const relativeSplitPath of walkDir(archiveDatasetDir)) { + const archiveFilePath = path.join( + archiveDatasetDir, + ...relativeSplitPath, + ) + if (fs.lstatSync(archiveFilePath).isSymbolicLink()) { + const datasetFilePath = path.join(datasetName, ...relativeSplitPath) + if ( + fs.pathExistsSync(datasetFilePath) && + !fs.lstatSync(datasetFilePath).isSymbolicLink() + ) { + fs.removeSync(datasetFilePath) + } + } + } + + await $`cp -r ${archiveDatasetDir}/* ${datasetName}/` const removalListFilePath = path.join( date, `liste_suppression_${datasetName}.dat`,