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:
parent
0d5836b4e8
commit
f1b62ea30d
4 changed files with 23 additions and 6 deletions
|
@ -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…
Add table
Add a link
Reference in a new issue