-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(windows): Fix "Access is defined." error on Windows when renaming.
- Loading branch information
Showing
5 changed files
with
93 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// +build !windows | ||
|
||
package base | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
// SetFileHidden ensures the filename begins with a dot. Other OSes may do more | ||
func SetFileHidden(path string) error { | ||
basename := filepath.Base(path) | ||
if !strings.HasPrefix(basename, ".") { | ||
return fmt.Errorf("hidden files must begin with \".\"") | ||
} | ||
return nil | ||
} | ||
|
||
// WriteHiddenFile ensures the filename begins with a dot, and writes content to it. Other | ||
// Operating Systems may do more | ||
func WriteHiddenFile(path, content string) error { | ||
// Ensure the filename begins with a dot | ||
basename := filepath.Base(path) | ||
if !strings.HasPrefix(basename, ".") { | ||
return fmt.Errorf("hidden files must begin with \".\"") | ||
} | ||
// Write the contents | ||
if err := ioutil.WriteFile(path, []byte(content), 0644); err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
"path/filepath" | ||
"strings" | ||
"syscall" | ||
) | ||
|
||
// SetFileHidden sets the hidden attribute on the given file. Windows specific functionality | ||
func SetFileHidden(path string) error { | ||
basename := filepath.Base(path) | ||
if !strings.HasPrefix(basename, ".") { | ||
return fmt.Errorf("hidden files must begin with \".\"") | ||
} | ||
filenamePtr, err := syscall.UTF16PtrFromString(path) | ||
if err != nil { | ||
return err | ||
} | ||
return syscall.SetFileAttributes(filenamePtr, syscall.FILE_ATTRIBUTE_HIDDEN) | ||
} | ||
|
||
// WriteHiddenFile writes content to the file and sets the hidden attribute on it. For a hidden | ||
// file, using ioutil.WriteFile instead will result in "Access is denied." so use this instead. | ||
// Windows specific functionality | ||
func WriteHiddenFile(path, content string) error { | ||
// Ensure the filename begins with a dot | ||
basename := filepath.Base(path) | ||
if !strings.HasPrefix(basename, ".") { | ||
return fmt.Errorf("hidden files must begin with \".\"") | ||
} | ||
|
||
// Create the file to write, making sure to set the hidden file attribute | ||
filenamePtr, err := syscall.UTF16PtrFromString(path) | ||
if err != nil { | ||
return err | ||
} | ||
h, err := syscall.CreateFile( | ||
filenamePtr, // filename | ||
syscall.GENERIC_WRITE, // access | ||
uint32(syscall.FILE_SHARE_READ | syscall.FILE_SHARE_WRITE), // share mode | ||
nil, // security attributes | ||
syscall.CREATE_ALWAYS, // creation disposition | ||
syscall.FILE_ATTRIBUTE_HIDDEN, // flags and attributes | ||
0, // template file handle | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
defer syscall.Close(h) | ||
|
||
// Write contents | ||
_, err = syscall.Write(h, []byte(content)) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters