如何在 Windows/Mac/Linux 中创建隐藏文件?

How to create a hidden file in Windows/Mac/Linux?

我构建了一个控制台应用程序,需要创建一些隐藏文件。 我也知道以点开头的文件名将隐藏在 Linux 和 mac 中,但是 windows?

设置文件属性?

有没有办法在 Windows / Linux / Mac 中创建隐藏文件和目录?

Windows:

SetFileAttributesW function

Sets the attributes for a file or directory.

FILE_ATTRIBUTE_HIDDEN 2 (0x2)

The file or directory is hidden. It is not included in an ordinary directory listing.


开始:

Package syscall

func SetFileAttributes

func SetFileAttributes(name *uint16, attrs uint32) (err error)

从 Go UTF-8 编码字符串 (string) 转换为 Windows UTF-16 编码字符串指针 (*uint16)。

Package syscall

func UTF16PtrFromString

func UTF16PtrFromString(s string) (*uint16, error)

UTF16PtrFromString returns pointer to the UTF-16 encoding of the UTF-8 string s, with a terminating NUL added. If s contains a NUL byte at any location, it returns (nil, EINVAL).


使用OSBuild Contraints.


例如,

hide/attrib.go:

package main

import (
    "fmt"
    "io/ioutil"
    "os"
)

func main() {
    filename := `test.hidden.file`
    os.Remove(filename)
    os.Remove("." + filename)
    err := ioutil.WriteFile(filename, []byte(filename), 0666)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
    err = HideFile(filename)
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        return
    }
    fmt.Println("hidden:", filename)
}

hide/hide.go:

// +build !windows

package main

import (
    "os"
    "path/filepath"
    "strings"
)

func HideFile(filename string) error {
    if !strings.HasPrefix(filepath.Base(filename), ".") {
        err := os.Rename(filename, "."+filename)
        if err != nil {
            return err
        }
    }
    return nil
}

hide/hide_windows.go:

// +build windows

package main

import (
    "syscall"
)

func HideFile(filename string) error {
    filenameW, err := syscall.UTF16PtrFromString(filename)
    if err != nil {
        return err
    }
    err = syscall.SetFileAttributes(filenameW, syscall.FILE_ATTRIBUTE_HIDDEN)
    if err != nil {
        return err
    }
    return nil
}

输出(Linux):

$ tree hide
hide
├── attrib.go
├── hide.go
└── hide_windows.go
$

$ go build && ./hide
hidden: test.hidden.file
$ ls -a .test.hidden.file
.test.hidden.file
$ 

输出(Windows):

>go build && hide
hidden: test.hidden.file
>attrib test.hidden.file
A   H        \test.hidden.file
>
import (
    _ "golang.org/x/sys/windows"
    "os"
    "runtime"
    "syscall"
)

func HideFile(filename string) (status bool, err error) {
    if runtime.GOOS == "windows" {
        filenameW, err := syscall.UTF16PtrFromString(filename)
        if err != nil {
            return false, err
        }

        err = syscall.SetFileAttributes(filenameW, syscall.FILE_ATTRIBUTE_HIDDEN)
        if err != nil {
            return false, err
        }
    } else {
        if filename[0:1] != "." {
            err = os.Rename(filename, "." + filename)
            if err != nil {
                return false, err
            }
        }
    }

    return true, nil
}

这是我的代码。但是无法在Linux中构建。

错误:

src/util/hidden.go:12:21: undefined: syscall.UTF16PtrFromString
src/util/hidden.go:17:9: undefined: syscall.SetFileAttributes
src/util/hidden.go:17:46: undefined: syscall.FILE_ATTRIBUTE_HIDDEN

我为此制作了一个跨平台模块(可在此处获得:higgs)。 您只需调用 HideUnhide 函数即可隐藏或取消隐藏文件或目录。

示例代码:

package main

import (
    "fmt"
    "github.com/dastoori/higgs"
)

func main() {
    err := higgs.Hide("foo.txt")
    
    if err != nil {
        fmt.Println(err)
    }
}

制作这样的文件:

//go:generate mkwinsyscall -output zhide.go hide.go
//sys setFileAttributes(name string, attr int) (err error) = kernel32.SetFileAttributesW
package main

const (
   file_attribute_hidden = 2
   file_attribute_normal = 128
)

func main() {
   setFileAttributes("file.txt", file_attribute_hidden)
}

然后构建:

go mod init hide
go mod tidy
go generate
go build