-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathseafile-symlink.ps1
611 lines (475 loc) · 19.8 KB
/
seafile-symlink.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
#Requires -Version 5
<#
.SYNOPSIS
Saves symlinks to a syncable format and restores them therefrom.
.DESCRIPTION
Has the ability to save symlink date via placeholder files, or via
`seafile-symlink.txt` in library root. Create custom ini files in
the `preset` directory. Tell the script which ini file to use via
the `-Preset` param.
Meant to address https://github.com/haiwen/seafile/issues/288
.PARAMETER Preset
Specifies which config file to use in the `presets` subdirectory.
.OUTPUTS
None
.NOTES
Version: 1.0
Author: Illya Moskvin <[email protected]>
Creation Date: 2018-07-07
License: MIT
.EXAMPLE
.\seafile-symlink.ps1 -Prefix MyCustomPreset
This will search for `.\presets\MyCustomPreset.ini` and load its config.
.EXAMPLE
.\seafile-symlink.ps1 -Prefix C:\foobar\custom.ini
This will search for `C:\foobar\custom.ini` and load its config.
#>
# Specify -Preset as a param when calling this script to use custom ini files.
# Ex: `.\seafile-symlink.ps1 -Preset Custom` to use `.\presets\custom.ini`
# Params are capitalization-agnostic so `-preset custom` would work just as well.
param (
[string]
$Preset='default'
)
# Read INI file into hashtable. Adapted from these examples:
# https://blogs.technet.microsoft.com/heyscriptingguy/2011/08/20/use-powershell-to-work-with-any-ini-file/
# https://serverfault.com/questions/186030/how-to-use-a-config-file-ini-conf-with-a-powershell-script-is-it-possib
# We'll ignore [Sections], comments (;), and require strings to be wrapped in quotes.
function Get-IniContent ([string]$FilePath) {
$ini = @{}
switch -regex -file $FilePath
{
"(.+?)\s*=(.*)" # Key
{
$k,$v = $matches[1..2]
# Trim trailing comments, if present
$c = $v.IndexOf(';')
if ($c -gt 0) {
$v = $v.Substring(0, $c)
}
$v = Invoke-Expression($v)
$ini[$k] = $v
}
}
$ini
}
# Given a preset name, load and validate its config file.
function Get-Config ([string]$Preset) {
# Unless an absolute path is provided, look in the `presets` directory
if (![System.IO.Path]::IsPathRooted($Preset)) {
if (!$Preset.EndsWith('.ini')) {
$Preset += '.ini'
}
$Preset = $PSScriptRoot + '\presets\' + $Preset
}
if (!(Test-Path $Preset)) {
Write-Host 'Config not found:' $Preset
exit 1
}
$config = Get-IniContent ($Preset)
$keys = @('LibraryPath', 'StorageMethod', 'PlaceholderExt')
foreach ($key in $keys) {
if (!$config[$key]) {
Write-Host 'Missing key in config:' $key
exit 1
}
}
if (!(Test-Path $config['LibraryPath'])) {
Write-Host 'Cannot resolve LibraryPath:' $config['LibraryPath']
exit 1
}
# Modify this via ini if the script isn't in a subfolder of a Seafile library
$config['LibraryPath'] = Get-AbsolutePath $config['LibraryPath'] $PSScriptRoot
# Ensure that LibraryPath points to a directory
Assert-IsDirectory $config['LibraryPath'] 'LibraryPath'
# Ensure that the specified storage method is valid
$storageMethods = @('database','placeholder')
if (!($storageMethods -contains $config['StorageMethod'])) {
Write-Host 'Invalid StorageMethod:' $config['StorageMethod']
Write-Host 'Valid methods:' ($storageMethods -join ', ')
exit 1
}
# Extension to use for reading or writing symlink placeholders, with leading period
$config['PlaceholderExt'] = $config['PlaceholderExt'] -replace '^\.*(.*)$', '.$1'
$config
}
# Convert an array into a hastable, with every two array members forming a name-value pair.
# https://stackoverflow.com/questions/27764394/get-valuefromremainingarguments-as-an-hashtable
function Get-ParamHash ([string[]]$ParamArray) {
$ParamHash = @{}
for ($i = 0; $i -lt $ParamArray.count; $i+=2) {
$ParamHash[($ParamArray[$i] -replace '^-+' -replace ':$')] = $ParamArray[$i+1]
}
$ParamHash
}
# Helper to find files recursively within a given directory. Returns absolute paths.
# Accepts the same named params as `Get-ChildItem`... except maybe `Path`?
# The cucial difference vs. `Get-ChildItem` is that this doesn't follow symlinks.
# First positional param is the path to the directory within which to search.
function Get-NonsymbolicPaths {
param (
[string]
$DirPath,
[parameter(ValueFromRemainingArguments=$true)]
[string[]]
$ParamArray
)
process {
# Convert param list to hash
$params = Get-ParamHash $ParamArray
# Get symbolic links located directly within this directory
$links = @(Get-ChildItem -Path "$DirPath\*" @params | ForEach-Object { $_.FullName })
# Get all subdirectories, excluding symbolic links
$subdirs = @(Get-ChildItem -Path $DirPath -Attributes Directory+!ReparsePoint)
# Call this function on each subdirectory and append the result
foreach ($subdir in $subdirs) {
$links += Get-NonsymbolicPaths $subdir.FullName @params
}
@($links | Where-Object { $_ }) # Remove empty items
}
}
# This should detect SymbolicLinks, but not HardLinks or Junctures (intentionally)
# https://stackoverflow.com/questions/817794/find-out-whether-a-file-is-a-symbolic-link-in-powershell
function Get-SymbolicLinkPaths ([string]$DirPath) {
Get-NonsymbolicPaths $DirPath -Attributes ReparsePoint
}
# Find placeholder files recursively, returning their paths.
function Get-PlaceholderPaths ([string]$DirPath, [string]$PlaceholderExt) {
Get-NonsymbolicPaths $DirPath -Include "*$PlaceholderExt"
}
function Test-IsDirectory ([string]$Path) {
(Get-Item -Path $Path) -is [System.IO.DirectoryInfo]
}
function Assert-IsDirectory ([string]$DirPath, [string]$Param) {
if (!(Test-IsDirectory $DirPath)) {
$method = (Get-PSCallStack)[1].Command
Write-Host "$method expects $Param to be a directory."
exit 1
}
}
# Helper function for retrieving one path relative to another
# Calls Resolve-Path but works for files that don't exist.
# https://stackoverflow.com/questions/3038337/powershell-resolve-path-that-might-not-exist
function Get-RelativePath ([string]$Path, [string]$DirPath) {
Assert-IsDirectory $DirPath 'DirPath'
Push-Location -Path $DirPath
$out = Resolve-Path $Path -Relative -ErrorAction 'SilentlyContinue' -ErrorVariable '_frperror'
if (-not($out)) {
$out = $_frperror[0].TargetObject
}
Pop-Location
$out
}
# Given a potentially Windows-style path, convert it to Unix-style.
# https://stackoverflow.com/questions/34286173/changing-windows-path-to-unix-path
function Get-NormalizedPath ([string]$Path) {
if ($Path.StartsWith('.\')) {
$Path = $Path.TrimStart('.\')
} elseif (-not $Path.StartsWith('..')) {
$Path = '\' + $Path
}
$Path = $Path.Replace('\','/')
$Path = $Path.Replace(':','')
$Path
}
# Given a potentially Unix-style path, convert it to Windows-style.
function Get-LocalizedPath ([string]$Path) {
if ($Path -notmatch '^[A-Za-z]:') {
if ($Path.StartsWith('/')) {
$Path = $Path.TrimStart('/')
$Path = $Path -replace '^([A-Za-z])/', '$1:/'
} elseif (-not $Path.StartsWith('..')) {
$Path = './' + $Path
}
}
$Path = $Path.Replace('/','\')
$Path
}
# Helper to normalize a potentially relative path to absolute.
# If $Path is relative, it'll be resolved relative to $DirPath, else returned as-is.
# https://stackoverflow.com/questions/495618/how-to-normalize-a-path-in-powershell
function Get-AbsolutePath ([string]$Path, [string]$DirPath) {
Assert-IsDirectory $DirPath 'DirPath'
if (![System.IO.Path]::IsPathRooted($Path)) {
$Path = Join-Path ($DirPath) $Path
$Path = [System.IO.Path]::GetFullPath($Path)
}
$Path
}
# Generates {link, dest} pairs from placeholder files in library.
function Get-PlaceholderRawData ([string]$LibraryPath, [string]$PlaceholderExt) {
Get-PlaceholderPaths $LibraryPath $PlaceholderExt | ForEach-Object {
@{
# Assumes file w/ single line, no empty trailing ones
LinkPath = $_.TrimEnd($PlaceholderExt)
DestPath = Get-Content -Path $_
}
}
}
# Generates {link, dest} pairs from database text file in library.
function Get-DatabaseRawData ([string]$LibraryPath) {
$databasePath = Get-DatabasePath $LibraryPath
if (Test-Path $databasePath) {
Get-Content -Path $databasePath | Where-Object { $_ } | ForEach-Object {
$line = $_ -Split ' >>> ', 2
@{
LinkPath = $LibraryPath + '/' + $line[0]
DestPath = $line[1]
}
}
} else {
Write-Host 'No existing database file found for reference'
}
}
# Generates {link, dest} pairs from symlinks in library.
function Get-SymbolicLinkRawData ([string]$LibraryPath) {
Get-SymbolicLinkPaths $LibraryPath | ForEach-Object {
@{
LinkPath = $_
DestPath = Get-Item -Path $_ | Select-Object -ExpandProperty Target
}
}
}
# Convert any normalized (Unix) paths in raw data to Windows conventions.
function Get-LocalizedData ($Data) {
$Data | ForEach-Object {
@{
DestPath = Get-LocalizedPath $_.DestPath
LinkPath = Get-LocalizedPath $_.LinkPath
}
}
}
# Normalizes all symlink target paths in $Data to absolute.
function Get-AbsoluteData ($Data) {
$Data | ForEach-Object {
@{
DestPath = Get-AbsoluteDestPath $_.LinkPath $_.DestPath
LinkPath = $_.LinkPath
}
}
}
# Helper to de-duplicate records returned by Get-FoobarRawData functions.
# https://stackoverflow.com/questions/14332930/how-to-get-unique-value-from-an-array-of-hashtable-in-powershell
function Get-UniqueData ($HashArray) {
$HashArray | Select-Object @{
Expression = { "$($_.Keys):$($_.Values)" }
Label ='AsString'
}, @{
Expression ={$_}
Label = 'Hash'
} -Unique | Select-Object -ExpandProperty Hash
}
# Runs all Get-FoobarRawData functions, normalizes symlink targets to absolute, and returns de-duped results.
function Get-Data ([string]$LibraryPath, [string]$PlaceholderExt) {
$data = @()
$data += Get-SymbolicLinkRawData $LibraryPath
$data += Get-DatabaseRawData $LibraryPath
$data += Get-PlaceholderRawData $LibraryPath $PlaceholderExt
# Skip clean-up steps if there are no symlinks
if ($data.Count -gt 0) {
$data = Get-LocalizedData $data
$data = Get-AbsoluteData $data
$data = Get-UniqueData $data
}
# https://stackoverflow.com/questions/18476634/powershell-doesnt-return-an-empty-array-as-an-array
return ,$data
}
# Helper to return the directory within which the symlink should be located.
function Get-LinkParentPath ([string]$LinkPath) {
if (![System.IO.Path]::IsPathRooted($LinkPath)) {
Write-Host 'Get-LinkParentPath expects LinkPath to be absolute.'
exit 1
}
Split-Path -Path $LinkPath -Parent
}
# Normalize $DestPaths returned by Get-FoobarRawData functions to absolute.
function Get-AbsoluteDestPath ([string]$LinkPath, [string]$DestPath) {
Get-AbsolutePath $DestPath (Get-LinkParentPath $LinkPath)
}
# Normalize $DestPaths returned by Get-FoobarRawData functions to relative.
function Get-RelativeDestPath ([string]$LinkPath, [string]$DestPath) {
Get-RelativePath $DestPath (Get-LinkParentPath $LinkPath)
}
# Given a relative or absolute symlink target path, normalize it for how we want to save it.
function Get-BusinessDestPath ([string]$LinkPath, [string]$DestPath, [string]$LibraryPath) {
$DestPath = Get-AbsoluteDestPath $LinkPath $DestPath
# If the path falls below the library root, keep it absolute, else make it relative
# TODO: Make this a setting? Esp. how to treat paths on the same drive?
if ($DestPath.StartsWith($LibraryPath)) {
$DestPath = Get-RelativeDestPath $LinkPath $DestPath
}
$DestPath
}
# Expects absolute paths to a symlink, its target, and the Seafile library.
# Returns a `seafile-ignore.txt` line that will cause Seafile to ignore the symlink.
function Get-SymbolicLinkIgnorePath ([string]$LinkPath, [string]$DestPath, [string]$LibraryPath) {
# Determine the relative path from library root to the symlink for ignoring
$ignorePath = Get-RelativePath $LinkPath $LibraryPath
$ignorePath = Get-NormalizedPath $ignorePath
# If the $DestPath is relative, resolve it as such to the $LinkPath
$DestPath = Get-AbsoluteDestPath $LinkPath $DestPath
# If the symlink refers to a directory, treat it as such. The docs are wrong.
# https://www.seafile.com/en/help/ignore/
if (Test-IsDirectory $DestPath) {
$ignorePath = $ignorePath + '/'
}
$ignorePath
}
function New-SymbolicLink ([string]$LinkPath, [string]$DestPath, [string]$LibraryPath) {
# Ensure that the $DestPath fits our business logic
$DestPath = Get-BusinessDestPath $LinkPath $DestPath $LibraryPath
# We need to enter the folder where the symlink will be located for any relative paths to resolve
Push-Location -Path (Get-LinkParentPath $LinkPath)
# https://stackoverflow.com/questions/894430/creating-hard-and-soft-links-using-powershell
New-Item -Path $LinkPath -ItemType SymbolicLink -Value $DestPath -Force | Out-Null
# Restore our working directory
Pop-Location
Write-Host "Created symlink: `"$LinkPath`" >>> `"$DestPath`""
}
# Given a symlink path, get a path to the corresponding placeholder with extension
function Get-PlaceholderPath ([string]$LinkPath, [string]$PlaceholderExt) {
$dir = Get-LinkParentPath $LinkPath
$fname = (Split-Path -Path $LinkPath -Leaf) + $PlaceholderExt
"$dir\$fname"
}
# Create a symlink placeholder file.
function New-Placeholder ([string]$LinkPath, [string]$DestPath, [string]$PlaceholderExt, [string]$LibraryPath) {
# Ensure $DestPath follows our business logic and Unix conventions
$DestPath = Get-BusinessDestPath $LinkPath $DestPath $LibraryPath
$DestPath = Get-NormalizedPath $DestPath
$placeholderPath = Get-PlaceholderPath $LinkPath $PlaceholderExt
Write-Host "Creating placeholder: `"$placeholderPath`" >>> `"$DestPath`""
Write-IfChanged $placeholderPath $DestPath
}
# Expects both $LinkPath and $DestPath for splatting convenience, but only needs the former.
function Remove-Placeholder ([string]$LinkPath, [string]$DestPath, [string]$PlaceholderExt) {
$placeholderPath = Get-PlaceholderPath $LinkPath $PlaceholderExt
if (Test-Path $placeholderPath) {
Remove-Item -Path $placeholderPath
Write-Host "Removed placeholder: `"$placeholderPath`""
}
}
function Get-DatabasePath ([string]$LibraryPath) {
$LibraryPath + '\seafile-symlink.txt'
}
# Returns System.IO.FileSystemInfo of file at $Path, creating it if necessary
function Get-File ([string]$Path) {
if (Test-Path $Path) {
Write-Host 'Found:' $Path
Get-Item -Path $Path
} else {
Write-Host 'Created:' $Path
New-Item -Path $Path -Type 'file'
}
}
# Returns System.IO.FileSystemInfo of seafile-ignore.txt, creating it if necessary
function Get-SeafileIgnoreFile ([string]$LibraryPath) {
Get-File "$LibraryPath\seafile-ignore.txt"
}
# Used for padding output
function Add-TrailingNewline ([string[]]$Lines) {
if (($Lines.count -gt 0) -and (![string]::IsNullOrEmpty($Lines[-1]))) {
$Lines += ''
}
$Lines
}
# Write to file in $Path only if there are changes in content
function Write-IfChanged ([string]$Path, [string[]]$ContentNew) {
# Opinionated for our purpose - return early if there's nothing to write
if ($ContentNew.Length -lt 1) {
Write-Host 'Nothing to write:' $Path
return
}
# Get the file contents as string to preserve trailing newlines
if (Test-Path $Path) {
$ContentOld = Get-Content $Path -Raw
} else {
$ContentOld = ''
}
if ($ContentOld.Length -eq 0) {
Write-Host 'Appears empty:' $Path
}
# Add trailing newline to our new content
$ContentNew = Add-TrailingNewline $ContentNew
# Convert $ContentNew from [string[]] to [string]
[string]$ContentNew = $ContentNew -Join "`n"
if ($ContentNew -eq $ContentOld) {
Write-Host 'No changes required:' $Path
} else {
New-Item -Path $Path -Type 'file' -Value $ContentNew -Force | Out-Null
Write-Host 'Updated:' $Path
}
}
function Write-SeafileIgnoreFile ([string]$LibraryPath, [string[]]$PathsToIgnore ){
# This is the separator b/w your manually ignored items, and auto-ignored symlinks
$needle = '### SEAFILE-SYMLINK (AUTOGENERATED) ###'
# Split the ignore file into two parts based on our needle
$ignoreFile = Get-SeafileIgnoreFile $LibraryPath
$contentOld = Get-Content ($ignoreFile)
$contentNew = $contentOld.Where({ $_ -Like $needle }, 'Until')
# Putting this into a conditional will remove suffix header if there are no symlinks
if ($PathsToIgnore -and $PathsToIgnore.Length -gt 0) {
# Ensure that a newline precedes the suffix
$contentNew = Add-TrailingNewline $contentNew
# Append the suffix header
$contentNew += @($needle, '# Do not modify the line above or anything below it')
# Append the ignore paths to our suffix
$contentNew += $PathsToIgnore
} elseif ($contentOld.Count -lt 1) {
Remove-Item -Path $ignoreFile.FullName
Write-Host 'Removed seafile-ignore.txt because it would be empty'
}
Write-IfChanged "$LibraryPath\seafile-ignore.txt" $contentNew
}
function Write-DatabaseFile ($Data, [string]$LibraryPath) {
# Exit early if there are no symlinks to write
if ($Data.Count -lt 1) {
Write-Host 'No symlink data found'
Remove-DatabaseFile $LibraryPath
return
}
$contentNew = $Data | ForEach-Object {
# Link paths should be relative to library root, target paths follow our business logic
$linkPath = Get-RelativePath $_.LinkPath $LibraryPath
$destPath = Get-BusinessDestPath $_.LinkPath $_.DestPath $LibraryPath
# Both paths should be stored normalized to Unix conventions
$linkPath = Get-NormalizedPath ($linkPath)
$destPath = Get-NormalizedPath ($destPath)
$linkPath + ' >>> ' + $destPath
}
Write-IfChanged (Get-DatabasePath $LibraryPath) $contentNew
}
function Remove-DatabaseFile ([string]$LibraryPath) {
$databasePath = Get-DatabasePath $LibraryPath
if (Test-Path $databasePath) {
Remove-Item -Path $databasePath
Write-Host 'Removed database:' $databasePath
}
}
# Uses -Preset param from commandline, defaults to `default`
$Config = Get-Config $Preset
Write-Host 'Processing LibraryPath:' $Config['LibraryPath']
# Gather symlink records from placeholders, pseudo-database, and actual symlinks
$Data = Get-Data $Config['LibraryPath'] $Config['PlaceholderExt']
# For debug, try uncommenting this before it changes data:
# $Data | ForEach-Object { Write-Host @_ }; exit
# Persist symlink data for syncing using specified StorageMethod
Write-Host 'Using StorageMethod:' $Config['StorageMethod']
switch ($config['StorageMethod']) {
'placeholder' {
$Data | ForEach-Object { New-Placeholder @_ $Config['PlaceholderExt'] $Config['LibraryPath'] }
Remove-DatabaseFile $Config['LibraryPath']
}
'database' {
$Data | ForEach-Object { Remove-Placeholder @_ $Config['PlaceholderExt'] }
Write-DatabaseFile $Data $Config['LibraryPath']
}
}
# Gather symlink paths to ignore
$IgnorePaths = $Data | ForEach-Object {
Get-SymbolicLinkIgnorePath @_ $Config['LibraryPath']
}
# Write symlink paths to ignore file
Write-SeafileIgnoreFile $Config['LibraryPath'] $IgnorePaths
# Create actual symlinks from data
$Data | ForEach-Object { New-SymbolicLink @_ $Config['LibraryPath'] }