Skip to content

sync dir Access is denied on windows #75841

@ningmingxiao

Description

@ningmingxiao

Go version

any version

Output of go env in your module/workspace:

default

What did you do?

package main

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

func syncDir(dir string) error {
	dirF, err := os.Open(dir)
	if err != nil {
		return fmt.Errorf("failed to open dir %s: %w", dir, err)
	}
	err = dirF.Sync()
	dirF.Close()
	if err != nil {
		return fmt.Errorf("failed to sync dir %s: %w", dir, err)
	}
	return nil
}

func main() {
	targetDir := "C:\\data\\logs"
	targetFile := filepath.Join(targetDir, "output.txt")

	if err := os.MkdirAll(targetDir, 0755); err != nil {
		panic(err)
	}

	content := "This is a test string"
	if err := ioutil.WriteFile(targetFile, []byte(content), 0644); err != nil {
		panic(err)
	}
	if err := syncDir(filepath.Dir(targetDir)); err != nil {
		fmt.Println(err)
	}

}

will show error Access is denied.

What did you see happen?

will show error Access is denied.

What did you expect to see?

run successful.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugReportIssues describing a possible bug in the Go implementation.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions