mirror of https://github.com/schollz/croc.git
commit
e2de3c7444
36
connect.go
36
connect.go
|
@ -422,24 +422,23 @@ func (c *Connection) runClient() error {
|
|||
|
||||
if c.File.Hash != fileHash {
|
||||
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
|
||||
} else {
|
||||
if c.File.IsDir { // if the file was originally a dir
|
||||
fmt.Print("decompressing folder")
|
||||
log.Debug("untarring " + c.File.Name)
|
||||
err := tarinator.UnTarinate(c.Path, path.Join(c.Path, c.File.Name))
|
||||
}
|
||||
if c.File.IsDir { // if the file was originally a dir
|
||||
fmt.Print("decompressing folder")
|
||||
log.Debug("untarring " + c.File.Name)
|
||||
err := tarinator.UnTarinate(c.Path, path.Join(c.Path, c.File.Name))
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// we remove the old tar.gz file
|
||||
err = os.Remove(path.Join(c.Path, c.File.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("\nReceived folder written to %s\n", path.Join(c.Path, c.File.Name[:len(c.File.Name)-4]))
|
||||
} else {
|
||||
fmt.Printf("\nReceived file written to %s\n", path.Join(c.Path, c.File.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// we remove the old tar.gz file
|
||||
err = os.Remove(path.Join(c.Path, c.File.Name))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("\nReceived folder written to %s\n", path.Join(c.Path, c.File.Name[:len(c.File.Name)-4]))
|
||||
} else {
|
||||
fmt.Printf("\nReceived file written to %s\n", path.Join(c.Path, c.File.Name))
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -461,10 +460,7 @@ func (c *Connection) catFile() error {
|
|||
for id := range files {
|
||||
files[id] = path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id))
|
||||
}
|
||||
toRemove := true
|
||||
if c.Debug {
|
||||
toRemove = false
|
||||
}
|
||||
toRemove := !c.Debug
|
||||
return CatFiles(files, path.Join(c.Path, c.File.Name+".enc"), toRemove)
|
||||
}
|
||||
|
||||
|
|
7
relay.go
7
relay.go
|
@ -303,15 +303,14 @@ func Pipe(conn1 net.Conn, conn2 net.Conn) {
|
|||
case b1 := <-chan1:
|
||||
if b1 == nil {
|
||||
return
|
||||
} else {
|
||||
conn2.Write(b1)
|
||||
}
|
||||
conn2.Write(b1)
|
||||
|
||||
case b2 := <-chan2:
|
||||
if b2 == nil {
|
||||
return
|
||||
} else {
|
||||
conn1.Write(b2)
|
||||
}
|
||||
conn1.Write(b2)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
28
utils.go
28
utils.go
|
@ -11,31 +11,32 @@ import (
|
|||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
func CatFiles(files []string, outfile string, remove ...bool) error {
|
||||
// CatFiles copies data from n files to a single one and removes source files
|
||||
// if Debug mode is set to false
|
||||
func CatFiles(files []string, outfile string, remove bool) error {
|
||||
finished, err := os.Create(outfile)
|
||||
defer finished.Close()
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "CatFiles create: ")
|
||||
}
|
||||
for i := range files {
|
||||
fh, err := os.Open(files[i])
|
||||
defer finished.Close()
|
||||
for _, file := range files {
|
||||
fh, err := os.Open(file)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "CatFiles open "+files[i]+": ")
|
||||
return errors.Wrap(err, fmt.Sprintf("CatFiles open %v: ", file))
|
||||
}
|
||||
|
||||
defer fh.Close()
|
||||
_, err = io.Copy(finished, fh)
|
||||
if err != nil {
|
||||
return errors.Wrap(err, "CatFiles copy: ")
|
||||
}
|
||||
fh.Close()
|
||||
if len(remove) > 0 && remove[0] {
|
||||
os.Remove(files[i])
|
||||
if remove {
|
||||
os.Remove(file)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// SplitFile
|
||||
// SplitFile creates a bunch of smaller files with the data from source splited into them
|
||||
func SplitFile(fileName string, numPieces int) (err error) {
|
||||
file, err := os.Open(fileName)
|
||||
if err != nil {
|
||||
|
@ -61,10 +62,13 @@ func SplitFile(fileName string, numPieces int) (err error) {
|
|||
for {
|
||||
n, err := file.Read(buf)
|
||||
out.Write(buf[:n])
|
||||
bytesRead += n
|
||||
if err == io.EOF {
|
||||
// If written bytes count is smaller than lenght of buffer
|
||||
// then we don't create one more empty file
|
||||
if err == io.EOF || n < len(buf) {
|
||||
break
|
||||
}
|
||||
bytesRead += n
|
||||
|
||||
if bytesRead >= bytesPerPiece {
|
||||
// Close file and open a new one
|
||||
out.Close()
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
|
@ -9,5 +10,6 @@ func TestSplitFile(t *testing.T) {
|
|||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
os.Remove("README.md.0")
|
||||
os.Remove("README.md.1")
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue