-
Notifications
You must be signed in to change notification settings - Fork 22
open files on windows with shared delete mode #71
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// +build !windows | ||
|
||
package flatfs | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
) | ||
|
||
func tempFile(dir, pattern string) (f *os.File, err error) { | ||
return ioutil.TempFile(dir, pattern) | ||
} | ||
|
||
func readFile(filename string) ([]byte, error) { | ||
return ioutil.ReadFile(filename) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// +build windows | ||
|
||
package flatfs | ||
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
"strconv" | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
goissue34681 "github.com/alexbrainman/goissue34681" | ||
) | ||
|
||
var tmpRand uint32 | ||
var randmu sync.Mutex | ||
|
||
func reseed() uint32 { | ||
return uint32(time.Now().UnixNano() + int64(os.Getpid())) | ||
} | ||
|
||
func nextRandom() string { | ||
randmu.Lock() | ||
r := tmpRand | ||
if r == 0 { | ||
r = reseed() | ||
} | ||
r = r*1664525 + 1013904223 // constants from Numerical Recipes | ||
tmpRand = r | ||
randmu.Unlock() | ||
return strconv.Itoa(int(1e9 + r%1e9))[1:] | ||
} | ||
|
||
func prefixAndSuffix(pattern string) (prefix, suffix string) { | ||
if pos := strings.LastIndex(pattern, "*"); pos != -1 { | ||
prefix, suffix = pattern[:pos], pattern[pos+1:] | ||
} else { | ||
prefix = pattern | ||
} | ||
return | ||
} | ||
|
||
func tempFile(dir, pattern string) (f *os.File, err error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually, now that we're doing this, we should rename the temporary file while it's open. If we do that, nobody will be able to re-open the file without the share delete flag. |
||
if dir == "" { | ||
dir = os.TempDir() | ||
} | ||
|
||
prefix, suffix := prefixAndSuffix(pattern) | ||
|
||
nconflict := 0 | ||
for i := 0; i < 10000; i++ { | ||
name := filepath.Join(dir, prefix+nextRandom()+suffix) | ||
f, err = goissue34681.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) | ||
if os.IsExist(err) { | ||
if nconflict++; nconflict > 10 { | ||
randmu.Lock() | ||
tmpRand = reseed() | ||
randmu.Unlock() | ||
} | ||
continue | ||
} | ||
break | ||
} | ||
return | ||
} | ||
|
||
func readFile(filename string) ([]byte, error) { | ||
f, err := goissue34681.Open(filename) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer f.Close() | ||
// It's a good but not certain bet that FileInfo will tell us exactly how much to | ||
// read, so let's try it but be prepared for the answer to be wrong. | ||
var n int64 = bytes.MinRead | ||
|
||
if fi, err := f.Stat(); err == nil { | ||
// As initial capacity for readAll, use Size + a little extra in case Size | ||
// is zero, and to avoid another allocation after Read has filled the | ||
// buffer. The readAll call will read into its allocated internal buffer | ||
// cheaply. If the size was wrong, we'll either waste some space off the end | ||
// or reallocate as needed, but in the overwhelmingly common case we'll get | ||
// it just right. | ||
if size := fi.Size() + bytes.MinRead; size > n { | ||
n = size | ||
} | ||
} | ||
|
||
return ioutil.ReadAll(f) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's document that we got this from golang (license, attribution, etc.)