|
@@ -10,54 +10,74 @@ import (
|
|
|
"path"
|
|
"path"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
-type Hfile struct {
|
|
|
|
|
- name string
|
|
|
|
|
- dir string
|
|
|
|
|
- path string
|
|
|
|
|
- handle *os.File
|
|
|
|
|
-}
|
|
|
|
|
|
|
+// File locations enum
|
|
|
|
|
+type fileLoc uint8
|
|
|
|
|
+
|
|
|
|
|
+const (
|
|
|
|
|
+ locHome fileLoc = 0 // in user home dir
|
|
|
|
|
+ locTemp fileLoc = 1 // in temp dir
|
|
|
|
|
+)
|
|
|
|
|
|
|
|
-type Tmpfile struct {
|
|
|
|
|
- path string
|
|
|
|
|
- dir string
|
|
|
|
|
- name string
|
|
|
|
|
- handle *os.File
|
|
|
|
|
|
|
+// File stucture
|
|
|
|
|
+type file struct {
|
|
|
|
|
+ path, dir, name string
|
|
|
|
|
+ floc fileLoc
|
|
|
|
|
+ handle *os.File
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (f *Hfile) init() {
|
|
|
|
|
- u, err := user.Current()
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- fmt.Println("Panicking!")
|
|
|
|
|
- panic(fmt.Sprintf("%v", err))
|
|
|
|
|
|
|
+func (f *file) init(fname string, loc fileLoc) {
|
|
|
|
|
+
|
|
|
|
|
+ // define outside switch scope
|
|
|
|
|
+ var dir string
|
|
|
|
|
+ var err error
|
|
|
|
|
+
|
|
|
|
|
+ // Set file name
|
|
|
|
|
+ f.name = fname
|
|
|
|
|
+
|
|
|
|
|
+ // Using switch (in case there's going ever to be more file types)
|
|
|
|
|
+ switch loc {
|
|
|
|
|
+ case locHome:
|
|
|
|
|
+ var u *user.User
|
|
|
|
|
+ u, err = user.Current()
|
|
|
|
|
+ dir = u.HomeDir
|
|
|
|
|
+ case locTemp:
|
|
|
|
|
+ dir, err = ioutil.TempDir(os.TempDir(), f.name)
|
|
|
}
|
|
}
|
|
|
- f.dir = u.HomeDir
|
|
|
|
|
- f.path = path.Join(f.dir, f.name)
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-func (f *Tmpfile) init() {
|
|
|
|
|
- tmpdir, err := ioutil.TempDir(os.TempDir(), f.name)
|
|
|
|
|
|
|
+ // Check for error
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
fmt.Println("Panicking!")
|
|
fmt.Println("Panicking!")
|
|
|
panic(fmt.Sprintf("%v", err))
|
|
panic(fmt.Sprintf("%v", err))
|
|
|
}
|
|
}
|
|
|
- f.dir = tmpdir
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Set necessary data
|
|
|
|
|
+ f.floc = loc
|
|
|
|
|
+ f.dir = dir
|
|
|
f.path = path.Join(f.dir, f.name)
|
|
f.path = path.Join(f.dir, f.name)
|
|
|
|
|
+
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
-func (h *Hfile) Open() {
|
|
|
|
|
- fh, err := os.Open(h.path)
|
|
|
|
|
- if err != nil {
|
|
|
|
|
- fmt.Print(err)
|
|
|
|
|
|
|
+func (f *file) Open() {
|
|
|
|
|
+
|
|
|
|
|
+ // define outside switch scope
|
|
|
|
|
+ var fh *os.File
|
|
|
|
|
+ var err error
|
|
|
|
|
+
|
|
|
|
|
+ // Different permissions for different locations
|
|
|
|
|
+ switch f.floc {
|
|
|
|
|
+ case locHome:
|
|
|
|
|
+ fh, err = os.Open(h.path)
|
|
|
|
|
+ case locTemp:
|
|
|
|
|
+ fh, err = os.OpenFile(h.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
}
|
|
}
|
|
|
- h.handle = fh
|
|
|
|
|
-}
|
|
|
|
|
|
|
|
|
|
-func (h *Tmpfile) Open() {
|
|
|
|
|
- fh, err := os.OpenFile(h.path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
|
|
|
|
|
|
|
+ // Check for error
|
|
|
if err != nil {
|
|
if err != nil {
|
|
|
- fmt.Print(err)
|
|
|
|
|
|
|
+ fmt.Println(err)
|
|
|
}
|
|
}
|
|
|
- h.handle = fh
|
|
|
|
|
|
|
+
|
|
|
|
|
+ // Set handle
|
|
|
|
|
+ f.handle = fh
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
func CopyFile(src, dst string) (int64, error) {
|
|
func CopyFile(src, dst string) (int64, error) {
|