From e452967d664e8c360a56a812691c9789f0503686 Mon Sep 17 00:00:00 2001 From: "Andrew E. Torda" <torda@zbh.uni-hamburg.de> Date: Fri, 3 Jun 2022 11:18:16 +0200 Subject: [PATCH] Simplify file creation. Remove all the funny cases for windows and just print out a long error message. --- README.md | 5 +++-- mc_work/dorun.go | 50 +++++++++++++------------------------------ mc_work/unwanted.goat | 14 ++++++++++++ 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/README.md b/README.md index ead73c1..feb57fe 100644 --- a/README.md +++ b/README.md @@ -40,11 +40,12 @@ This is in csv format. For gnuplot, do not forget to `set datafile separator ',' # BUGS ## Real Bugs -Only under windows: I use os.Create() to open a file for writing. In my "Documents" folder, the function call returns "The system cannot find the file specified". Note, this is when creating a file, not opening an existing one. I do not have any error when I run the program from a different directory. I did experiment with chmod() on the directory, but this (a) did not do anything and (b) only the bits set by 0200 are actually affected according to go's documentation. +### Only under windows +I use `os.Create()` to open a file for writing (plots, `.csv` files. In my "Documents" folder, the function call returns "The system cannot find the file specified". On my test machine, this was a result of trying to write to a protected folder with an application (ackley_mc) that windows did not know about. If you fight your way through the windows menus, you will find a place that shows "blocked software". You could add `ackley_mc.exe` to the list of blessed executables, but why not just run the code from another directory? ## Non bugs -The UI toolkit is "fyne". It does not do charts, so we use go-chart which makes a png, which we can store in memory and feed to the fyne toolkit. Unfortunately, an svg file from go-chart causes fyne to choke. This means we are stuck with png. One day, one should try out the charting library which is based on fyne. +The UI toolkit is "fyne". It does not do charts, so we use go-chart which makes a png, which we can store in memory and feed to the fyne toolkit. We can ask go-chart to make an svg file, but this causes fyne to choke. This means we are stuck with png. One day, one should try out the charting library which is based on fyne. The graphical interface follows no known style convention and looks like it was designed by a three-year old. diff --git a/mc_work/dorun.go b/mc_work/dorun.go index cd600b6..fe78a58 100644 --- a/mc_work/dorun.go +++ b/mc_work/dorun.go @@ -4,10 +4,8 @@ package mcwork import ( "bufio" "bytes" - "errors" "fmt" "io" - "io/fs" "math" "math/rand" "os" @@ -65,45 +63,27 @@ func isNotSane(mcPrm *McPrm) error { return nil } -// fExists returns true if the file exists. -// It is part of the exercise, working around file permissions on windows -func fExists(name string) bool { - var err error - if _, err = os.Stat(name); err == nil { - return true - } - if errors.Is(err, fs.ErrNotExist) { - return false - } - return true -} - -// createFix is a wrapper around os.Create() which I seem to need for windows. -// On windows, this fails if I am in my "Documents" directory. +// createFix is a wrapper around os.Create() with a long error message. +// On windows, this fails if I am in my "Documents" directory due to +// its ransomware protection. func createFix(name string) (*os.File, error) { - longErr := `%s does not exist, but could not create it: + emsg := `Could not create "%s". The error was %w -Please run the program from somewhere other than %s` - existErr := `File exists, but unable to write to it: -%w` +This is probably a permission error. Specify a directory other than +%s +and try again. It might be your protected folder settings.` + var t string + var err, e2 error var f *os.File - var err error if f, err = os.Create(name); err == nil { return f, nil // Most common path (no problem) } - if !fExists(name) { // does not exist, but we have a problem. - var d string - if t, e := filepath.Abs(name); e != nil { - return nil, fmt.Errorf("cannot work out path %w", e) - } else { - d = filepath.Dir(t) - } - if _, e := os.Stat(d); e != nil { - return nil, fmt.Errorf("parent directory %s is broken %w", d, err) - } - return nil, fmt.Errorf(longErr, name, err, d) + + if t, e2 = filepath.Abs(name); e2 != nil { + return nil, fmt.Errorf("The path for %s is broken: \"%w\"", name, e2) } - return nil, fmt.Errorf(existErr, err) // The file exists.. + + return nil, fmt.Errorf(emsg, name, err, filepath.Dir(t)) } // setupRun does things like get the cooling rate, seed the random numbers, @@ -140,7 +120,7 @@ func setupRun(mcPrm *McPrm, cprm *cprm) error { return fmt.Errorf("broke setting plot filename: %w", err) } if cprm.fplotWrt, err = os.Create(mcPrm.fPltName); err != nil { - return fmt.Errorf ("Trying to create file: %w", err) + return fmt.Errorf("Trying to create file: %w", err) } } else { // Must be writing to a screen var w bytes.Buffer // could be fancier. Preallocate and use bytes.Newbuffer diff --git a/mc_work/unwanted.goat b/mc_work/unwanted.goat index 707f4fc..094f87f 100644 --- a/mc_work/unwanted.goat +++ b/mc_work/unwanted.goat @@ -7,3 +7,17 @@ func alt_newx(xold []float32, xT []float32, rand *rand.Rand, xDlta float32) { xT[i] = x + t } } + + +// fExists returns true if the file exists. +// It is part of the exercise, working around file permissions on windows +func fExists(name string) bool { + var err error + if _, err = os.Stat(name); err == nil { + return true + } + if errors.Is(err, fs.ErrNotExist) { + return false + } + return true +} -- GitLab