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 {
|
if c.File.Hash != fileHash {
|
||||||
return fmt.Errorf("\nUh oh! %s is corrupted! Sorry, try again.\n", c.File.Name)
|
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
|
if c.File.IsDir { // if the file was originally a dir
|
||||||
fmt.Print("decompressing folder")
|
fmt.Print("decompressing folder")
|
||||||
log.Debug("untarring " + c.File.Name)
|
log.Debug("untarring " + c.File.Name)
|
||||||
err := tarinator.UnTarinate(c.Path, path.Join(c.Path, c.File.Name))
|
err := tarinator.UnTarinate(c.Path, path.Join(c.Path, c.File.Name))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
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))
|
|
||||||
}
|
}
|
||||||
|
// 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 {
|
for id := range files {
|
||||||
files[id] = path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id))
|
files[id] = path.Join(c.Path, c.File.Name+".enc."+strconv.Itoa(id))
|
||||||
}
|
}
|
||||||
toRemove := true
|
toRemove := !c.Debug
|
||||||
if c.Debug {
|
|
||||||
toRemove = false
|
|
||||||
}
|
|
||||||
return CatFiles(files, path.Join(c.Path, c.File.Name+".enc"), toRemove)
|
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:
|
case b1 := <-chan1:
|
||||||
if b1 == nil {
|
if b1 == nil {
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
conn2.Write(b1)
|
|
||||||
}
|
}
|
||||||
|
conn2.Write(b1)
|
||||||
|
|
||||||
case b2 := <-chan2:
|
case b2 := <-chan2:
|
||||||
if b2 == nil {
|
if b2 == nil {
|
||||||
return
|
return
|
||||||
} else {
|
|
||||||
conn1.Write(b2)
|
|
||||||
}
|
}
|
||||||
|
conn1.Write(b2)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
28
utils.go
28
utils.go
|
@ -11,31 +11,32 @@ import (
|
||||||
"github.com/pkg/errors"
|
"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)
|
finished, err := os.Create(outfile)
|
||||||
defer finished.Close()
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "CatFiles create: ")
|
return errors.Wrap(err, "CatFiles create: ")
|
||||||
}
|
}
|
||||||
for i := range files {
|
defer finished.Close()
|
||||||
fh, err := os.Open(files[i])
|
for _, file := range files {
|
||||||
|
fh, err := os.Open(file)
|
||||||
if err != nil {
|
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)
|
_, err = io.Copy(finished, fh)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrap(err, "CatFiles copy: ")
|
return errors.Wrap(err, "CatFiles copy: ")
|
||||||
}
|
}
|
||||||
fh.Close()
|
if remove {
|
||||||
if len(remove) > 0 && remove[0] {
|
os.Remove(file)
|
||||||
os.Remove(files[i])
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
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) {
|
func SplitFile(fileName string, numPieces int) (err error) {
|
||||||
file, err := os.Open(fileName)
|
file, err := os.Open(fileName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -61,10 +62,13 @@ func SplitFile(fileName string, numPieces int) (err error) {
|
||||||
for {
|
for {
|
||||||
n, err := file.Read(buf)
|
n, err := file.Read(buf)
|
||||||
out.Write(buf[:n])
|
out.Write(buf[:n])
|
||||||
bytesRead += n
|
// If written bytes count is smaller than lenght of buffer
|
||||||
if err == io.EOF {
|
// then we don't create one more empty file
|
||||||
|
if err == io.EOF || n < len(buf) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
bytesRead += n
|
||||||
|
|
||||||
if bytesRead >= bytesPerPiece {
|
if bytesRead >= bytesPerPiece {
|
||||||
// Close file and open a new one
|
// Close file and open a new one
|
||||||
out.Close()
|
out.Close()
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -9,5 +10,6 @@ func TestSplitFile(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
os.Remove("README.md.0")
|
||||||
|
os.Remove("README.md.1")
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue