Пример #1
0
func NewClient(path string, d debug.Debugger) (*Client, error) {
	c := Client{
		Debugger: d,
	}

	// just in case
	if d == nil {
		c.Debugger = debug.NewDebugNil()
	}

	c.Log(DebugTrace, "[TRC] NewClient %v", path)

	var err error
	c.ts, err = token.New(path, DebugToken, c.Debugger)
	if err != nil {
		return nil, err
	}

	// cache root id
	a, err := c.GetMetadataJSON("")
	if err != nil {
		return nil, err
	}

	// sanity
	if a.Name != "" || a.Kind != AssetFolder || a.IsRoot == false {
		return nil, fmt.Errorf("inconsistent root")
	}
	if a.Status != StatusAvailable {
		return nil, fmt.Errorf("root not available")
	}
	c.root = a.ID

	return &c, nil
}
Пример #2
0
func _main() error {
	// tar like
	create := flag.Bool("c", false, "create archive") // default *is* true
	extract := flag.Bool("x", false, "extract archive")
	lst := flag.Bool("t", false, "list archive contents")
	verbose := flag.Bool("v", false, "verbose")
	compress := flag.Bool("z", false, "enable compression (default false)")
	perms := flag.Bool("p", false, "restore ACL")
	target := flag.String("f", "-", "archive target is Cloud Drive)")
	root := flag.String("C", "", "extract path")

	// not tar like
	debugLevel := flag.Int("d", 0, "debug level: 0 off, 1 trace, 2 loud")
	debugTarget := flag.String("l", "-", "debug target file name, - is stdout")
	flag.Parse()

	args := flag.Args()

	var err error
	a := acdb{
		permList: list.New(),
		target:   *target,
		verbose:  *verbose,
		compress: *compress,
		perms:    *perms,
		root:     *root,
	}
	defer func() {
		goutil.Zero(a.keys.MD[:])
		goutil.Zero(a.keys.Data[:])
		goutil.Zero(a.keys.Dedup[:])
	}()

	// debug target
	if *debugTarget == "-" {
		a.Debugger, err = debug.NewDebugStdout()
		if err != nil {
			return err
		}
	} else {
		a.Debugger, err = debug.NewDebugFile(*debugTarget)
		if err != nil {
			return err
		}
	}

	switch *debugLevel {
	case 0:
		a.Debugger = debug.NewDebugNil()
	case 1:
		a.Debugger.Mask(acd.DebugTrace | acd.DebugHTTP | acd.DebugURL |
			debugApp)
	case 2:
		a.Debugger.Mask(acd.DebugTrace | acd.DebugHTTP | acd.DebugURL |
			acd.DebugBody | acd.DebugJSON | acd.DebugToken |
			acd.DebugLoud | debugApp)
	default:
		return fmt.Errorf("invalid debug level %v", *debugLevel)
	}

	//a.Debugger.Mask(acd.DebugTrace | acd.DebugHTTP | acd.DebugURL |
	//acd.DebugJSON | debugApp)

	a.Log(debugApp, "[APP] start of day")
	defer a.Log(debugApp, "[APP] end of times")

	// default to create
	if *create == false && *extract == false && *lst == false {
		*create = true
	}

	// determine operation
	switch {
	case *create && !(*extract || *lst):
		a.mode = modeCreate

		if len(args) == 0 {
			fmt.Printf("acdbackup <-c> [-vzf target] filenames...\n")
			flag.PrintDefaults()
			return nil
		}

		return a.archive(args)

	case *extract && !(*create || *lst):
		a.mode = modeExtract

		if a.target == "-" {
			return fmt.Errorf("must provide archive metadata file")
		}
		return a.list()

	case *lst && !(*create || *extract):
		a.mode = modeList

		if a.target == "-" {
			return fmt.Errorf("must provide archive metadata file")
		}
		return a.list()

	default:
		return fmt.Errorf("must specify only -c, -x or -t")
	}

	return nil
}
Пример #3
0
func _main() error {
	debugLevel := flag.Int("d", 0, "debug level: 0 off, 1 trace, 2 loud")
	debugTarget := flag.String("l", "-", "debug target file name, - is stdout")
	compress := flag.Bool("c", false, "try to compress (default = false)")
	extract := flag.Bool("e", false, "extract files")
	flag.Parse()

	args := flag.Args()
	if len(args) == 0 {
		fmt.Printf("sfe [-d][-l target] <filename> ...\n")
		flag.PrintDefaults()
		return nil
	}

	var (
		err error
	)

	s := sfe{
		compress: *compress,
	}
	defer func() {
		goutil.Zero(s.keys.MD[:])
		goutil.Zero(s.keys.Data[:])
		goutil.Zero(s.keys.Dedup[:])
	}()

	// debug target
	if *debugTarget == "-" {
		s.Debugger, err = debug.NewDebugStdout()
		if err != nil {
			return err
		}
	} else {
		s.Debugger, err = debug.NewDebugFile(*debugTarget)
		if err != nil {
			return err
		}
	}

	switch *debugLevel {
	case 0:
		s.Debugger = debug.NewDebugNil()
	case 1:
		s.Debugger.Mask(dbgTrace)
	case 2:
		s.Debugger.Mask(dbgTrace | dbgLoud)
	default:
		return fmt.Errorf("invalid debug level %v", *debugLevel)
	}

	keysFilename, err := shared.DefaultKeysFilename()
	if err != nil {
		return err
	}
	rootDir := path.Dir(keysFilename)
	err = os.MkdirAll(rootDir, 0700)
	if err != nil {
		return err
	}

	err = shared.LoadKeys(keysFilename, &s.keys)
	if err != nil {
		return err
	}

	for _, v := range args {
		if *extract {
			s.Log(dbgTrace, "decrypting: %v\n", v)
			err = s.decrypt(v)
			if err != nil {
				fmt.Fprintf(os.Stderr, "could not decrypt: %v\n",
					err)
				continue
			}
		} else {
			s.Log(dbgTrace, "encrypting: %v\n", v)
			err = s.encrypt(v)
			if err != nil {
				fmt.Fprintf(os.Stderr, "could not encrypt: %v\n",
					err)
				continue
			}
		}
	}

	return nil
}