Skip to content

Commit a3a86b7

Browse files
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.
1 parent 36189eb commit a3a86b7

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

src/libbson/src/bson/bson-clock.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,29 @@ bson_gettimeofday (struct timeval *tv) /* OUT */
9898
}
9999

100100

101+
/*
102+
*--------------------------------------------------------------------------
103+
*
104+
* bson_get_real_time_ms --
105+
*
106+
* Returns:
107+
* The current number of milliseconds that have elapsed since the
108+
* Unix Epoch.
109+
*
110+
* Side effects:
111+
* None.
112+
*
113+
*--------------------------------------------------------------------------
114+
*/
115+
int64_t
116+
bson_get_real_time_ms (void)
117+
{
118+
struct timeval tv;
119+
const bool ok = bson_gettimeofday (&tv);
120+
return tv.tv_sec * 1000 + tv.tv_usec / 1000;
121+
}
122+
123+
101124
/*
102125
*--------------------------------------------------------------------------
103126
*

src/libbson/src/bson/bson-clock.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ BSON_EXPORT (int64_t)
3333
bson_get_monotonic_time (void);
3434
BSON_EXPORT (int)
3535
bson_gettimeofday (struct timeval *tv);
36+
BSON_EXPORT (int64_t)
37+
bson_get_real_time_ms (void);
3638

3739

3840
BSON_END_DECLS

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ _mongoc_gridfs_bucket_file_save (mongoc_gridfs_bucket_file_t *file)
491491
BSON_APPEND_VALUE (&new_doc, "_id", file->file_id);
492492
BSON_APPEND_INT64 (&new_doc, "length", file->length);
493493
BSON_APPEND_INT32 (&new_doc, "chunkSize", file->chunk_size);
494-
BSON_APPEND_DATE_TIME (&new_doc, "uploadDate", bson_get_monotonic_time ());
494+
BSON_APPEND_DATE_TIME (&new_doc, "uploadDate", bson_get_real_time_ms ());
495495
BSON_APPEND_UTF8 (&new_doc, "filename", file->filename);
496496
if (file->metadata) {
497497
BSON_APPEND_DOCUMENT (&new_doc, "metadata", file->metadata);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ _mongoc_gridfs_file_new (mongoc_gridfs_t *gridfs, mongoc_gridfs_file_opt_t *opt)
337337
file->files_id.value_type = BSON_TYPE_OID;
338338
bson_oid_init (&file->files_id.value.v_oid, NULL);
339339

340-
file->upload_date = ((int64_t) time (NULL)) * 1000;
340+
file->upload_date = bson_get_real_time_ms ();
341341

342342
if (opt->md5) {
343343
file->md5 = bson_strdup (opt->md5);

0 commit comments

Comments
 (0)