Files
kopia/internal/timestampmeta/timestampmeta.go
Jarek Kowalski 7401684e71 blob: replaced blob.Storage.SetTime() method with blob.PutOptions.SetTime (#1595)
* sharded: plumbed through blob.PutOptions

* blob: removed blob.Storage.SetTime() method

This was only used for `kopia repo sync-to` and got replaced with
an equivalent blob.PutOptions.SetTime, which wehn set to non-zero time
will attempt to set the modification time on a file.

Since some providers don't support changing modification time, we
are able to emulate it using per-blob metadata (on B2, Azure and GCS),
sadly S3 is still unsupported, because it does not support returning
metadata in list results.

Also added PutOptions.GetTime, which when set to not nil, will
populate the provided variable with actual time that got assigned
to the blob.

Added tests that verify that each provider supports GetTime
and SetTime according to this spec.

* blob: additional test coverage for filesystem storage

* blob: added PutBlobAndGetMetadata() helper and used where appropriate

* fixed test failures

* pr feedback

* Update repo/blob/azure/azure_storage.go

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>

* Update repo/blob/filesystem/filesystem_storage.go

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>

* Update repo/blob/filesystem/filesystem_storage.go

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>

* blobtesting: fixed object_locking_map.go

* blobtesting: removed SetTime from ObjectLockingMap

Co-authored-by: Shikhar Mall <mall.shikhar.in@gmail.com>
2021-12-18 14:00:20 -08:00

31 lines
824 B
Go

// Package timestampmeta provides utilities for preserving timestamps
// using per-blob key-value-pairs (metadata, tags, etc.)
package timestampmeta
import (
"strconv"
"time"
)
// ToMap returns a map containing single entry representing the provided time or nil map
// if the time is zero. The key-value pair map should be stored alongside the blob.
func ToMap(t time.Time, mapKey string) map[string]string {
if t.IsZero() {
return nil
}
return map[string]string{
mapKey: strconv.FormatInt(t.UnixNano(), 10), // nolint:gomnd
}
}
// FromValue attempts to convert the provided value stored in metadata into time.Time.
func FromValue(v string) (t time.Time, ok bool) {
nanos, err := strconv.ParseInt(v, 10, 64) // nolint:gomnd
if err != nil {
return time.Time{}, false
}
return time.Unix(0, nanos), true
}