Skip to content
Snippets Groups Projects
Commit d40bcaf8 authored by Andrew E. Torda's avatar Andrew E. Torda
Browse files

Very verbose error message, but still cannot resolve the problem.

parent 5fbcc2ec
No related branches found
No related tags found
No related merge requests found
...@@ -11,6 +11,7 @@ import ( ...@@ -11,6 +11,7 @@ import (
"math" "math"
"math/rand" "math/rand"
"os" "os"
"path/filepath"
"sync" "sync"
"example.com/ackley_mc/ackley" "example.com/ackley_mc/ackley"
...@@ -68,7 +69,7 @@ func isNotSane(mcPrm *McPrm) error { ...@@ -68,7 +69,7 @@ func isNotSane(mcPrm *McPrm) error {
// It is part of the exercise, working around file permissions on windows // It is part of the exercise, working around file permissions on windows
func fExists(name string) bool { func fExists(name string) bool {
var err error var err error
if _, err := os.Stat (name); err == nil { if _, err = os.Stat(name); err == nil {
return true return true
} }
if errors.Is(err, fs.ErrNotExist) { if errors.Is(err, fs.ErrNotExist) {
...@@ -77,26 +78,34 @@ func fExists (name string) bool { ...@@ -77,26 +78,34 @@ func fExists (name string) bool {
return true return true
} }
// createFix is a wrapper around os.Create() which I seem to need for windows. // createFix is a wrapper around os.Create() which I seem to need for windows.
// On windows, I cannot overwrite an existing file. To be very exact, the // On windows, I cannot overwrite an existing file. To be very exact, the
// O_CREATE fails if a file exists. O_TRUNC also fails. // O_CREATE fails if a file exists. O_TRUNC also fails.
// Doing an open on the file gives the same, "The system cannot find the file specified"
// The calls fail, even if the file does not exist. It must be a problem
// with permissions on the directory, not the file.
func createFix(name string) (*os.File, error) { func createFix(name string) (*os.File, error) {
var rErr = `Remove the file %s and try again. This only happens on windows: %w` longErr := `%s does not exist, but could not create it.
Tried setting pemissions on directory %s, but failed:
%w`
var f *os.File var f *os.File
var err error var err error
if f, err = os.Create(name); err == nil { if f, err = os.Create(name); err == nil {
return f, nil // Most common path (no problem) return f, nil // Most common path (no problem)
} }
if !fExists(name) { // does not exist, but we have a problem.
var t string
if t, err = filepath.Abs(name); err != nil {
return nil, fmt.Errorf("cannot work out path %w", err)
}
d := filepath.Dir(t)
e := os.Chmod(d, 0200)
if e != nil {
return nil, fmt.Errorf(longErr, name, d, e)
}
if !fExists(name) { // does not exist, but we have a real error. } else { // the file exists
return nil, err // Return original error fmt.Println("file exists, what to do ?")
} // Next step try to remove original file
if e := os.Remove (name); e != nil {
return nil, fmt.Errorf (rErr, name, e)
}
if f, err = os.Create(name); err == nil { // second attempt, after removing name
return f, nil
} }
return nil, err return nil, err
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment