Skip to content

FileManager.copyItem() throws a permission error after having copied files (Ubuntu) #1125

Open
@sebsto

Description

@sebsto

When copying an entire directory where files are owned by root, FileManager.copyItem() throws a CocoaError.fileWriteNoPermission after having copied the files.

It works on macOS and it is reproductible on Ubuntu.

Version : Swift 6.0.3 on Ubuntu 24.04

Minimal code to reproduce

import FoundationEssentials

do {
    try FileManager.default.copyItem(
        atPath: "src",
        toPath: "dst"
    )
    print("Files copied successfully.")
} catch let error as CocoaError{
    print("Cocoa Error copying files: \(error)")
} catch {
    print("Other error copying files: \(error)")
}

and this shell script

#!/bin/bash 

sudo mkdir src 
sudo touch src/toto 
ls -al src
swift main.swift
ls -al dst

This produces

total 8
drwxr-xr-x 2 root   root   4096 Jan 20 07:24 .
drwxrwxr-x 3 ubuntu ubuntu 4096 Jan 20 07:24 ..
-rw-r--r-- 1 root   root      0 Jan 20 07:24 toto

Cocoa Error copying files: CocoaError(code: FoundationEssentials.CocoaError.Code(rawValue: 513), userInfo: ["NSURL": AnyHashable(dst -- file:///home/ubuntu/swift-aws-lambda-runtime/_REPRO/), "NSFilePath": AnyHashable("dst"), "NSUnderlyingError": AnyHashable(FoundationEssentials.POSIXError(code: Glibc.POSIXErrorCode.EPERM))])

total 8
drwxr-xr-x 2 ubuntu ubuntu 4096 Jan 20 07:24 .
drwxrwxr-x 4 ubuntu ubuntu 4096 Jan 20 07:24 ..
-rw-r--r-- 1 ubuntu ubuntu    0 Jan 20 07:24 toto

Note that the source files have perm 0o644, they are readable by non-root users. Copying them must succeed and the target files must be owned by the user running the swift script. This is the case here, but FileManager.copyItem() throws an error after the copy operation.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions