Hone logo
Hone
Problems

File System Information Explorer in Go

This challenge focuses on leveraging Go's os package to interact with the file system. You will build a program that can retrieve and display various pieces of information about files and directories, demonstrating essential file system operations. This is a fundamental skill for any Go developer working with system-level tasks.

Problem Description

Your task is to create a Go program that allows a user to specify a file or directory path and then displays relevant information about it. The program should be able to handle common file system operations using the os package.

Key Requirements:

  1. Input: The program should accept a single command-line argument representing the path to a file or directory.
  2. Information Display: For the given path, the program should display:
    • Whether the path exists.
    • If it exists, whether it's a file or a directory.
    • The file's or directory's size (in bytes). For directories, this is typically the size of the directory entry itself, not the sum of its contents.
    • The last modification time.
    • The file mode (permissions).
  3. Error Handling: Gracefully handle cases where the provided path does not exist or other file system errors occur.
  4. Output Format: Present the information in a clear and readable format.

Expected Behavior:

If a valid path is provided, the program should print details. If the path does not exist, it should print a specific message indicating that.

Edge Cases:

  • No command-line argument provided.
  • Path points to a symbolic link (you don't need to resolve it, just report its properties).
  • Permissions issues preventing access to file information (though for this challenge, assume sufficient permissions for the current user).

Examples

Example 1:

Input:
go run main.go /path/to/a/file.txt

Output:
Path: /path/to/a/file.txt
Exists: true
Type: File
Size: 1024 bytes
Last Modified: 2023-10-27 10:30:00 +0000 UTC
Mode: -rw-r--r--

Explanation: The program successfully identified '/path/to/a/file.txt' as an existing file and displayed its size, modification time, and permissions.

Example 2:

Input:
go run main.go /path/to/a/directory

Output:
Path: /path/to/a/directory
Exists: true
Type: Directory
Size: 4096 bytes
Last Modified: 2023-10-26 15:00:00 +0000 UTC
Mode: drwxr-xr-x

Explanation: The program identified '/path/to/a/directory' as an existing directory and provided its details.

Example 3:

Input:
go run main.go /this/path/does/not/exist

Output:
Path: /this/path/does/not/exist
Exists: false
Error: stat /this/path/does/not/exist: no such file or directory

Explanation: The program correctly identified that the provided path does not exist and reported the relevant error.

Example 4:

Input:
go run main.go

Output:
Usage: go run main.go <file_or_directory_path>

Explanation: The program detected that no command-line argument was provided and printed a usage message.

Constraints

  • The program must be written in Go.
  • It must use the standard os package for file system operations.
  • The program should not rely on external libraries for file system interaction.
  • The response time for a valid file or directory should be negligible (within typical system call latency).

Notes

  • Consider using os.Stat() to get file information.
  • The os.FileInfo interface returned by os.Stat() contains methods like Name(), Size(), Mode(), ModTime(), and IsDir().
  • Remember to handle the error returned by os.Stat().
  • You'll need to parse command-line arguments, which can be done using the os.Args slice.
  • Formatting the ModTime can be done using its Format() method.
  • The Mode() method returns an os.FileMode, which can be interpreted to show permissions. You can use mode.String() for a human-readable representation.
Loading editor...
go