Skip to content

Fix: CDRIVER-4050: Set the correct uploadDate on GridFS files #818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/libmongoc/src/mongoc/mongoc-gridfs-bucket-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include "mongoc-stream-gridfs-download-private.h"
#include "mongoc-stream-gridfs-upload-private.h"
#include "mongoc-collection-private.h"
#include "mongoc-util-private.h"

/* Returns the minimum of two numbers */
static size_t
Expand Down Expand Up @@ -369,7 +370,9 @@ _mongoc_gridfs_bucket_file_writev (mongoc_gridfs_bucket_file_t *file,
#ifndef _WIN32
(char *)
#endif
iov[i].iov_base + written_this_iov,
iov[i]
.iov_base +
written_this_iov,
to_write);
file->in_buffer += to_write;
written_this_iov += to_write;
Expand Down Expand Up @@ -420,11 +423,13 @@ _mongoc_gridfs_bucket_file_readv (mongoc_gridfs_bucket_file_t *file,
to_read = _mongoc_min (bytes_available, space_available);
memcpy (
#ifndef _WIN32
(char *)
(char *)
#endif
iov[i].iov_base + read_this_iov,
file->buffer + file->bytes_read,
to_read);
iov[i]
.iov_base +
read_this_iov,
file->buffer + file->bytes_read,
to_read);
file->bytes_read += to_read;
read_this_iov += to_read;
total += to_read;
Expand Down Expand Up @@ -491,7 +496,7 @@ _mongoc_gridfs_bucket_file_save (mongoc_gridfs_bucket_file_t *file)
BSON_APPEND_VALUE (&new_doc, "_id", file->file_id);
BSON_APPEND_INT64 (&new_doc, "length", file->length);
BSON_APPEND_INT32 (&new_doc, "chunkSize", file->chunk_size);
BSON_APPEND_DATE_TIME (&new_doc, "uploadDate", bson_get_monotonic_time ());
BSON_APPEND_DATE_TIME (&new_doc, "uploadDate", _mongoc_get_real_time_ms ());
BSON_APPEND_UTF8 (&new_doc, "filename", file->filename);
if (file->metadata) {
BSON_APPEND_DOCUMENT (&new_doc, "metadata", file->metadata);
Expand Down
3 changes: 2 additions & 1 deletion src/libmongoc/src/mongoc/mongoc-gridfs-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "mongoc-gridfs-file-page-private.h"
#include "mongoc-iovec.h"
#include "mongoc-trace-private.h"
#include "mongoc-util-private.h"
#include "mongoc-error.h"

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

file->upload_date = ((int64_t) time (NULL)) * 1000;
file->upload_date = _mongoc_get_real_time_ms ();

if (opt->md5) {
file->md5 = bson_strdup (opt->md5);
Expand Down
4 changes: 4 additions & 0 deletions src/libmongoc/src/mongoc/mongoc-util-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ _mongoc_hex_md5 (const char *input);
void
_mongoc_usleep (int64_t usec);

/* Get the current time as a number of milliseconds since the Unix Epoch. */
int64_t
_mongoc_get_real_time_ms (void);

const char *
_mongoc_get_command_name (const bson_t *command);

Expand Down
13 changes: 12 additions & 1 deletion src/libmongoc/src/mongoc/mongoc-util.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ _mongoc_rand_simple (unsigned int *seed)

err = rand_s (&ret);
if (0 != err) {
MONGOC_ERROR ("rand_s failed: %s", strerror(err));
MONGOC_ERROR ("rand_s failed: %s", strerror (err));
}

return (int) ret;
Expand Down Expand Up @@ -100,6 +100,17 @@ _mongoc_usleep (int64_t usec)
#endif
}

int64_t
_mongoc_get_real_time_ms (void)
{
struct timeval tv;
const bool rc = bson_gettimeofday (&tv);
if (rc != 0) {
return -1;
}
return tv.tv_sec * (int64_t) 1000 + tv.tv_usec / (int64_t) 1000;
}


const char *
_mongoc_get_command_name (const bson_t *command)
Expand Down
4 changes: 2 additions & 2 deletions src/libmongoc/tests/test-mongoc-gridfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ test_create_from_stream (void)

mongoc_gridfs_drop (gridfs, &error);

start = ((int64_t) time (NULL)) * 1000;
start = _mongoc_get_real_time_ms ();
stream =
mongoc_stream_file_new_for_path (BINARY_DIR "/gridfs.dat", O_RDONLY, 0);
ASSERT_OR_PRINT_ERRNO (stream, errno);
Expand All @@ -582,7 +582,7 @@ test_create_from_stream (void)
ASSERT (file);
ASSERT (mongoc_gridfs_file_save (file));

now = ((int64_t) time (NULL)) * 1000;
now = _mongoc_get_real_time_ms ();

filter = tmp_bson (NULL);
BSON_APPEND_VALUE (filter, "_id", mongoc_gridfs_file_get_id (file));
Expand Down