mirror of
https://github.com/schollz/croc.git
synced 2025-01-06 05:13:02 +03:00
check if file exists
This commit is contained in:
parent
0d5836b4e8
commit
f1b62ea30d
8
main.go
8
main.go
@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
|
||||
croc "github.com/schollz/croc/src"
|
||||
)
|
||||
@ -10,17 +11,20 @@ func main() {
|
||||
var err error
|
||||
role := flag.Int("role", 0, "role number")
|
||||
passphrase := flag.String("code", "chou", "codephrase")
|
||||
fname := flag.String("file", "", "codephrase")
|
||||
flag.Parse()
|
||||
|
||||
c := croc.Init()
|
||||
// croc.SetLogLevel("error")
|
||||
if *role == -1 {
|
||||
err = c.Relay()
|
||||
} else if *role == 0 {
|
||||
err = c.Send("README.md", *passphrase)
|
||||
err = c.Send(*fname, *passphrase)
|
||||
} else {
|
||||
err = c.Receive(*passphrase)
|
||||
}
|
||||
if err != nil {
|
||||
panic(err)
|
||||
fmt.Print("Error: ")
|
||||
fmt.Println(err.Error())
|
||||
}
|
||||
}
|
||||
|
@ -40,6 +40,10 @@ func (c *Croc) processFile(src string) (err error) {
|
||||
fd.Name = "stdin"
|
||||
fd.DeleteAfterSending = true
|
||||
} else {
|
||||
if !exists(src) {
|
||||
err = errors.Errorf("file/folder '%s' does not exist", src)
|
||||
return
|
||||
}
|
||||
pathToFile, filename = filepath.Split(filepath.Clean(src))
|
||||
fd.Name = filename
|
||||
}
|
||||
@ -47,12 +51,17 @@ func (c *Croc) processFile(src string) (err error) {
|
||||
// check wether the file is a dir
|
||||
info, err := os.Stat(path.Join(pathToFile, filename))
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
fd.IsDir = info.Mode().IsDir()
|
||||
|
||||
// zip file
|
||||
c.crocFile, err = zipFile(path.Join(pathToFile, filename), c.UseCompression)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
fd.IsCompressed = c.UseCompression
|
||||
|
||||
fd.Hash, err = hashFile(c.crocFile)
|
||||
|
@ -67,6 +67,7 @@ func unzipFile(src, dest string) (err error) {
|
||||
}
|
||||
|
||||
func zipFile(fname string, compress bool) (writtenFilename string, err error) {
|
||||
log.Debugf("zipping %s with compression? %v", fname, compress)
|
||||
pathtofile, filename := filepath.Split(fname)
|
||||
curdir, err := os.Getwd()
|
||||
if err != nil {
|
||||
@ -78,7 +79,8 @@ func zipFile(fname string, compress bool) (writtenFilename string, err error) {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
newfile, err := ioutil.TempFile(".", "croc-zipped")
|
||||
log.Debugf("current directory: %s", curdir)
|
||||
newfile, err := ioutil.TempFile(curdir, "croc-zipped")
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
@ -87,6 +89,7 @@ func zipFile(fname string, compress bool) (writtenFilename string, err error) {
|
||||
defer newfile.Close()
|
||||
|
||||
defer os.Chdir(curdir)
|
||||
log.Debugf("changing dir to %s", pathtofile)
|
||||
os.Chdir(pathtofile)
|
||||
|
||||
zipWriter := zip.NewWriter(newfile)
|
||||
@ -165,7 +168,7 @@ func zipFile(fname string, compress bool) (writtenFilename string, err error) {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
_, err = io.Copy(writer, newfile)
|
||||
_, err = io.Copy(writer, zipfile)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
|
@ -1,6 +1,7 @@
|
||||
package croc
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
log "github.com/cihub/seelog"
|
||||
@ -11,10 +12,10 @@ func TestZip(t *testing.T) {
|
||||
defer log.Flush()
|
||||
writtenFilename, err := zipFile("../testing_data", false)
|
||||
assert.Nil(t, err)
|
||||
// defer os.Remove(writtenFilename)
|
||||
defer os.Remove(writtenFilename)
|
||||
|
||||
err = unzipFile(writtenFilename, ".")
|
||||
assert.Nil(t, err)
|
||||
assert.True(t, exists("testing_data"))
|
||||
// os.RemoveAll("testing_data")
|
||||
os.RemoveAll("testing_data")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user