Skip to content

Commit c6b1ed0

Browse files
Fix: CDRIVER-4050: Set the correct uploadDate on GridFS files (#818)
* Fix: CDRIVER-4050: Set the correct uploadDate on GridFS files Previously used the time from bson_get_monotonic_time(), which is incorrect on most systems, as it is usually realtive to the system uptime and is stored as microseconds, whereas uploadDate should be stored as number of milliseconds since the Unix Epoch. * Propagate failure from bson_gettimeofday * Make get_real_time_ms a private API (for now) * Fix inverted error case and tests on file uploadDate * Fix test cases that check file uploadDate to use a more precise time * Fix integer overflow in _mongoc_get_real_time_ms on MSVC
1 parent d8684be commit c6b1ed0

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

src/libmongoc/src/mongoc/mongoc-gridfs-bucket-file.c

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "mongoc-stream-gridfs-download-private.h"
2222
#include "mongoc-stream-gridfs-upload-private.h"
2323
#include "mongoc-collection-private.h"
24+
#include "mongoc-util-private.h"
2425

2526
/* Returns the minimum of two numbers */
2627
static size_t
@@ -369,7 +370,9 @@ _mongoc_gridfs_bucket_file_writev (mongoc_gridfs_bucket_file_t *file,
369370
#ifndef _WIN32
370371
(char *)
371372
#endif
372-
iov[i].iov_base + written_this_iov,
373+
iov[i]
374+
.iov_base +
375+
written_this_iov,
373376
to_write);
374377
file->in_buffer += to_write;
375378
written_this_iov += to_write;
@@ -420,11 +423,13 @@ _mongoc_gridfs_bucket_file_readv (mongoc_gridfs_bucket_file_t *file,
420423
to_read = _mongoc_min (bytes_available, space_available);
421424
memcpy (
422425
#ifndef _WIN32
423-
(char *)
426+
(char *)
424427
#endif
425-
iov[i].iov_base + read_this_iov,
426-
file->buffer + file->bytes_read,
427-
to_read);
428+
iov[i]
429+
.iov_base +
430+
read_this_iov,
431+
file->buffer + file->bytes_read,
432+
to_read);
428433
file->bytes_read += to_read;
429434
read_this_iov += to_read;
430435
total += to_read;
@@ -491,7 +496,7 @@ _mongoc_gridfs_bucket_file_save (mongoc_gridfs_bucket_file_t *file)
491496
BSON_APPEND_VALUE (&new_doc, "_id", file->file_id);
492497
BSON_APPEND_INT64 (&new_doc, "length", file->length);
493498
BSON_APPEND_INT32 (&new_doc, "chunkSize", file->chunk_size);
494-
BSON_APPEND_DATE_TIME (&new_doc, "uploadDate", bson_get_monotonic_time ());
499+
BSON_APPEND_DATE_TIME (&new_doc, "uploadDate", _mongoc_get_real_time_ms ());
495500
BSON_APPEND_UTF8 (&new_doc, "filename", file->filename);
496501
if (file->metadata) {
497502
BSON_APPEND_DOCUMENT (&new_doc, "metadata", file->metadata);

src/libmongoc/src/mongoc/mongoc-gridfs-file.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "mongoc-gridfs-file-page-private.h"
3434
#include "mongoc-iovec.h"
3535
#include "mongoc-trace-private.h"
36+
#include "mongoc-util-private.h"
3637
#include "mongoc-error.h"
3738

3839
static bool
@@ -337,7 +338,7 @@ _mongoc_gridfs_file_new (mongoc_gridfs_t *gridfs, mongoc_gridfs_file_opt_t *opt)
337338
file->files_id.value_type = BSON_TYPE_OID;
338339
bson_oid_init (&file->files_id.value.v_oid, NULL);
339340

340-
file->upload_date = ((int64_t) time (NULL)) * 1000;
341+
file->upload_date = _mongoc_get_real_time_ms ();
341342

342343
if (opt->md5) {
343344
file->md5 = bson_strdup (opt->md5);

src/libmongoc/src/mongoc/mongoc-util-private.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ _mongoc_hex_md5 (const char *input);
7575
void
7676
_mongoc_usleep (int64_t usec);
7777

78+
/* Get the current time as a number of milliseconds since the Unix Epoch. */
79+
int64_t
80+
_mongoc_get_real_time_ms (void);
81+
7882
const char *
7983
_mongoc_get_command_name (const bson_t *command);
8084

src/libmongoc/src/mongoc/mongoc-util.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ _mongoc_rand_simple (unsigned int *seed)
4848

4949
err = rand_s (&ret);
5050
if (0 != err) {
51-
MONGOC_ERROR ("rand_s failed: %s", strerror(err));
51+
MONGOC_ERROR ("rand_s failed: %s", strerror (err));
5252
}
5353

5454
return (int) ret;
@@ -100,6 +100,17 @@ _mongoc_usleep (int64_t usec)
100100
#endif
101101
}
102102

103+
int64_t
104+
_mongoc_get_real_time_ms (void)
105+
{
106+
struct timeval tv;
107+
const bool rc = bson_gettimeofday (&tv);
108+
if (rc != 0) {
109+
return -1;
110+
}
111+
return tv.tv_sec * (int64_t) 1000 + tv.tv_usec / (int64_t) 1000;
112+
}
113+
103114

104115
const char *
105116
_mongoc_get_command_name (const bson_t *command)

src/libmongoc/tests/test-mongoc-gridfs.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ test_create_from_stream (void)
572572

573573
mongoc_gridfs_drop (gridfs, &error);
574574

575-
start = ((int64_t) time (NULL)) * 1000;
575+
start = _mongoc_get_real_time_ms ();
576576
stream =
577577
mongoc_stream_file_new_for_path (BINARY_DIR "/gridfs.dat", O_RDONLY, 0);
578578
ASSERT_OR_PRINT_ERRNO (stream, errno);
@@ -582,7 +582,7 @@ test_create_from_stream (void)
582582
ASSERT (file);
583583
ASSERT (mongoc_gridfs_file_save (file));
584584

585-
now = ((int64_t) time (NULL)) * 1000;
585+
now = _mongoc_get_real_time_ms ();
586586

587587
filter = tmp_bson (NULL);
588588
BSON_APPEND_VALUE (filter, "_id", mongoc_gridfs_file_get_id (file));

0 commit comments

Comments
 (0)