Exemple #1
0
func TestBigFile(t *testing.T) {
	src := testutil.CreateDummyBuf(147611)
	dst := &bytes.Buffer{}

	srcEnc := &bytes.Buffer{}
	wEnc, err := encrypt.NewWriter(srcEnc, TestKey)
	if err != nil {
		t.Errorf("Cannot create write-encryption layer: %v", err)
		return
	}

	if err := wEnc.Close(); err != nil {
		t.Errorf("Cannot close write-encryption layer: %v", err)
		return
	}

	wDec, err := encrypt.NewReader(bytes.NewReader(srcEnc.Bytes()), TestKey)
	if err != nil {
		t.Errorf("Cannot create read-encryption layer: %v", err)
		return
	}

	// Act a bit like the fuse layer:
	lay := NewLayer(wDec)
	lay.Truncate(0)

	bufSize := 128 * 1024
	if _, err := io.CopyBuffer(lay, bytes.NewReader(src), make([]byte, bufSize)); err != nil {
		t.Errorf("Could not encrypt data")
		return
	}

	lay.Truncate(int64(len(src)))

	if _, err := lay.Seek(0, os.SEEK_SET); err != nil {
		t.Errorf("Seeking to 0 in big file failed: %v", err)
		return
	}

	n, err := io.CopyBuffer(dst, lay, make([]byte, bufSize))
	if err != nil {
		t.Errorf("Could not copy big file data over overlay: %v", err)
		return
	}

	if n != int64(len(src)) {
		t.Errorf("Did not fully copy big file: got %d, should be %d bytes", n, len(src))
		return
	}

	if !bytes.Equal(dst.Bytes(), src) {
		t.Errorf("Source and destination buffers differ.")
		return
	}
}
Exemple #2
0
// NewFileReader reads an unencrypted, uncompressed file and
// returns a reader that will yield the data we feed to ipfs.
func NewFileReader(key []byte, r io.Reader, algo compress.AlgorithmType) (or io.Reader, err error) {
	pr, pw := io.Pipe()

	// Setup the writer part:
	wEnc, encErr := encrypt.NewWriter(pw, key)
	if encErr != nil {
		fmt.Println("Enc w fail", err)
		return nil, encErr
	}

	wZip, zipErr := compress.NewWriter(wEnc, algo)
	if zipErr != nil {
		fmt.Println("zip w fail", err)
		return nil, zipErr
	}

	// Suck the reader empty and move it to `wZip`.
	// Every write to wZip will be available as read in `pr`.
	go func() {
		defer func() {
			if zipCloseErr := wZip.Close(); zipCloseErr != nil {
				fmt.Println("wenc close")
				err = zipCloseErr
			}

			if encCloseErr := wEnc.Close(); encCloseErr != nil {
				fmt.Println("wenc close")
				err = encCloseErr
			}

			if pwErr := pw.Close(); pwErr != nil {
				fmt.Println("pw close")
				err = pwErr
			}
		}()

		if _, copyErr := io.Copy(wZip, r); copyErr != nil {
			// TODO: yup.
			fmt.Println("copy f****d up")
			err = copyErr
		}
	}()

	return pr, nil
}
Exemple #3
0
func main() {
	opts := parseFlags()

	if len(opts.args) != 1 {
		dieWithUsage()
	}
	if opts.compress && opts.decompress {
		dieWithUsage()
	}
	if !opts.compress && !opts.decompress {
		dieWithUsage()
	}

	srcPath := opts.args[0]
	algo, err := compress.FromString(opts.algo)
	if err != nil {
		die(err)
	}

	src := openSrc(srcPath)
	defer src.Close()

	dstPath := dstFilename(opts.compress, srcPath, opts.algo)
	if opts.useDevNull {
		dstPath = os.DevNull
	}

	dst := openDst(dstPath, opts.forceDstOverwrite)
	defer dst.Close()

	pass, err := pwd.PromptPassword()
	if err != nil {
		die(err)
	}

	key := derivateAesKey([]byte(pass), nil, 32)
	if key == nil {
		die(err)
	}

	fmt.Println("Processing...")
	nBytes := int64(0)
	elapsed := withTime(func() {
		if opts.compress {
			ew, err := encrypt.NewWriter(dst, key)
			if err != nil {
				die(err)
			}
			zw, err := compress.NewWriter(ew, algo)
			if err != nil {
				die(err)
			}
			nBytes, err = io.Copy(zw, src)
			if err != nil {
				die(err)
			}
			if err := zw.Close(); err != nil {
				die(err)
			}
			if err := ew.Close(); err != nil {
				die(err)
			}
		}
		if opts.decompress {
			er, err := encrypt.NewReader(src, key)
			if err != nil {
				die(err)
			}
			zr := compress.NewReader(er)
			nBytes, err = io.Copy(dst, zr)
			if err != nil {
				die(err)
			}
		}
	})
	fmt.Printf(
		"%s created, %s processed in %.2f seconds.\n",
		dstPath, humanize.Bytes(uint64(nBytes)), elapsed.Seconds(),
	)
}