Function rename [src]

Change the name or location of a file or directory. If new_sub_path already exists, it will be replaced. Renaming a file over an existing directory or a directory over an existing file will fail with error.IsDir or error.NotDir On Windows, both paths should be encoded as WTF-8. On WASI, both paths should be encoded as valid UTF-8. On other platforms, both paths are an opaque sequence of bytes with no particular encoding.

Prototype

pub fn rename(self: Dir, old_sub_path: []const u8, new_sub_path: []const u8) RenameError!void

Parameters

self: Dirold_sub_path: []const u8new_sub_path: []const u8

Possible Errors

AccessDenied RenameError

In WASI, this error may occur when the file descriptor does not hold the required rights to rename a resource by path relative to it.

On Windows, this error may be returned instead of PathAlreadyExists when renaming a directory over an existing directory.

AntivirusInterference RenameError

On Windows, antivirus software is enabled by default. It can be disabled, but Windows Update sometimes ignores the user's preference and re-enables it. When enabled, antivirus software on Windows intercepts file system operations and makes them significantly slower in addition to possibly failing with this error code.

BadPathName RenameError
DiskQuota RenameError
FileBusy RenameError
FileNotFound RenameError
InvalidUtf8 RenameError

WASI-only; file paths must be valid UTF-8.

InvalidWtf8 RenameError

Windows-only; file paths provided by the user must be valid WTF-8. https://simonsapin.github.io/wtf-8/

IsDir RenameError
LinkQuotaExceeded RenameError
NameTooLong RenameError
NetworkNotFound RenameError

On Windows, \\server or \\server\share was not found.

NoDevice RenameError
NoSpaceLeft RenameError
NotDir RenameError
PathAlreadyExists RenameError
PermissionDenied RenameError
PipeBusy RenameError
ReadOnlyFileSystem RenameError
RenameAcrossMountPoints RenameError
SharingViolation RenameError
SymLinkLoop RenameError
SystemResources RenameError
Unexpected UnexpectedError

The Operating System returned an undocumented error code.

This error is in theory not possible, but it would be better to handle this error than to invoke undefined behavior.

When this error code is observed, it usually means the Zig Standard Library needs a small patch to add the error code to the error set for the respective function.

Source

pub fn rename(self: Dir, old_sub_path: []const u8, new_sub_path: []const u8) RenameError!void { return posix.renameat(self.fd, old_sub_path, self.fd, new_sub_path); }