FileInfo Example
Demonstration of Basic Operations of FileInfo
Code:
import std.fs.*
import std.time.DateTime
main() {
// Creates a temporary file in the current directory for the following demonstration of FileInfo.
let curDirPath: Path = Path("./").toCanonical()
let file: File = File.createTemp(curDirPath)
file.write("123456789\n".toArray())
let fileInfo: FileInfo = file.info
file.close()
/* Obtains FileInfo of the parent directory of the file. The parent directory of the file is the current directory. */
let parentDirectory: Option<FileInfo> = fileInfo.parentDirectory
checkResult(parentDirectory == Some(FileInfo(curDirPath)), "The 'parentFileInfo' is obtained successfully.")
/* Obtains the path of the file. */
/*
let filePath: Path = fileInfo.path
*/
/* Obtains the path of the link file if the file is a soft link. Since this file is not a soft link, Option<Path>.None is obtained. */
let symbolicLinkTarget: Option<Path> = fileInfo.symbolicLinkTarget
checkResult(symbolicLinkTarget == None, "It's not a symbolic link, there's no `symbolicLinkTarget`.")
/* Obtains the creation time, last access time, and last modification time of the file. */
/*
let creationTime: DateTime = fileInfo.creationTime
let lastAccessTime: DateTime = fileInfo.lastAccessTime
let lastModificationTime: DateTime = fileInfo.lastModificationTime
*/
/*
* Obtains the length of the file.
* For a file, obtains the disk space occupied by the file.
* For a directory, obtains the disk space occupied by all files in the directory (excluding subdirectories).
*/
/*
let length: Int64 = fileInfo.length
*/
/* Checks whether the file is a soft link, common file, or directory. */
checkResult(fileInfo.isSymbolicLink(), "The file is a symbolic link.")
checkResult(fileInfo.isFile(), "The file is a common file.")
checkResult(fileInfo.isDirectory(), "The file is a directory.")
/* Checks whether the file is read-only, hidden, executable, readable, or writable by the current user. */
checkResult(fileInfo.isReadOnly(), "This file is read-only.")
checkResult(fileInfo.isHidden(), "The file is hidden.")
checkResult(fileInfo.canExecute(), "The file is executable.")
checkResult(fileInfo.canRead(), "The file is readable.")
checkResult(fileInfo.canWrite(), "The file is writable.")
/* Modifies the permission of the current user on the file. In this example, the read-only permission on the file is granted. */
checkResult(fileInfo.setExecutable(false), "The file was successfully set to executable.")
checkResult(fileInfo.setReadable(true), "The file was successfully set to readable.")
checkResult(fileInfo.setWritable(false), "The file was successfully set to writable.")
checkResult(fileInfo.isReadOnly(), "This file is now read-only.")
return 0
}
func checkResult(result: Bool, message: String): Unit {
if (result) {
println(message)
}
}
Running result:
The 'parentFileInfo' is obtained successfully.
It's not a symbolic link, there's no `symbolicLinkTarget`.
The file is a common file.
The file is readable.
The file is writable.
The file was successfully set to executable.
The file was successfully set to readable.
The file was successfully set to writable.
This file is now read-only.