Debian Codegraph @

storage/filesystem/dotgit/writers_windows.go

raw
1 //go:build windows
2
3 package dotgit
4
5 import (
6 "fmt"
7 "path/filepath"
8
9 "github.com/go-git/go-billy/v5"
10 "github.com/go-git/go-git/v5/utils/trace"
11 "golang.org/x/sys/windows"
12 )
13
14 func fixPermissions(fs billy.Filesystem, path string) {
15 fullpath := filepath.Join(fs.Root(), path)
16 p, err := windows.UTF16PtrFromString(fullpath)
17 if err != nil {
18 trace.General.Printf("failed to chmod %s: %v", fullpath, err)
19 return
20 }
21
22 attrs, err := windows.GetFileAttributes(p)
23 if err != nil {
24 trace.General.Printf("failed to chmod %s: %v", fullpath, err)
25 return
26 }
27
28 if attrs&windows.FILE_ATTRIBUTE_READONLY != 0 {
29 return
30 }
31
32 err = windows.SetFileAttributes(p,
33 attrs|windows.FILE_ATTRIBUTE_READONLY,
34 )
35
36 if err != nil {
37 trace.General.Printf("failed to chmod %s: %v", fullpath, err)
38 }
39 }
40
41 func isReadOnly(fs billy.Filesystem, path string) (bool, error) {
42 fullpath := filepath.Join(fs.Root(), path)
43 p, err := windows.UTF16PtrFromString(fullpath)
44 if err != nil {
45 return false, fmt.Errorf("%w: %q", err, fullpath)
46 }
47
48 attrs, err := windows.GetFileAttributes(p)
49 if err != nil {
50 return false, fmt.Errorf("%w: %q", err, fullpath)
51 }
52
53 if attrs&windows.FILE_ATTRIBUTE_READONLY != 0 {
54 return true, nil
55 }
56
57 return false, nil
58 }