diff --git a/.gitignore b/.gitignore index 48b2713249dde83761d7b60ba4ac281315ab9182..d1171387fedae356fa074f9e8219dcb9785641b9 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ test_tmp* -ackley ackley_mc +ackley_mc.exe *.csv /ackley_mc.exe diff --git a/Makefile b/Makefile index 71a42562b3f2a922f1924d3049d0fa10591399ed..893dad27a2d90b5e8d39ee90ab0f10a5441943ad 100644 --- a/Makefile +++ b/Makefile @@ -34,6 +34,7 @@ clean: $(GO) clean rm -rf bin/* rm -rf examples/*.csv + rm -rf *.csv rm -rf */*_delme.* rm -rf */test_tmp* rm -rf /tmp/go-build[0-9]* diff --git a/README.md b/README.md index f140a7afa6602b6beeeaa992a4cfa1187f06aa64..c987a608df3b5a934314ff50203daf050eff6e8b 100644 --- a/README.md +++ b/README.md @@ -18,11 +18,11 @@ I do not have a mac for testing, so this is limited to linux and windows. It is cd ackley_mc go build . -and you should end up with an executable called `ackley_mc` or `ackley_mc.exe` under windows. To build without graphics: +and you should end up with an executable called `ackley_mc` or `ackley_mc.exe` under windows. To build without graphics, git clone git@gitlab.rrz.uni-hamburg.de:Bae5157/ackley_mc.git cd ackley_mc - go build -tags=no_gfx . + go build . ### Graphics yes / no ? diff --git a/mc_work/dorun.go b/mc_work/dorun.go index 9a408987a20c25ce004adf0c3fb9abe86150e1cb..86b775d74f5bd3b88f4862208225f2e57b5871e2 100644 --- a/mc_work/dorun.go +++ b/mc_work/dorun.go @@ -4,8 +4,10 @@ package mcwork import ( "bufio" "bytes" + "errors" "fmt" "io" + "io/fs" "math" "math/rand" "os" @@ -62,6 +64,43 @@ 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, I cannot overwrite an existing file. To be very exact, the +// O_CREATE fails if a file exists. O_TRUNC also fails. +func createFix (name string) (*os.File, error) { + var rErr = `Remove the file %s and try again. This only happens on windows: %w` + 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 real error. + return nil, err // Return original error + } // 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 +} + // setupRun does things like get the cooling rate, seed the random numbers. func setupRun(mcPrm *McPrm, cprm *cprm) error { var err error @@ -83,8 +122,8 @@ func setupRun(mcPrm *McPrm, cprm *cprm) error { if mcPrm.fOutName, err = setSuffix(mcPrm.fOutName, ".csv"); err != nil { return err } - if cprm.fOutRaw, err = os.Create(mcPrm.fOutName); err != nil { - return fmt.Errorf ("Trying to create file: %w", err) + if cprm.fOutRaw, err = createFix(mcPrm.fOutName); err != nil { + return err } cprm.fOut = bufio.NewWriter(cprm.fOutRaw) cprm.doTxtFval = true // other parts of code can see that we are writing to file