Skip to content

Commit 7689119

Browse files
committed
Minor fix for setting permissions on directories
1 parent 03544c6 commit 7689119

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

ES.SFTP.Host/Orchestrator.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -376,15 +376,26 @@ private async Task PrepareUserForSftp(string username)
376376

377377
var directoryInfo = new DirectoryInfo(dirPath);
378378

379-
var firstParentInChroot = directoryInfo.Parent ?? chrootDirectory;
380-
while ((firstParentInChroot.Parent ??
381-
throw new InvalidOperationException("Cannot find first parent in chroot")).FullName !=
382-
chrootDirectory.FullName)
379+
try
383380
{
384-
firstParentInChroot = firstParentInChroot.Parent;
381+
var firstParentInChroot = directoryInfo;
382+
while ((firstParentInChroot.Parent ??
383+
throw new InvalidOperationException("Cannot find first parent in chroot")).FullName !=
384+
chrootDirectory.FullName)
385+
{
386+
firstParentInChroot = firstParentInChroot.Parent;
387+
}
388+
await ProcessUtil.QuickRun("chown", $"-R {username}:{SftpUserInventoryGroup} {firstParentInChroot.FullName}");
389+
}
390+
catch(Exception exception)
391+
{
392+
_logger.LogWarning(exception,
393+
"Could not determine first parent of '{dir}' in chroot '{chroot}' or failed to set permissions",
394+
directoryInfo.FullName, chrootDirectory.FullName);
395+
396+
await ProcessUtil.QuickRun("chown", $"-R {username}:{SftpUserInventoryGroup} {directoryInfo.FullName}");
385397
}
386398

387-
await ProcessUtil.QuickRun("chown", $"-R {username}:{SftpUserInventoryGroup} {firstParentInChroot.FullName}");
388399
}
389400

390401
var sshDir = Path.Combine(homeDirPath, ".ssh");

0 commit comments

Comments
 (0)