Browse Source

fix setting of missing time stamps while reading.

pull/3/head
Herbert Poul 4 years ago
parent
commit
35a8889d2a
  1. 27
      lib/src/kdbx_times.dart
  2. 6
      lib/src/kdbx_xml.dart

27
lib/src/kdbx_times.dart

@ -5,6 +5,10 @@ import 'package:kdbx/src/kdbx_xml.dart';
import 'package:quiver/iterables.dart'; import 'package:quiver/iterables.dart';
import 'package:xml/xml.dart'; import 'package:xml/xml.dart';
import 'package:logging/logging.dart';
final _logger = Logger('kdbx_times');
class KdbxTimes extends KdbxNode implements KdbxNodeContext { class KdbxTimes extends KdbxNode implements KdbxNodeContext {
KdbxTimes.create(this.ctx) : super.create('Times') { KdbxTimes.create(this.ctx) : super.create('Times') {
final now = clock.now().toUtc(); final now = clock.now().toUtc();
@ -16,15 +20,32 @@ class KdbxTimes extends KdbxNode implements KdbxNodeContext {
usageCount.set(0); usageCount.set(0);
locationChanged.set(now); locationChanged.set(now);
} }
KdbxTimes.read(XmlElement node, this.ctx) : super.read(node); KdbxTimes.read(XmlElement node, this.ctx) : super.read(node) {
// backward compatibility - there was a bug setting/reading
// modification, lastAccess and expiryTime. Make sure they are defined.
final checkDates = {
lastModificationTime: () => creationTime.get() ?? clock.now().toUtc(),
lastAccessTime: () => lastModificationTime.get() ?? clock.now().toUtc(),
expiryTime: () {
expires.set(false);
return clock.now().toUtc();
},
};
for (final check in checkDates.entries) {
if (check.key.get() == null) {
final val = check.value();
_logger.warning('${check.key.name} was not defined. setting to $val');
check.key.set(val);
}
}
}
@override @override
final KdbxReadWriteContext ctx; final KdbxReadWriteContext ctx;
DateTimeUtcNode get creationTime => DateTimeUtcNode(this, 'CreationTime'); DateTimeUtcNode get creationTime => DateTimeUtcNode(this, 'CreationTime');
DateTimeUtcNode get lastModificationTime => DateTimeUtcNode get lastModificationTime =>
DateTimeUtcNode(this, 'LastModificationTime', DateTimeUtcNode(this, 'LastModificationTime');
defaultValue: () => creationTime.get() ?? DateTimeUtcNode.minDate);
DateTimeUtcNode get lastAccessTime => DateTimeUtcNode(this, 'LastAccessTime'); DateTimeUtcNode get lastAccessTime => DateTimeUtcNode(this, 'LastAccessTime');
DateTimeUtcNode get expiryTime => DateTimeUtcNode(this, 'ExpiryTime'); DateTimeUtcNode get expiryTime => DateTimeUtcNode(this, 'ExpiryTime');
BooleanNode get expires => BooleanNode(this, 'Expires'); BooleanNode get expires => BooleanNode(this, 'Expires');

6
lib/src/kdbx_xml.dart

@ -231,11 +231,9 @@ class BooleanNode extends KdbxSubTextNode<bool> {
} }
class DateTimeUtcNode extends KdbxSubTextNode<DateTime> { class DateTimeUtcNode extends KdbxSubTextNode<DateTime> {
DateTimeUtcNode(KdbxNodeContext node, String name, {this.defaultValue}) DateTimeUtcNode(KdbxNodeContext node, String name) : super(node, name);
: super(node, name);
static const EpochSeconds = 62135596800; static const EpochSeconds = 62135596800;
final DateTime Function() defaultValue;
KdbxReadWriteContext get _ctx => (node as KdbxNodeContext).ctx; KdbxReadWriteContext get _ctx => (node as KdbxNodeContext).ctx;
static final minDate = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true); static final minDate = DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
@ -250,7 +248,7 @@ class DateTimeUtcNode extends KdbxSubTextNode<DateTime> {
@override @override
DateTime decode(String value) { DateTime decode(String value) {
if (value == null) { if (value == null) {
return defaultValue?.call(); return null;
} }
if (value.contains(':')) { if (value.contains(':')) {
return DateTime.parse(value); return DateTime.parse(value);

Loading…
Cancel
Save