DEV Community

Toby Chui
Toby Chui

Posted on

Hide a file / folder using Golang

Recently, one of my clients is requesting me to develop a web system that runs on Windows (instead of Linux). What they thought is that they don't need to hire an IT guy to do the daily maintenance and they can fix / update the system even without much technical knowledge in the field.

As I am a "nice" developer, I say "sure", but the next things I am thinking is: How do I hide all the files that should not be touched securely? After some Googling, I decided to hide all the system generated files using Windows API. I also want to keep the Linux compatibility of the system (just in case they hire an actual IT guy later and decided to migrate to Linux), so I made this simple Go Module to get the job done.

GitHub logo tobychui / goHidden

Go module to set a hidden folder and check if a folder is hidden

goHidden

Go Reference

A Go module to set a folder hidden and check if a folder is hidden

Support Windows and Linux, should also work on MacOS

Usage

//Hide a folder
err := hidden.HideFile("./test")
if err != nil {
    panic(err)
}

//Check if a folder is hidden
isHidden, err := hidden.IsHidden("./test", false)
if err != nil {
    panic(err)
}

You can enable parent directories checking on IsHidden by passing "true" as the last parameter for IsHidden. This will allowIsHidden to check all the upstream folders to see if the file is located inside a hidden folder. Set this to false for checking only the targeted file / folder.

hidden.HideFile("./test")
isHidden, _ := hidden.IsHidden("./test/myfile.txt", true)
//isHidden is true

License

MIT




Here is a basic example usage of the module.

//Hide a folder
err := hidden.HideFile("./test")
if err != nil {
    panic(err)
}

//Check if a folder is hidden
isHidden, err := hidden.IsHidden("./test", false)
if err != nil {
    panic(err)
}
Enter fullscreen mode Exit fullscreen mode

It is licensed under MIT, for anyone who needs it, feel free to use it in your projects :)

Top comments (5)

Collapse
 
hamzaanis profile image
Hamza Anis

This does not hide the file but just add the . in the prefix.

Collapse
 
tobychui profile image
Toby Chui

Add "." in the prefix is how you hide a file on Linux. On Windows, it will use syscall SetFileAttributes to set a folder to "hidden".

Collapse
 
hamzaanis profile image
Hamza Anis • Edited

In linux build

func isHidden(filename string) (bool, error) {
    if len(filepath.Base(filename)) > 0 && filepath.Base(filename)[0:1] == "." {
        return true, nil
    }

    return false, nil
}
Enter fullscreen mode Exit fullscreen mode

You are checking via

isHidden, err := hidden.IsHidden("./test", false)
Enter fullscreen mode Exit fullscreen mode

It will always end up panic because this file will be renamed to ./.test after hiding it and the above isHidden will never find the file.

For windows it should be working fine with the example in your package.

Collapse
 
hamzaanis profile image
Hamza Anis

Ok so there is another package for the windows. There is a small problem in your isHidden method and the example.

Thread Thread
 
tobychui profile image
Toby Chui

Thanks for pointing it out! I will try to fix it soon :D