Skip to content

Commit 839e66b

Browse files
author
Christian Hergert
committed
examples: use fopen() for stream in mongoc-dump.
1 parent cb7e2dd commit 839e66b

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

examples/mongoc-dump.c

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,11 @@ mongoc_dump_collection (mongoc_client_t *client,
4949
{
5050
mongoc_collection_t *col;
5151
mongoc_cursor_t *cursor;
52-
mongoc_stream_t *stream;
5352
mongoc_iovec_t iov;
5453
const bson_t *doc;
5554
bson_error_t error;
5655
bson_t query = BSON_INITIALIZER;
56+
FILE *stream;
5757
char *path;
5858
int ret = EXIT_SUCCESS;
5959

@@ -68,7 +68,7 @@ mongoc_dump_collection (mongoc_client_t *client,
6868
}
6969
#endif
7070

71-
stream = mongoc_stream_file_new_for_path (path, O_RDWR|O_CREAT, 0664);
71+
stream = fopen (path, "w");
7272
if (!stream) {
7373
fprintf (stderr, "Failed to open \"%s\", aborting.\n", path);
7474
exit (EXIT_FAILURE);
@@ -78,28 +78,23 @@ mongoc_dump_collection (mongoc_client_t *client,
7878
cursor = mongoc_collection_find (col, MONGOC_QUERY_NONE, 0, 0, 0,
7979
&query, NULL, NULL);
8080

81-
while (mongoc_cursor_more (cursor) &&
82-
!mongoc_cursor_error (cursor, &error)) {
83-
if (mongoc_cursor_next (cursor, &doc)) {
84-
iov.iov_len = doc->len;
85-
iov.iov_base = (void *)bson_get_data (doc);
86-
if (doc->len != mongoc_stream_writev (stream, &iov, 1, 0)) {
87-
fprintf (stderr, "Failed to write %u bytes to %s\n",
88-
doc->len, path);
89-
ret = EXIT_FAILURE;
90-
goto cleanup;
91-
}
81+
while (mongoc_cursor_next (cursor, &doc)) {
82+
if (BSON_UNLIKELY (doc->len != fwrite (bson_get_data (doc), 1, doc->len, stream))) {
83+
fprintf (stderr, "Failed to write %u bytes to %s\n",
84+
doc->len, path);
85+
ret = EXIT_FAILURE;
86+
goto cleanup;
9287
}
9388
}
9489

9590
if (mongoc_cursor_error (cursor, &error)) {
96-
fprintf (stderr, "%s\n", error.message);
91+
fprintf (stderr, "ERROR: %s\n", error.message);
9792
ret = EXIT_FAILURE;
9893
}
9994

10095
cleanup:
10196
bson_free (path);
102-
mongoc_stream_destroy (stream);
97+
fclose (stream);
10398
mongoc_cursor_destroy (cursor);
10499
mongoc_collection_destroy (col);
105100

@@ -245,11 +240,14 @@ main (int argc,
245240
}
246241
}
247242

243+
#if 0
248244
uri = bson_strdup_printf ("mongodb://%s:%hu/%s?ssl=%s",
249245
host,
250246
port,
251247
database ? database : "",
252248
ssl ? "true" : "false");
249+
#endif
250+
uri = NULL;
253251

254252
if (!(client = mongoc_client_new (uri))) {
255253
fprintf (stderr, "Invalid connection URI: %s\n", uri);

0 commit comments

Comments
 (0)