diff --git a/utils/tempfile.go b/utils/tempfile.go new file mode 100644 index 0000000..f99629e --- /dev/null +++ b/utils/tempfile.go @@ -0,0 +1,67 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package fileutils + +import ( + "os" + "path/filepath" + "strconv" + "sync" + "time" +) + +// Random number state. +// We generate random temporary file names so that there's a good +// chance the file doesn't exist yet - keeps the number of tries in +// TempFile to a minimum. +var rand uint32 +var randmu sync.Mutex + +func reseed() uint32 { + return uint32(time.Now().UnixNano() + int64(os.Getpid())) +} + +func nextSuffix() string { + randmu.Lock() + r := rand + if r == 0 { + r = reseed() + } + r = r*1664525 + 1013904223 // constants from Numerical Recipes + rand = r + randmu.Unlock() + return strconv.Itoa(int(1e9 + r%1e9))[1:] +} + +// TempFile creates a new temporary file in the directory dir +// with a name beginning with prefix and ending with ext, opens the +// file for reading and writing, and returns the resulting *os.File. +// If dir is the empty string, TempFile uses the default directory +// for temporary files (see os.TempDir). +// Multiple programs calling TempFile simultaneously +// will not choose the same file. The caller can use f.Name() +// to find the pathname of the file. It is the caller's responsibility +// to remove the file when no longer needed. +func TempFile(dir, prefix, ext string) (f *os.File, err error) { + if dir == "" { + dir = os.TempDir() + } + + nconflict := 0 + for i := 0; i < 10000; i++ { + name := filepath.Join(dir, prefix+nextSuffix()+"."+ext) + f, err = os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0600) + if os.IsExist(err) { + if nconflict++; nconflict > 10 { + randmu.Lock() + rand = reseed() + randmu.Unlock() + } + continue + } + break + } + return +} diff --git a/writeas/posts.go b/writeas/posts.go index f3b6748..085c542 100644 --- a/writeas/posts.go +++ b/writeas/posts.go @@ -1,148 +1,149 @@ package main import ( "fmt" "github.com/writeas/writeas-cli/utils" "io/ioutil" "os" "path/filepath" "strings" ) const ( POSTS_FILE = "posts.psv" SEPARATOR = `|` ) type Post struct { ID string EditToken string } func userDataDir() string { return filepath.Join(parentDataDir(), DATA_DIR_NAME) } func dataDirExists() bool { return fileutils.Exists(userDataDir()) } func createDataDir() { err := os.Mkdir(userDataDir(), 0700) if err != nil { if DEBUG { panic(err) } else { fmt.Printf("Error creating data directory: %s\n", err) return } } } func addPost(id, token string) { f, err := os.OpenFile(filepath.Join(userDataDir(), POSTS_FILE), os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600) if err != nil { if DEBUG { panic(err) } else { fmt.Printf("Error creating local posts list: %s\n", err) return } } defer f.Close() l := fmt.Sprintf("%s%s%s\n", id, SEPARATOR, token) if _, err = f.WriteString(l); err != nil { if DEBUG { panic(err) } else { fmt.Printf("Error writing to local posts list: %s\n", err) return } } } func tokenFromID(id string) string { post := fileutils.FindLine(filepath.Join(userDataDir(), POSTS_FILE), id) if post == "" { return "" } parts := strings.Split(post, SEPARATOR) if len(parts) < 2 { return "" } return parts[1] } func removePost(id string) { fileutils.RemoveLine(filepath.Join(userDataDir(), POSTS_FILE), id) } func getPosts() *[]Post { lines := fileutils.ReadData(filepath.Join(userDataDir(), POSTS_FILE)) posts := []Post{} parts := make([]string, 2) for _, l := range *lines { parts = strings.Split(l, SEPARATOR) if len(parts) < 2 { continue } posts = append(posts, Post{ID: parts[0], EditToken: parts[1]}) } return &posts } func composeNewPost() *[]byte { - f, err := ioutil.TempFile(os.TempDir(), "WApost") + f, err := fileutils.TempFile(os.TempDir(), "WApost", "txt") if err != nil { if DEBUG { panic(err) } else { fmt.Printf("Error creating temp file: %s\n", err) return nil } } defer os.Remove(f.Name()) + f.Close() cmd := editPostCmd(f.Name()) if cmd == nil { fmt.Println(NO_EDITOR_ERR) return nil } cmd.Stdin = os.Stdin cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if err := cmd.Start(); err != nil { if DEBUG { panic(err) } else { fmt.Printf("Error starting editor: %s\n", err) return nil } } if err := cmd.Wait(); err != nil { if DEBUG { panic(err) } else { fmt.Printf("Editor finished with error: %s\n", err) return nil } } post, err := ioutil.ReadFile(f.Name()) if err != nil { if DEBUG { panic(err) } else { fmt.Printf("Error reading post: %s\n", err) return nil } } return &post } diff --git a/writeas/posts_win.go b/writeas/posts_win.go index d7b5663..9ecbae7 100644 --- a/writeas/posts_win.go +++ b/writeas/posts_win.go @@ -1,21 +1,22 @@ // +build windows package main import ( "os" "os/exec" ) const ( DATA_DIR_NAME = "Write.as" NO_EDITOR_ERR = "Error getting default editor. You shouldn't see this, so let us know you did: hello@write.as" ) func parentDataDir() string { return os.Getenv("APPDATA") } func editPostCmd(fname string) *exec.Cmd { - return exec.Command(fname) + // NOTE this won't work if fname contains spaces. + return exec.Command("cmd", "/C start /WAIT "+fname) }