diff --git a/cmd-session.go b/cmd-session.go new file mode 100644 index 00000000..f12ca706 --- /dev/null +++ b/cmd-session.go @@ -0,0 +1,145 @@ +/* + * Minio Client, (C) 2015 Minio, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package main + +import ( + "crypto/rand" + "fmt" + "io" + "io/ioutil" + "os" + "path/filepath" + "strings" + + "github.com/minio/cli" + "github.com/minio/mc/pkg/console" + "github.com/minio/minio/pkg/iodine" +) + +// Help message. +var sessionCmd = cli.Command{ + Name: "session", + Usage: "Start sessions for cp and sync", + Action: runSessionCmd, + CustomHelpTemplate: `NAME: + mc {{.Name}} - {{.Usage}} + +USAGE: + mc {{.Name}}{{if .Flags}} [ARGS...]{{end}} SOURCE TARGET [TARGET...] {{if .Description}} + +DESCRIPTION: + {{.Description}}{{end}}{{if .Flags}} + +FLAGS: + {{range .Flags}}{{.}} + {{end}}{{ end }} + +EXAMPLES: + -- TODO -- + +`, +} + +func isSessionDirExists() bool { + sdir, err := getSessionDir() + if err != nil { + return false + } + _, err = os.Stat(sdir) + if err != nil { + return false + } + return true +} + +func createSessionDir() error { + sdir, err := getSessionDir() + if err != nil { + return err + } + if err := os.MkdirAll(sdir, 0700); err != nil { + return err + } + return nil +} + +func getSessionDir() (string, error) { + mcConfigDir, err := getMcConfigDir() + if err != nil { + return "", err + } + return filepath.Join(mcConfigDir, sessionDir), nil +} + +// newUUID generates a random UUID according to RFC 4122 +func newUUID() string { + uuid := make([]byte, 16) + _, err := io.ReadFull(rand.Reader, uuid) + if err != nil { + panic(err) + } + // variant bits; see section 4.1.1 + uuid[8] = uuid[8]&^0xc0 | 0x80 + // version 4 (pseudo-random); see section 4.1.3 + uuid[6] = uuid[6]&^0xf0 | 0x40 + return fmt.Sprintf("%x-%x-%x-%x-%x", uuid[0:4], uuid[4:6], uuid[6:8], uuid[8:10], uuid[10:]) +} + +func runSessionCmd(ctx *cli.Context) { + if len(ctx.Args()) < 1 || ctx.Args().First() == "help" { + cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code + } + if strings.TrimSpace(ctx.Args().First()) == "" { + cli.ShowCommandHelpAndExit(ctx, "session", 1) // last argument is exit code + } + if !isSessionDirExists() { + if err := createSessionDir(); err != nil { + console.Fatalln(iodine.ToError(err)) + } + } + switch strings.TrimSpace(ctx.Args().First()) { + // list resumable sessions + case "list": + sessionDir, err := getSessionDir() + if err != nil { + console.Fatalln(iodine.ToError(err)) + } + sessions, err := ioutil.ReadDir(sessionDir) + if err != nil { + console.Fatalln(iodine.ToError(err)) + } + for _, session := range sessions { + if session.Mode().IsRegular() { + console.Infoln(session.Name()) + } + } + // purge all pending sessions + case "clear": + sessions, err := ioutil.ReadDir(sessionDir) + if err != nil { + console.Fatalln(iodine.ToError(err)) + } + for _, session := range sessions { + if session.Mode().IsRegular() { + err := os.Remove(filepath.Join(sessionDir, session.Name())) + if err != nil { + console.Fatalln(iodine.ToError(err)) + } + } + } + } +} diff --git a/globals.go b/globals.go index 878ee60b..085b4987 100644 --- a/globals.go +++ b/globals.go @@ -33,6 +33,11 @@ const ( mcConfigFile = "config.json" ) +// session config related constants +const ( + sessionDir = "session" +) + // default access and secret key const ( // do not pass accesskeyid and secretaccesskey through cli diff --git a/main.go b/main.go index 5f4241f7..4a907465 100644 --- a/main.go +++ b/main.go @@ -83,15 +83,16 @@ func main() { runtime.GOMAXPROCS(runtime.NumCPU()) // register all the commands - registerCmd(lsCmd) // List contents of a bucket - registerCmd(mbCmd) // make a bucket - registerCmd(catCmd) // concantenate an object to standard output - registerCmd(cpCmd) // copy objects and files from multiple sources to single destination - registerCmd(syncCmd) // copy objects and files from single source to multiple destionations - registerCmd(diffCmd) // compare two objects - registerCmd(accessCmd) // set permissions [public, private, readonly, authenticated] for buckets and folders. - registerCmd(configCmd) // generate configuration "/home/harsha/.mc/config.json" file. - registerCmd(updateCmd) // update Check for new software updates + registerCmd(lsCmd) // List contents of a bucket + registerCmd(mbCmd) // make a bucket + registerCmd(catCmd) // concantenate an object to standard output + registerCmd(cpCmd) // copy objects and files from multiple sources to single destination + registerCmd(syncCmd) // sync objects and files from single source to multiple destinations + registerCmd(sessionCmd) // session handling for resuming copy and sync operations + registerCmd(diffCmd) // compare two objects + registerCmd(accessCmd) // set permissions [public, private, readonly, authenticated] for buckets and folders. + registerCmd(configCmd) // generate configuration "/home/harsha/.mc/config.json" file. + registerCmd(updateCmd) // update Check for new software updates // register all the flags registerFlag(quietFlag) // suppress console output