diff --git a/cmd/cp-main.go b/cmd/cp-main.go index b79456cd..1cb6448f 100644 --- a/cmd/cp-main.go +++ b/cmd/cp-main.go @@ -41,13 +41,13 @@ var ( Name: "recursive, r", Usage: "copy recursively", }, - cli.IntFlag{ + cli.StringFlag{ Name: "older-than", - Usage: "copy object(s) older than N days", + Usage: "copy objects older than L days, M hours and N minutes", }, - cli.IntFlag{ + cli.StringFlag{ Name: "newer-than", - Usage: "copy object(s) newer than N days", + Usage: "copy objects newer than L days, M hours and N minutes", }, cli.StringFlag{ Name: "storage-class, sc", @@ -93,11 +93,11 @@ EXAMPLES: 4. Copy a bucket recursively from aliased Amazon S3 cloud storage to local filesystem on Windows. $ {{.HelpName}} --recursive s3\documents\2014\ C:\Backups\2014 - 5. Copy files older than 7 days from Minio cloud storage to Amazon S3 cloud storage. - $ {{.HelpName}} --older-than 7 play/mybucket/burningman2011/ s3/mybucket/ + 5. Copy files older than 7 days and 10 hours from Minio cloud storage to Amazon S3 cloud storage. + $ {{.HelpName}} --older-than 7d10h play/mybucket/burningman2011/ s3/mybucket/ - 6. Copy files newer than 7 days from Minio cloud storage to a local path. - $ {{.HelpName}} --newer-than 7 play/mybucket/burningman2011/ ~/latest/ + 6. Copy files newer than 7 days and 10 hours from Minio cloud storage to a local path. + $ {{.HelpName}} --newer-than 7d10h play/mybucket/burningman2011/ ~/latest/ 7. Copy an object with name containing unicode characters to Amazon S3 cloud storage. $ {{.HelpName}} 本語 s3/andoria/ @@ -217,8 +217,8 @@ func doPrepareCopyURLs(session *sessionV8, trapCh <-chan bool, cancelCopy contex // Access recursive flag inside the session header. isRecursive := session.Header.CommandBoolFlags["recursive"] - olderThan := session.Header.CommandIntFlags["older-than"] - newerThan := session.Header.CommandIntFlags["newer-than"] + olderThan := session.Header.CommandStringFlags["older-than"] + newerThan := session.Header.CommandStringFlags["newer-than"] encryptKeys := session.Header.CommandStringFlags["encrypt-key"] encrypt := session.Header.CommandStringFlags["encrypt"] encKeyDB, err := parseAndValidateEncryptionKeys(encryptKeys, encrypt) @@ -260,12 +260,12 @@ func doPrepareCopyURLs(session *sessionV8, trapCh <-chan bool, cancelCopy contex } // Skip objects older than --older-than parameter if specified - if olderThan > 0 && isOlder(cpURLs.SourceContent, olderThan) { + if olderThan != "" && isOlder(cpURLs.SourceContent.Time, olderThan) { continue } // Skip objects newer than --newer-than parameter if specified - if newerThan > 0 && isNewer(cpURLs.SourceContent, newerThan) { + if newerThan != "" && isNewer(cpURLs.SourceContent.Time, newerThan) { continue } @@ -452,8 +452,8 @@ func mainCopy(ctx *cli.Context) error { console.SetColor("Copy", color.New(color.FgGreen, color.Bold)) recursive := ctx.Bool("recursive") - olderThan := ctx.Int("older-than") - newerThan := ctx.Int("newer-than") + olderThan := ctx.String("older-than") + newerThan := ctx.String("newer-than") storageClass := ctx.String("storage-class") sseKeys := os.Getenv("MC_ENCRYPT_KEY") if key := ctx.String("encrypt-key"); key != "" { @@ -464,8 +464,8 @@ func mainCopy(ctx *cli.Context) error { session := newSessionV8() session.Header.CommandType = "cp" session.Header.CommandBoolFlags["recursive"] = recursive - session.Header.CommandIntFlags["older-than"] = olderThan - session.Header.CommandIntFlags["newer-than"] = newerThan + session.Header.CommandStringFlags["older-than"] = olderThan + session.Header.CommandStringFlags["newer-than"] = newerThan session.Header.CommandStringFlags["storage-class"] = storageClass session.Header.CommandStringFlags["encrypt-key"] = sseKeys session.Header.CommandStringFlags["encrypt"] = sse diff --git a/cmd/find-main.go b/cmd/find-main.go index 86d950fd..938ff439 100644 --- a/cmd/find-main.go +++ b/cmd/find-main.go @@ -18,7 +18,6 @@ package cmd import ( "strings" - "time" humanize "github.com/dustin/go-humanize" "github.com/fatih/color" @@ -43,12 +42,12 @@ var ( Usage: "find object names matching wildcard pattern", }, cli.StringFlag{ - Name: "newer", - Usage: "match all objects newer than specified time in units (see UNITS)", + Name: "newer-than", + Usage: "match all objects newer than L days, M hours and N minutes", }, cli.StringFlag{ - Name: "older", - Usage: "match all objects older than specified time in units (see UNITS)", + Name: "older-than", + Usage: "match all objects older than L days, M hours and N minutes", }, cli.StringFlag{ Name: "path", @@ -103,10 +102,8 @@ UNITS units, so that "gi" refers to "gibibyte" or "GiB". A "b" at the end is also accepted. Without suffixes the unit is bytes. - --older, --newer flags accept the suffixes "d", "w", "m" and "y" to refer - to units of days, weeks, months and years respectively. With the standard - rate of conversion being 7 days in 1 week, 30 days in 1 month, and 365 - days in one year. + --older-than, --newer-than flags accept the string for days, hours and minutes + i.e. 1d2h30m states 1 day, 2 hours and 30 minutes. FORMAT Support string substitutions with special interpretations for following keywords. @@ -147,9 +144,9 @@ EXAMPLES: 08. Find all objects created in the last week under "s3/bucket". $ {{.HelpName}} s3/bucket --newer 1w - 09. Find all objects which were created more than 6 months ago, and exclude the ones with ".jpg" + 09. Find all objects which were created are older than 2 days, 5 hours and 10 minutes and exclude the ones with ".jpg" extension under "s3". - $ {{.HelpName}} s3 --older 6m --ignore "*.jpg" + $ {{.HelpName}} s3 --older-than 2d5h10m --ignore "*.jpg" 10. List all objects up to 3 levels sub-directory deep under "s3/bucket". $ {{.HelpName}} s3/bucket --maxdepth 3 @@ -197,8 +194,8 @@ type findContext struct { regexPattern string maxDepth uint printFmt string - olderThan time.Time - newerThan time.Time + olderThan string + newerThan string largerSize uint64 smallerSize uint64 watch bool @@ -232,15 +229,13 @@ func mainFind(ctx *cli.Context) error { clnt, err := newClient(args[0]) fatalIf(err.Trace(args...), "Unable to initialize `"+args[0]+"`.") - var olderThan, newerThan time.Time + var olderThan, newerThan string - if ctx.String("older") != "" { - olderThan, err = parseTime(ctx.String("older")) - fatalIf(err.Trace(ctx.String("older")), "Unable to parse input time.") + if ctx.String("older-than") != "" { + olderThan = ctx.String("older-than") } - if ctx.String("newer") != "" { - newerThan, err = parseTime(ctx.String("newer")) - fatalIf(err.Trace(ctx.String("newer")), "Unable to parse input time.") + if ctx.String("newer-than") != "" { + newerThan = ctx.String("newer-than") } // Use 'e' to indicate Go error, this is a convention followed in `mc`. For probe.Error we call it diff --git a/cmd/find.go b/cmd/find.go index 8c84467a..ae285ee6 100644 --- a/cmd/find.go +++ b/cmd/find.go @@ -396,11 +396,11 @@ func matchFind(ctx *findContext, fileContent contentMessage) (match bool) { if match && ctx.regexPattern != "" { match = regexMatch(ctx.regexPattern, path) } - if match && !ctx.olderThan.IsZero() { - match = fileContent.Time.Before(ctx.olderThan) + if match && ctx.olderThan != "" { + match = !isOlder(fileContent.Time, ctx.olderThan) } - if match && !ctx.newerThan.IsZero() { - match = fileContent.Time.After(ctx.newerThan) || fileContent.Time.Equal(ctx.newerThan) + if match && ctx.newerThan != "" { + match = !isNewer(fileContent.Time, ctx.newerThan) } if match && ctx.largerSize > 0 { match = int64(ctx.largerSize) < fileContent.Size @@ -411,43 +411,6 @@ func matchFind(ctx *findContext, fileContent contentMessage) (match bool) { return match } -// parseTime - parses input value into a corresponding time value in -// time.Time by adding the input time duration to local UTC time.Now(). -func parseTime(duration string) (time.Time, *probe.Error) { - if duration == "" { - return time.Time{}, errInvalidArgument().Trace(duration) - } - - conversion := map[string]int{ - "d": 1, - "w": 7, - "m": 30, - "y": 365, - } - - // Parse the incoming pattern if its exact number. - i, e := strconv.Atoi(duration) - if e != nil { - // If cant parse as regular string look for - // a conversion multiplier, either d,w,m,y. - p := duration[len(duration)-1:] - i, e = strconv.Atoi(duration[:len(duration)-1]) - if e != nil { - // if we still cant parse, user input is invalid, return error. - return time.Time{}, probe.NewError(e) - } - i = i * conversion[strings.ToLower(p)] - } - - now := UTCNow() - - // Find all time in which the time in which the object was just created is after the current time - t := time.Date(now.Year(), now.Month(), now.Day()-i, now.Hour(), now.Minute(), 0, 0, time.UTC) - - // if we reach this line, user has passed a valid alphanumeric string - return t, nil -} - // 7 days in seconds. var defaultSevenDays = time.Duration(604800) * time.Second diff --git a/cmd/find_test.go b/cmd/find_test.go index 0324b492..811c311a 100644 --- a/cmd/find_test.go +++ b/cmd/find_test.go @@ -57,13 +57,13 @@ func TestMatchFind(t *testing.T) { clnt: &s3Client{ targetURL: &clientURL{}, }, - olderThan: time.Unix(12000, 0).UTC(), + olderThan: "1d", }, &findContext{ clnt: &s3Client{ targetURL: &clientURL{}, }, - newerThan: time.Unix(12000, 0).UTC(), + newerThan: "32000d", }, &findContext{ clnt: &s3Client{ @@ -337,57 +337,6 @@ func TestFindMatch(t *testing.T) { } } -// Tests for parsing time layout. -func TestParseTime(t *testing.T) { - testCases := []struct { - value string - success bool - }{ - // Parses 1 day successfully. - { - value: "1d", - success: true, - }, - // Parses 1 week successfully. - { - value: "1w", - success: true, - }, - // Parses 1 year successfully. - { - value: "1y", - success: true, - }, - // Parses 2 months successfully. - { - value: "2m", - success: true, - }, - // Failure to parse "xd". - { - value: "xd", - success: false, - }, - // Failure to parse empty string. - { - value: "", - success: false, - }, - } - for i, testCase := range testCases { - pt, err := parseTime(testCase.value) - if err != nil && testCase.success { - t.Errorf("Test: %d, Expected to be successful, but found error %s, for time value %s", i+1, err, testCase.value) - } - if pt.IsZero() && testCase.success { - t.Errorf("Test: %d, Expected time to be non zero, but found zero time for time value %s", i+1, testCase.value) - } - if err == nil && !testCase.success { - t.Errorf("Test: %d, Expected error but found to be successful for time value %s", i+1, testCase.value) - } - } -} - // Tests string substitution function. func TestStringReplace(t *testing.T) { testCases := []struct { diff --git a/cmd/mirror-main.go b/cmd/mirror-main.go index 1ccd6841..29e3a246 100644 --- a/cmd/mirror-main.go +++ b/cmd/mirror-main.go @@ -71,13 +71,13 @@ var ( Name: "exclude", Usage: "exclude object(s) that match specified object name pattern", }, - cli.IntFlag{ + cli.StringFlag{ Name: "older-than", - Usage: "filter object(s) older than N days", + Usage: "filter object(s) older than L days, M hours and N minutes", }, - cli.IntFlag{ + cli.StringFlag{ Name: "newer-than", - Usage: "filter object(s) newer than N days", + Usage: "filter object(s) newer than L days, M hours and N minutes", }, cli.StringFlag{ Name: "storage-class, sc", @@ -117,8 +117,8 @@ EXAMPLES: 2. Mirror a local folder recursively to Amazon S3 cloud storage. $ {{.HelpName}} backup/ s3/archive - 3. Only mirror files that are newer than 7 days to Amazon S3 cloud storage. - $ {{.HelpName}} --newer-than 7 backup/ s3/archive + 3. Only mirror files that are newer than 7 days, 10 hours and 30 minutes to Amazon S3 cloud storage. + $ {{.HelpName}} --newer-than "7d10h30m" backup/ s3/archive 4. Mirror a bucket from aliased Amazon S3 cloud storage to a folder on Windows. $ {{.HelpName}} s3\documents\2014\ C:\backup\2014 @@ -139,10 +139,10 @@ EXAMPLES: $ {{.HelpName}} --exclude ".*" --exclude "*.temp" s3/test ~/test 9. Mirror objects newer than 10 days from bucket test to a local folder. - $ {{.HelpName}} --newer-than=10 s3/test ~/localfolder + $ {{.HelpName}} --newer-than 10d s3/test ~/localfolder 10. Mirror objects older than 30 days from Amazon S3 bucket test to a local folder. - $ {{.HelpName}} --older-than=30 s3/test ~/test + $ {{.HelpName}} --older-than 30d s3/test ~/test 11. Mirror server encrypted objects from Minio cloud storage to a bucket on Amazon S3 cloud storage $ {{.HelpName}} --encrypt-key "minio/photos=32byteslongsecretkeymustbegiven1,s3/archive=32byteslongsecretkeymustbegiven2" minio/photos/ s3/archive/ @@ -185,7 +185,7 @@ type mirrorJob struct { targetURL string isFake, isRemove, isOverwrite, isWatch bool - olderThan, newerThan int + olderThan, newerThan string storageClass string excludeOptions []string @@ -522,16 +522,12 @@ func (mj *mirrorJob) startMirror(ctx context.Context, cancelMirror context.Cance } if sURLs.SourceContent != nil { - // Skip objects older than --older-than parameter if specified - if mj.olderThan > 0 && isOlder(sURLs.SourceContent, mj.olderThan) { + if mj.olderThan != "" && isOlder(sURLs.SourceContent.Time, mj.olderThan) { continue } - - // Skip objects newer than --newer-than parameter if specified - if mj.newerThan > 0 && isNewer(sURLs.SourceContent, mj.newerThan) { + if mj.newerThan != "" && isNewer(sURLs.SourceContent.Time, mj.newerThan) { continue } - // copy totalBytes += sURLs.SourceContent.Size } @@ -614,7 +610,7 @@ func (mj *mirrorJob) mirror(ctx context.Context, cancelMirror context.CancelFunc } } -func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch bool, excludeOptions []string, olderThan, newerThan int, storageClass string, encKeyDB map[string][]prefixSSEPair) *mirrorJob { +func newMirrorJob(srcURL, dstURL string, isFake, isRemove, isOverwrite, isWatch bool, excludeOptions []string, olderThan, newerThan string, storageClass string, encKeyDB map[string][]prefixSSEPair) *mirrorJob { mj := mirrorJob{ trapCh: signalTrap(os.Interrupt, syscall.SIGTERM, syscall.SIGKILL), m: new(sync.Mutex), @@ -691,8 +687,8 @@ func runMirror(srcURL, dstURL string, ctx *cli.Context, encKeyDB map[string][]pr isOverwrite, ctx.Bool("watch"), ctx.StringSlice("exclude"), - ctx.Int("older-than"), - ctx.Int("newer-than"), + ctx.String("older-than"), + ctx.String("newer-than"), ctx.String("storage-class"), encKeyDB) diff --git a/cmd/rm-main.go b/cmd/rm-main.go index 5cae9344..15a6c4f0 100644 --- a/cmd/rm-main.go +++ b/cmd/rm-main.go @@ -22,7 +22,6 @@ import ( "os" "path/filepath" "strings" - "time" "github.com/fatih/color" "github.com/minio/cli" @@ -31,9 +30,6 @@ import ( "github.com/minio/mc/pkg/probe" ) -// Day time.Duration for day. -const Day = 24 * time.Hour - // rm specific flags. var ( rmFlags = []cli.Flag{ @@ -61,13 +57,13 @@ var ( Name: "stdin", Usage: "read object names from STDIN", }, - cli.IntFlag{ + cli.StringFlag{ Name: "older-than", - Usage: "remove objects older than N days", + Usage: "remove objects older than L days, M hours and N minutes", }, - cli.IntFlag{ + cli.StringFlag{ Name: "newer-than", - Usage: "remove objects newer than N days", + Usage: "remove objects newer than L days, M hours and N minutes", }, } ) @@ -99,10 +95,10 @@ EXAMPLES: $ {{.HelpName}} --recursive s3/jazz-songs/louis/ 3. Remove all objects older than '90' days recursively from bucket 'jazz-songs' that match 'louis' prefix. - $ {{.HelpName}} --recursive --older-than=90 s3/jazz-songs/louis/ + $ {{.HelpName}} --recursive --older-than 90d s3/jazz-songs/louis/ - 4. Remove all objects newer than 7 days recursively from bucket 'pop-songs' - $ {{.HelpName}} --recursive --newer-than=7 s3/pop-songs/ + 4. Remove all objects newer than 7 days and 10 hours recursively from bucket 'pop-songs' + $ {{.HelpName}} --recursive --newer-than 7d10h s3/pop-songs/ 5. Remove all objects read from STDIN. $ {{.HelpName}} --force --stdin @@ -110,8 +106,8 @@ EXAMPLES: 6. Remove all objects recursively from S3 host $ {{.HelpName}} --recursive --dangerous s3 - 7. Remove all objects older than '90' days recursively from host - $ {{.HelpName}} --recursive --dangerous --older-than=90 s3 + 7. Remove all buckets and objects older than '90' days recursively from host + $ {{.HelpName}} --recursive --dangerous --older-than 90d s3 8. Drop all incomplete uploads on 'jazz-songs' bucket. $ {{.HelpName}} --incomplete --recursive s3/jazz-songs/ @@ -179,7 +175,7 @@ func checkRmSyntax(ctx *cli.Context, encKeyDB map[string][]prefixSSEPair) { } } -func removeSingle(url string, isIncomplete bool, isFake bool, olderThan int, newerThan int, encKeyDB map[string][]prefixSSEPair) error { +func removeSingle(url string, isIncomplete bool, isFake bool, olderThan, newerThan string, encKeyDB map[string][]prefixSSEPair) error { targetAlias, targetURL, _ := mustExpandAlias(url) clnt, pErr := newClientFromAlias(targetAlias, targetURL) if pErr != nil { @@ -196,12 +192,12 @@ func removeSingle(url string, isIncomplete bool, isFake bool, olderThan int, new } // Skip objects older than older--than parameter if specified - if olderThan > 0 && isOlder(content, olderThan) { + if olderThan != "" && isOlder(content.Time, olderThan) { return nil } // Skip objects older than older--than parameter if specified - if newerThan > 0 && isNewer(content, newerThan) { + if newerThan != "" && isNewer(content.Time, newerThan) { return nil } @@ -231,7 +227,7 @@ func removeSingle(url string, isIncomplete bool, isFake bool, olderThan int, new return nil } -func removeRecursive(url string, isIncomplete bool, isFake bool, olderThan int, newerThan int, encKeyDB map[string][]prefixSSEPair) error { +func removeRecursive(url string, isIncomplete bool, isFake bool, olderThan, newerThan string, encKeyDB map[string][]prefixSSEPair) error { targetAlias, targetURL, _ := mustExpandAlias(url) clnt, pErr := newClientFromAlias(targetAlias, targetURL) if pErr != nil { @@ -258,12 +254,12 @@ func removeRecursive(url string, isIncomplete bool, isFake bool, olderThan int, urlString := content.URL.Path // Skip objects older than --older-than parameter if specified - if olderThan > 0 && isOlder(content, olderThan) { + if olderThan != "" && isOlder(content.Time, olderThan) { continue } // Skip objects newer than --newer-than parameter if specified - if newerThan > 0 && isNewer(content, newerThan) { + if newerThan != "" && isNewer(content.Time, newerThan) { continue } @@ -323,8 +319,8 @@ func mainRm(ctx *cli.Context) error { isRecursive := ctx.Bool("recursive") isFake := ctx.Bool("fake") isStdin := ctx.Bool("stdin") - olderThan := ctx.Int("older-than") - newerThan := ctx.Int("newer-than") + olderThan := ctx.String("older-than") + newerThan := ctx.String("newer-than") // Set color. console.SetColor("Remove", color.New(color.FgGreen, color.Bold)) diff --git a/cmd/utils.go b/cmd/utils.go index 71f3891b..2aad6e53 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -31,6 +31,7 @@ import ( "github.com/minio/minio-go/pkg/encrypt" "github.com/minio/mc/pkg/console" + "github.com/minio/mc/pkg/ioutils" "github.com/minio/mc/pkg/probe" ) @@ -162,15 +163,27 @@ func lineTrunc(content string, maxLen int) string { } // isOlder returns true if the passed object is older than olderRef -func isOlder(c *clientContent, olderRef int) bool { - objectAge := UTCNow().Sub(c.Time) - return objectAge < (time.Duration(olderRef) * Day) +func isOlder(ti time.Time, olderRef string) bool { + objectAge := UTCNow().Sub(ti) + var e error + var olderThan time.Duration + if olderRef != "" { + olderThan, e = ioutils.ParseDurationTime(olderRef) + fatalIf(probe.NewError(e), "Unable to parse olderThan=`"+olderRef+"`.") + } + return objectAge < olderThan } // isNewer returns true if the passed object is newer than newerRef -func isNewer(c *clientContent, newerRef int) bool { - objectAge := UTCNow().Sub(c.Time) - return objectAge > (time.Duration(newerRef) * Day) +func isNewer(ti time.Time, newerRef string) bool { + objectAge := UTCNow().Sub(ti) + var e error + var newerThan time.Duration + if newerRef != "" { + newerThan, e = ioutils.ParseDurationTime(newerRef) + fatalIf(probe.NewError(e), "Unable to parse newerThan=`"+newerRef+"`.") + } + return objectAge >= newerThan } // getLookupType returns the minio.BucketLookupType for lookup diff --git a/docs/minio-client-complete-guide.md b/docs/minio-client-complete-guide.md index c821191b..0543a88e 100644 --- a/docs/minio-client-complete-guide.md +++ b/docs/minio-client-complete-guide.md @@ -531,8 +531,8 @@ FLAGS: --incomplete, -I remove incomplete uploads --fake perform a fake remove operation --stdin read object names from STDIN - --older-than value remove objects older than N days (default: 0) - --newer-than value remove objects newer than N days (default: 0) + --older-than value remove objects older than L days, M hours and N minutes LNM[d|h|m]. (default: 0) + --newer-than value remove objects newer than L days, M hours and N minutes LNM[d|h|m]. (default: 0) --encrypt-key value encrypt/decrypt objects (using server-side encryption with customer provided keys) --help, -h show help @@ -567,10 +567,10 @@ Removing `play/mybucket/otherobject.txt`. mc rm --incomplete play/mybucket/myobject.1gig Removing `play/mybucket/myobject.1gig`. ``` -*Example: Remove object and output a message only if the object is created older than one day. Otherwise, the command stays quiet and nothing is printed out.* +*Example: Remove object and output a message only if the object is created older than 1 day, 2 hours and 30 minutes. Otherwise, the command stays quiet and nothing is printed out.* ```sh -mc rm -r --force --older-than=1 myminio/mybucket +mc rm -r --force --older-than 1d2h30m myminio/mybucket Removing `myminio/mybucket/dayOld1.txt`. Removing `myminio/mybucket/dayOld2.txt`. Removing `myminio/mybucket/dayOld3.txt`. @@ -708,8 +708,8 @@ FLAGS: --exec value spawn an external process for each matching object (see FORMAT) --ignore value exclude objects matching the wildcard pattern --name value find object names matching wildcard pattern - --newer value match all objects newer than specified time in units (see UNITS) - --older value match all objects older than specified time in units (see UNITS) + --newer value match all objects newer than specified time L days, M hours and N minutes + --older value match all objects older than specified time L days, M hours and N minutes --path value match directory names matching wildcard pattern --print value print in custom format to STDOUT (see FORMAT) --regex value match directory and object name with PCRE regex pattern diff --git a/pkg/ioutils/format.go b/pkg/ioutils/format.go new file mode 100644 index 00000000..7e167e6b --- /dev/null +++ b/pkg/ioutils/format.go @@ -0,0 +1,219 @@ +/* + * Minio Cloud Storage, (C) 2018 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 ioutils + +import ( + "errors" + "time" +) + +// A Duration represents the elapsed time between two instants +// as an int64 nanosecond count. The representation limits the +// largest representable duration to approximately 290 years. +type Duration int64 + +// never printed +var errLeadingInt = errors.New("time: bad [0-9]*") + +// Common durations. There is no definition for units of Day or larger +// to avoid confusion across daylight savings time zone transitions. +// +// To count the number of units in a Duration, divide: +// second := time.Second +// fmt.Print(int64(second/time.Millisecond)) // prints 1000 +// +// To convert an integer number of units to a Duration, multiply: +// seconds := 10 +// fmt.Print(time.Duration(seconds)*time.Second) // prints 10s +// +const ( + Nanosecond Duration = 1 + Microsecond = 1000 * Nanosecond + Millisecond = 1000 * Microsecond + Second = 1000 * Millisecond + Minute = 60 * Second + Hour = 60 * Minute + Day = 24 * Hour +) + +var unitMap = map[string]int64{ + "ns": int64(Nanosecond), + "us": int64(Microsecond), + "ms": int64(Millisecond), + "s": int64(Second), + "m": int64(Minute), + "h": int64(Hour), + "d": int64(Day), +} + +// ParseDurationTime parses a duration string in +// the form "10d4h3m". +// A duration string is a possibly signed sequence of +// decimal numbers, each with optional fraction and a unit suffix, +// such as "300ms", "-1.5h" or "2h45m". +// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h", "d". +// It add the days functionality to time.ParseDuration(). +func ParseDurationTime(s string) (time.Duration, error) { + // [-+]?([0-9]*(\.[0-9]*)?[a-z]+)+ + orig := s + var d int64 + neg := false + + // Consume [-+]? + if s != "" { + c := s[0] + if c == '-' || c == '+' { + neg = c == '-' + s = s[1:] + } + } + // Special case: if all that is left is "0", this is zero. + if s == "0" { + return 0, nil + } + if s == "" { + return 0, errors.New("time: invalid duration " + orig) + } + for s != "" { + var ( + v, f int64 // integers before, after decimal point + scale float64 = 1 // value = v + f/scale + ) + + var err error + + // The next character must be [0-9.] + if !(s[0] == '.' || '0' <= s[0] && s[0] <= '9') { + return 0, errors.New("time: invalid duration " + orig) + } + // Consume [0-9]* + pl := len(s) + v, s, err = leadingInt(s) + if err != nil { + return 0, errors.New("time: invalid duration " + orig) + } + pre := pl != len(s) // whether we consumed anything before a period + + // Consume (\.[0-9]*)? + post := false + if s != "" && s[0] == '.' { + s = s[1:] + pl := len(s) + f, scale, s = leadingFraction(s) + post = pl != len(s) + } + if !pre && !post { + // no digits (e.g. ".s" or "-.s") + return 0, errors.New("time: invalid duration " + orig) + } + + // Consume unit. + i := 0 + for ; i < len(s); i++ { + c := s[i] + if c == '.' || '0' <= c && c <= '9' { + break + } + } + if i == 0 { + return 0, errors.New("time: missing unit in duration " + orig + ". Should be of days, hours and minutes format like 7d4h20m ") + } + u := s[:i] + s = s[i:] + unit, ok := unitMap[u] + if !ok { + return 0, errors.New("time: unknown unit " + u + " in duration " + orig) + } + if v > (1<<63-1)/unit { + // overflow + return 0, errors.New("time: invalid duration " + orig) + } + v *= unit + if f > 0 { + // float64 is needed to be nanosecond accurate for fractions of hours. + // v >= 0 && (f*unit/scale) <= 3.6e+12 (ns/h, h is the largest unit) + v += int64(float64(f) * (float64(unit) / scale)) + if v < 0 { + // overflow + return 0, errors.New("time: invalid duration " + orig) + } + } + d += v + if d < 0 { + // overflow + return 0, errors.New("time: invalid duration " + orig) + } + } + + if neg { + d = -d + } + + return time.Duration(d), nil +} + +// leadingInt consumes the leading [0-9]* from s. +func leadingInt(s string) (x int64, rem string, err error) { + i := 0 + for ; i < len(s); i++ { + c := s[i] + if c < '0' || c > '9' { + break + } + if x > (1<<63-1)/10 { + // overflow + return 0, "", errLeadingInt + } + x = x*10 + int64(c) - '0' + if x < 0 { + // overflow + return 0, "", errLeadingInt + } + } + return x, s[i:], nil +} + +// leadingFraction consumes the leading [0-9]* from s. +// It is used only for fractions, so does not return an error on overflow, +// it just stops accumulating precision. +func leadingFraction(s string) (x int64, scale float64, rem string) { + i := 0 + scale = 1 + overflow := false + for ; i < len(s); i++ { + c := s[i] + if c < '0' || c > '9' { + break + } + if overflow { + continue + } + if x > (1<<63-1)/10 { + // It's possible for overflow to give a positive number, so take care. + overflow = true + continue + } + y := x*10 + int64(c) - '0' + if y < 0 { + overflow = true + continue + } + x = y + scale *= 10 + } + return x, scale, s[i:] +} diff --git a/pkg/ioutils/ioutils_test.go b/pkg/ioutils/ioutils_test.go index beefe5a0..ebe3450e 100644 --- a/pkg/ioutils/ioutils_test.go +++ b/pkg/ioutils/ioutils_test.go @@ -20,6 +20,7 @@ import ( "io/ioutil" "os" "testing" + "time" "github.com/minio/mc/pkg/ioutils" @@ -41,3 +42,41 @@ func (s *MySuite) TestIoutils(c *C) { c.Assert(err, IsNil) c.Assert(status, Equals, true) } + +// Test for ParseDurationTime. Validates the returned value +// for given time value in days, hours and minute format. +func TestParseDurationTime(t *testing.T) { + + testCases := []struct { + timeValue string + expected time.Duration + err string + }{ + // Test 1: empty string as input + {"", 0, "time: invalid duration "}, + // Test 2: Input string contains 4 day, 10 hour and 3 minutes + {"4d10h3m", 381780000000000, ""}, + // Test 3: Input string contains 10 day and 3 hours + {"10d3h", 874800000000000, ""}, + // Test 4: Input string contains minutes and days + {"3m7d", 604980000000000, ""}, + // Test 5: Input string contains unknown unit + {"4a3d", 0, "time: unknown unit a in duration 4a3d"}, + // Test 6: Input string contains fractional day + {"1.5d", 129600000000000, ""}, + // Test 6: Input string contains fractional day and hour + {"2.5d1.5h", 221400000000000, ""}, + // Test 7: Input string contains fractional day , hour and minute + {"2.5d1.5h3.5m", 221610000000000, ""}, + } + + for _, testCase := range testCases { + myVal, err := ioutils.ParseDurationTime(testCase.timeValue) + if err != nil && err.Error() != testCase.err { + t.Error() + } + if myVal != testCase.expected { + t.Error() + } + } +}