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.
goHidden
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)
}
It is licensed under MIT, for anyone who needs it, feel free to use it in your projects :)
Top comments (5)
This does not hide the file but just add the
.
in the prefix.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".
In linux build
You are checking via
It will always end up panic because this file will be renamed to
./.test
after hiding it and the aboveisHidden
will never find the file.For windows it should be working fine with the example in your package.
Ok so there is another package for the windows. There is a small problem in your
isHidden
method and the example.Thanks for pointing it out! I will try to fix it soon :D