0
0
Fork 0
mirror of https://github.com/schollz/croc.git synced 2025-10-11 13:21:00 +02:00

check if file exists

This commit is contained in:
Zack Scholl 2018-06-30 09:01:30 -07:00
parent 0d5836b4e8
commit f1b62ea30d
4 changed files with 23 additions and 6 deletions

View file

@ -2,6 +2,7 @@ package main
import ( import (
"flag" "flag"
"fmt"
croc "github.com/schollz/croc/src" croc "github.com/schollz/croc/src"
) )
@ -10,17 +11,20 @@ func main() {
var err error var err error
role := flag.Int("role", 0, "role number") role := flag.Int("role", 0, "role number")
passphrase := flag.String("code", "chou", "codephrase") passphrase := flag.String("code", "chou", "codephrase")
fname := flag.String("file", "", "codephrase")
flag.Parse() flag.Parse()
c := croc.Init() c := croc.Init()
// croc.SetLogLevel("error")
if *role == -1 { if *role == -1 {
err = c.Relay() err = c.Relay()
} else if *role == 0 { } else if *role == 0 {
err = c.Send("README.md", *passphrase) err = c.Send(*fname, *passphrase)
} else { } else {
err = c.Receive(*passphrase) err = c.Receive(*passphrase)
} }
if err != nil { if err != nil {
panic(err) fmt.Print("Error: ")
fmt.Println(err.Error())
} }
} }

View file

@ -40,6 +40,10 @@ func (c *Croc) processFile(src string) (err error) {
fd.Name = "stdin" fd.Name = "stdin"
fd.DeleteAfterSending = true fd.DeleteAfterSending = true
} else { } else {
if !exists(src) {
err = errors.Errorf("file/folder '%s' does not exist", src)
return
}
pathToFile, filename = filepath.Split(filepath.Clean(src)) pathToFile, filename = filepath.Split(filepath.Clean(src))
fd.Name = filename fd.Name = filename
} }
@ -47,12 +51,17 @@ func (c *Croc) processFile(src string) (err error) {
// check wether the file is a dir // check wether the file is a dir
info, err := os.Stat(path.Join(pathToFile, filename)) info, err := os.Stat(path.Join(pathToFile, filename))
if err != nil { if err != nil {
log.Error(err)
return return
} }
fd.IsDir = info.Mode().IsDir() fd.IsDir = info.Mode().IsDir()
// zip file // zip file
c.crocFile, err = zipFile(path.Join(pathToFile, filename), c.UseCompression) c.crocFile, err = zipFile(path.Join(pathToFile, filename), c.UseCompression)
if err != nil {
log.Error(err)
return
}
fd.IsCompressed = c.UseCompression fd.IsCompressed = c.UseCompression
fd.Hash, err = hashFile(c.crocFile) fd.Hash, err = hashFile(c.crocFile)

View file

@ -67,6 +67,7 @@ func unzipFile(src, dest string) (err error) {
} }
func zipFile(fname string, compress bool) (writtenFilename 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) pathtofile, filename := filepath.Split(fname)
curdir, err := os.Getwd() curdir, err := os.Getwd()
if err != nil { if err != nil {
@ -78,7 +79,8 @@ func zipFile(fname string, compress bool) (writtenFilename string, err error) {
log.Error(err) log.Error(err)
return return
} }
newfile, err := ioutil.TempFile(".", "croc-zipped") log.Debugf("current directory: %s", curdir)
newfile, err := ioutil.TempFile(curdir, "croc-zipped")
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return return
@ -87,6 +89,7 @@ func zipFile(fname string, compress bool) (writtenFilename string, err error) {
defer newfile.Close() defer newfile.Close()
defer os.Chdir(curdir) defer os.Chdir(curdir)
log.Debugf("changing dir to %s", pathtofile)
os.Chdir(pathtofile) os.Chdir(pathtofile)
zipWriter := zip.NewWriter(newfile) zipWriter := zip.NewWriter(newfile)
@ -165,7 +168,7 @@ func zipFile(fname string, compress bool) (writtenFilename string, err error) {
log.Error(err) log.Error(err)
return return
} }
_, err = io.Copy(writer, newfile) _, err = io.Copy(writer, zipfile)
if err != nil { if err != nil {
log.Error(err) log.Error(err)
return return

View file

@ -1,6 +1,7 @@
package croc package croc
import ( import (
"os"
"testing" "testing"
log "github.com/cihub/seelog" log "github.com/cihub/seelog"
@ -11,10 +12,10 @@ func TestZip(t *testing.T) {
defer log.Flush() defer log.Flush()
writtenFilename, err := zipFile("../testing_data", false) writtenFilename, err := zipFile("../testing_data", false)
assert.Nil(t, err) assert.Nil(t, err)
// defer os.Remove(writtenFilename) defer os.Remove(writtenFilename)
err = unzipFile(writtenFilename, ".") err = unzipFile(writtenFilename, ".")
assert.Nil(t, err) assert.Nil(t, err)
assert.True(t, exists("testing_data")) assert.True(t, exists("testing_data"))
// os.RemoveAll("testing_data") os.RemoveAll("testing_data")
} }