forked from mongodb/mongo-php-driver-legacy
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgridfs.c
1221 lines (965 loc) · 34.1 KB
/
gridfs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//gridfs.c
/**
* Copyright 2009-2011 10gen, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <php.h>
#ifdef WIN32
# ifndef int64_t
typedef __int64 int64_t;
# endif
#endif
#include <zend_exceptions.h>
#include "php_mongo.h"
#include "gridfs.h"
#include "collection.h"
#include "cursor.h"
#include "mongo_types.h"
#include "db.h"
typedef struct {
FILE *file;
int fd; /* underlying file descriptor */
unsigned is_process_pipe:1; /* use pclose instead of fclose */
unsigned is_pipe:1; /* don't try and seek */
unsigned cached_fstat:1; /* sb is valid */
unsigned _reserved:29;
int lock_flag; /* stores the lock state */
char *temp_file_name; /* if non-null, this is the path to a temporary file that
* is to be deleted when the stream is closed */
#if HAVE_FLUSHIO
char last_op;
#endif
#if HAVE_MMAP
char *last_mapped_addr;
size_t last_mapped_len;
#endif
#ifdef PHP_WIN32
char *last_mapped_addr;
HANDLE file_mapping;
#endif
struct stat sb;
} php_stdio_stream_data;
extern zend_class_entry *mongo_ce_DB,
*mongo_ce_Collection,
*mongo_ce_Cursor,
*mongo_ce_Exception,
*mongo_ce_GridFSException,
*mongo_ce_Id,
*mongo_ce_Date,
*mongo_ce_BinData;
ZEND_EXTERN_MODULE_GLOBALS(mongo);
zend_class_entry *mongo_ce_GridFS = NULL,
*mongo_ce_GridFSFile = NULL,
*mongo_ce_GridFSCursor = NULL;
typedef int (*apply_copy_func_t)(void *to, char *from, int len);
static int copy_bytes(void *to, char *from, int len);
static int copy_file(void *to, char *from, int len);
static void add_md5(zval *zfile, zval *zid, mongo_collection *c TSRMLS_DC);
static int apply_to_cursor(zval *cursor, apply_copy_func_t apply_copy_func, void *to TSRMLS_DC);
static int setup_file(FILE *fpp, char *filename TSRMLS_DC);
static int get_chunk_size(zval *array TSRMLS_DC);
static zval* setup_extra(zval *zfile, zval *extra TSRMLS_DC);
static int setup_file_fields(zval *zfile, char *filename, int size TSRMLS_DC);
static int insert_chunk(zval *chunks, zval *zid, int chunk_num, char *buf, int chunk_size, zval *options TSRMLS_DC);
static void ensure_gridfs_index(zval *return_value, zval *this_ptr TSRMLS_DC);
php_stream * gridfs_stream_init(zval * file_object);
PHP_METHOD(MongoGridFS, __construct) {
zval *zdb, *files = 0, *chunks = 0, *zchunks;
// chunks is deprecated
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|zz", &zdb, mongo_ce_DB, &files, &chunks) == FAILURE) {
return;
}
if (!files && !chunks) {
MAKE_STD_ZVAL(files);
ZVAL_STRING(files, "fs.files", 1);
MAKE_STD_ZVAL(chunks);
ZVAL_STRING(chunks, "fs.chunks", 1);
}
else {
zval *temp_file;
char *temp;
if (Z_TYPE_P(files) != IS_STRING || Z_STRLEN_P(files) == 0 ) {
#if ZEND_MODULE_API_NO >= 20060613
zend_throw_exception_ex(zend_exception_get_default(TSRMLS_C), 0
TSRMLS_CC,
"MongoGridFS::__construct(): invalid prefix");
#else
zend_throw_exception_ex(zend_exception_get_default(), 0 TSRMLS_CC,
"MongoGridFS::__construct(): invalid prefix");
#endif /* ZEND_MODULE_API_NO >= 20060613 */
return;
}
MAKE_STD_ZVAL(chunks);
spprintf(&temp, 0, "%s.chunks", Z_STRVAL_P(files));
ZVAL_STRING(chunks, temp, 0);
MAKE_STD_ZVAL(temp_file);
spprintf(&temp, 0, "%s.files", Z_STRVAL_P(files));
ZVAL_STRING(temp_file, temp, 0);
files = temp_file;
}
// create files collection
MONGO_METHOD2(MongoCollection, __construct, return_value, getThis(), zdb, files);
// create chunks collection
MAKE_STD_ZVAL(zchunks);
object_init_ex(zchunks, mongo_ce_Collection);
MONGO_METHOD2(MongoCollection, __construct, return_value, zchunks, zdb, chunks);
// add chunks collection as a property
zend_update_property(mongo_ce_GridFS, getThis(), "chunks", strlen("chunks"), zchunks TSRMLS_CC);
zend_update_property(mongo_ce_GridFS, getThis(), "filesName", strlen("filesName"), files TSRMLS_CC);
zend_update_property(mongo_ce_GridFS, getThis(), "chunksName", strlen("chunksName"), chunks TSRMLS_CC);
// cleanup
zval_ptr_dtor(&zchunks);
zval_ptr_dtor(&files);
zval_ptr_dtor(&chunks);
}
static void ensure_gridfs_index(zval *return_value, zval *this_ptr TSRMLS_DC) {
zval *index, *options;
// ensure index on chunks.n
MAKE_STD_ZVAL(index);
array_init(index);
add_assoc_long(index, "files_id", 1);
add_assoc_long(index, "n", 1);
MAKE_STD_ZVAL(options);
array_init(options);
add_assoc_bool(options, "unique", 1);
add_assoc_bool(options, "dropDups", 1);
MONGO_METHOD2(MongoCollection, ensureIndex, return_value, getThis(), index, options);
zval_ptr_dtor(&index);
zval_ptr_dtor(&options);
}
PHP_METHOD(MongoGridFS, drop) {
zval *temp;
zval *zchunks = zend_read_property(mongo_ce_GridFS, getThis(), "chunks", strlen("chunks"), NOISY TSRMLS_CC);
MAKE_STD_ZVAL(temp);
MONGO_METHOD(MongoCollection, drop, temp, zchunks);
zval_ptr_dtor(&temp);
MONGO_METHOD(MongoCollection, drop, return_value, getThis());
}
PHP_METHOD(MongoGridFS, find) {
zval temp;
zval *zquery = 0, *zfields = 0;
mongo_collection *c;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &zquery, &zfields) == FAILURE) {
return;
}
if (!zquery) {
MAKE_STD_ZVAL(zquery);
array_init(zquery);
}
else {
zval_add_ref(&zquery);
}
if (!zfields) {
MAKE_STD_ZVAL(zfields);
array_init(zfields);
}
else {
zval_add_ref(&zfields);
}
object_init_ex(return_value, mongo_ce_GridFSCursor);
c = (mongo_collection*)zend_object_store_get_object(getThis() TSRMLS_CC);
MONGO_CHECK_INITIALIZED(c->ns, MongoGridFS);
MONGO_METHOD5(MongoGridFSCursor, __construct, &temp, return_value, getThis(), c->link, c->ns, zquery, zfields);
zval_ptr_dtor(&zquery);
zval_ptr_dtor(&zfields);
}
static int get_chunk_size(zval *array TSRMLS_DC) {
zval **zchunk_size = 0;
if (zend_hash_find(HASH_P(array), "chunkSize", strlen("chunkSize")+1, (void**)&zchunk_size) == FAILURE) {
add_assoc_long(array, "chunkSize", MonGlo(chunk_size));
return MonGlo(chunk_size);
}
convert_to_long(*zchunk_size);
return Z_LVAL_PP(zchunk_size) > 0 ?
Z_LVAL_PP(zchunk_size) :
MonGlo(chunk_size);
}
static int setup_file(FILE *fp, char *filename TSRMLS_DC) {
int size = 0;
// try to open the file
if (!fp) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "could not open file %s", filename);
return FAILURE;
}
// get size
fseek(fp, 0, SEEK_END);
size = ftell(fp);
if (size >= 0xffffffff) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "file %s is too large: %ld bytes", filename, size);
fclose(fp);
return FAILURE;
}
// reset file ptr
fseek(fp, 0, SEEK_SET);
return size;
}
static zval* setup_extra(zval *zfile, zval *extra TSRMLS_DC) {
zval temp;
zval *zid = 0;
zval **zzid = 0;
array_init(zfile);
// add user-defined fields
if (extra) {
zval temp;
zend_hash_merge(HASH_P(zfile), Z_ARRVAL_P(extra), (void (*)(void*))zval_add_ref, &temp, sizeof(zval*), 1);
}
// check if we need to add any fields
// _id
if (zend_hash_find(HASH_P(zfile), "_id", strlen("_id")+1, (void**)&zzid) == FAILURE) {
// create an id for the file
MAKE_STD_ZVAL(zid);
object_init_ex(zid, mongo_ce_Id);
MONGO_METHOD(MongoId, __construct, &temp, zid);
add_assoc_zval(zfile, "_id", zid);
}
else {
zid = *zzid;
}
return zid;
}
/* Use the db command to get the md5 hash of the inserted chunks
*
* $db->command(array(filemd5 => $fileId, "root" => $ns));
*
* adds the response to zfile as the "md5" field.
*
*/
static void add_md5(zval *zfile, zval *zid, mongo_collection *c TSRMLS_DC) {
if (!zend_hash_exists(HASH_P(zfile), "md5", strlen("md5")+1)) {
zval *data = 0, *response = 0, **md5 = 0;
// get the prefix
int prefix_len = strchr(Z_STRVAL_P(c->name), '.') - Z_STRVAL_P(c->name);
char *prefix = estrndup(Z_STRVAL_P(c->name), prefix_len);
// create command
MAKE_STD_ZVAL(data);
array_init(data);
add_assoc_zval(data, "filemd5", zid);
zval_add_ref(&zid);
add_assoc_stringl(data, "root", prefix, prefix_len, 0);
MAKE_STD_ZVAL(response);
ZVAL_NULL(response);
// run command
MONGO_CMD(response, c->parent);
// make sure there wasn't an error
if (!EG(exception) &&
zend_hash_find(HASH_P(response), "md5", strlen("md5")+1, (void**)&md5) == SUCCESS) {
// add it to zfile
add_assoc_zval(zfile, "md5", *md5);
/*
* increment the refcount so it isn't cleaned up at
* the end of this method
*/
zval_add_ref(md5);
}
// cleanup
if (!EG(exception)) {
zval_ptr_dtor(&response);
}
zval_ptr_dtor(&data);
}
}
/*
* Stores an array of bytes that may not have a filename,
* such as data from a socket or stream.
*
* Still somewhat limited atm, as the string has to fit
* in memory. It would be better if it could take a fh
* or something.
*
*/
PHP_METHOD(MongoGridFS, storeBytes) {
char *bytes = 0;
int bytes_len = 0, chunk_num = 0, chunk_size = 0, global_chunk_size = 0,
pos = 0;
zval temp;
zval *extra = 0, *zid = 0, *zfile = 0, *chunks = 0, *options = 0;
mongo_collection *c = (mongo_collection*)zend_object_store_get_object(getThis() TSRMLS_CC);
MONGO_CHECK_INITIALIZED(c->ns, MongoGridFS);
chunks = zend_read_property(mongo_ce_GridFS, getThis(), "chunks", strlen("chunks"), NOISY TSRMLS_CC);
ensure_gridfs_index(&temp, chunks TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|az", &bytes, &bytes_len, &extra, &options) == FAILURE) {
return;
}
if (!options) {
zval *opts;
MAKE_STD_ZVAL(opts);
array_init(opts);
options = opts;
} else {
zval_add_ref(&options);
}
// file array object
MAKE_STD_ZVAL(zfile);
ZVAL_NULL(zfile);
// merge extra & zfile and add _id if needed
zid = setup_extra(zfile, extra TSRMLS_CC);
// chunkSize
global_chunk_size = get_chunk_size(zfile TSRMLS_CC);
// size
if (!zend_hash_exists(HASH_P(zfile), "length", strlen("length")+1)) {
add_assoc_long(zfile, "length", bytes_len);
}
// insert chunks
while (pos < bytes_len) {
chunk_size = bytes_len-pos >= global_chunk_size ? global_chunk_size : bytes_len-pos;
insert_chunk(chunks, zid, chunk_num, bytes+pos, chunk_size, options TSRMLS_CC);
if (EG(exception)) {
return;
}
// increment counters
pos += chunk_size;
chunk_num++;
}
// now that we've inserted the chunks, use them to calculate the hash
add_md5(zfile, zid, c TSRMLS_CC);
// insert file
MONGO_METHOD2(MongoCollection, insert, &temp, getThis(), zfile, options);
zval_dtor(&temp);
zval_add_ref(&zid);
zval_ptr_dtor(&zfile);
zval_ptr_dtor(&options);
RETURN_ZVAL(zid, 1, 1);
}
/* add extra fields required for files:
* - filename
* - upload date
* - length
* these fields are only added if the user hasn't defined them.
*/
static int setup_file_fields(zval *zfile, char *filename, int size TSRMLS_DC) {
zval temp;
// filename
if (filename && !zend_hash_exists(HASH_P(zfile), "filename", strlen("filename")+1)) {
add_assoc_stringl(zfile, "filename", filename, strlen(filename), DUP);
}
// uploadDate
if (!zend_hash_exists(HASH_P(zfile), "uploadDate", strlen("uploadDate")+1)) {
// create an id for the file
zval *upload_date;
MAKE_STD_ZVAL(upload_date);
object_init_ex(upload_date, mongo_ce_Date);
MONGO_METHOD(MongoDate, __construct, &temp, upload_date);
add_assoc_zval(zfile, "uploadDate", upload_date);
}
// size
if (!zend_hash_exists(HASH_P(zfile), "length", strlen("length")+1)) {
add_assoc_long(zfile, "length", size);
}
return SUCCESS;
}
/* Creates a chunk and adds it to the chunks collection as:
* array(3) {
* files_id => zid
* n => chunk_num
* data => MongoBinData(buf, chunk_size, type 2)
* }
*
* Clean up should leave:
* - 1 ref to zid
* - buf
*/
static int insert_chunk(zval *chunks, zval *zid, int chunk_num, char *buf, int chunk_size, zval *options TSRMLS_DC) {
zval temp;
zval *zchunk, *zbin;
// create chunk
MAKE_STD_ZVAL(zchunk);
array_init(zchunk);
add_assoc_zval(zchunk, "files_id", zid);
zval_add_ref(&zid); // zid->refcount = 2
add_assoc_long(zchunk, "n", chunk_num);
// create MongoBinData object
MAKE_STD_ZVAL(zbin);
object_init_ex(zbin, mongo_ce_BinData);
zend_update_property_stringl(mongo_ce_BinData, zbin, "bin", strlen("bin"), buf, chunk_size TSRMLS_CC);
zend_update_property_long(mongo_ce_BinData, zbin, "type", strlen("type"), 2 TSRMLS_CC);
add_assoc_zval(zchunk, "data", zbin);
// insert chunk
if (options) {
MONGO_METHOD2(MongoCollection, insert, &temp, chunks, zchunk, options);
}
else {
MONGO_METHOD1(MongoCollection, insert, &temp, chunks, zchunk);
}
zval_dtor(&temp);
// increment counters
zval_ptr_dtor(&zchunk); // zid->refcount = 1
if (EG(exception)) {
return FAILURE;
}
return SUCCESS;
}
PHP_METHOD(MongoGridFS, storeFile) {
zval *fh, *extra = 0, *options = 0;
char *filename = 0;
int chunk_num = 0, global_chunk_size = 0, size = 0, pos = 0, fd = -1, safe = 0;
FILE *fp = 0;
zval temp;
zval *zid = 0, *zfile = 0, *chunks = 0;
mongo_collection *c = (mongo_collection*)zend_object_store_get_object(getThis() TSRMLS_CC);
MONGO_CHECK_INITIALIZED(c->ns, MongoGridFS);
chunks = zend_read_property(mongo_ce_GridFS, getThis(), "chunks", strlen("chunks"), NOISY TSRMLS_CC);
ensure_gridfs_index(&temp, chunks TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z|az", &fh, &extra, &options) == FAILURE) {
return;
}
if (!options) {
zval *opts;
MAKE_STD_ZVAL(opts);
array_init(opts);
options = opts;
}
if (Z_TYPE_P(fh) == IS_RESOURCE) {
zend_rsrc_list_entry *le;
php_stdio_stream_data *stdio_fptr = 0;
if (zend_hash_index_find(&EG(regular_list), Z_LVAL_P(fh), (void **) &le)==SUCCESS) {
php_stream *stream = (php_stream*)le->ptr;
if (!stream) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "could not find filehandle");
return;
}
stdio_fptr = (php_stdio_stream_data*)stream->abstract;
if (!stdio_fptr) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "no file is associate with this filehandle");
return;
}
}
if (stdio_fptr->file) {
if ((size = setup_file(stdio_fptr->file, filename TSRMLS_CC)) == FAILURE) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "error setting up file: %s", filename);
return;
}
fp = stdio_fptr->file;
}
fd = stdio_fptr->fd;
}
else if (Z_TYPE_P(fh) == IS_STRING) {
filename = Z_STRVAL_P(fh);
fp = fopen(filename, "rb");
// no point in continuing if we can't open the file
if ((size = setup_file(fp, filename TSRMLS_CC)) == FAILURE) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "error setting up file: %s", filename);
return;
}
}
else {
char *msg = "first argument must be a string or stream resource";
#if ZEND_MODULE_API_NO >= 20060613
zend_throw_exception(zend_exception_get_default(TSRMLS_C), msg, 0 TSRMLS_CC);
#else
zend_throw_exception(zend_exception_get_default(), msg, 0 TSRMLS_CC);
#endif /* ZEND_MODULE_API_NO >= 20060613 */
return;
}
// file array object
MAKE_STD_ZVAL(zfile);
ZVAL_NULL(zfile);
// merge extra & zfile and add _id if needed
zid = setup_extra(zfile, extra TSRMLS_CC);
setup_file_fields(zfile, filename, size TSRMLS_CC);
// chunkSize
global_chunk_size = get_chunk_size(zfile TSRMLS_CC);
// insert chunks
while (pos < size || fp == 0) {
int result = 0;
char *buf;
int chunk_size = size-pos >= global_chunk_size || fp == 0 ? global_chunk_size : size-pos;
buf = (char*)emalloc(chunk_size);
if (fp) {
if ((int)fread(buf, 1, chunk_size, fp) < chunk_size) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "error reading file %s", filename);
return;
}
pos += chunk_size;
if (insert_chunk(chunks, zid, chunk_num, buf, chunk_size, options TSRMLS_CC) == FAILURE) {
break;
}
}
else {
result = read(fd, buf, chunk_size);
if (result == -1) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "error reading filehandle");
return;
}
pos += result;
if (insert_chunk(chunks, zid, chunk_num, buf, result, options TSRMLS_CC) == FAILURE) {
break;
}
}
if (safe && EG(exception)) {
return;
}
chunk_num++;
efree(buf);
if (fp == 0 && result < chunk_size) {
break;
}
}
// close file ptr
if (fp) {
fclose(fp);
}
if (EG(exception)) {
zval_ptr_dtor(&zfile);
return;
}
if (!fp) {
add_assoc_long(zfile, "length", pos);
}
add_md5(zfile, zid, c TSRMLS_CC);
// insert file
MONGO_METHOD2(MongoCollection, insert, &temp, getThis(), zfile, options);
// cleanup
zval_add_ref(&zid);
zval_ptr_dtor(&zfile);
RETURN_ZVAL(zid, 1, 1);
}
PHP_METHOD(MongoGridFS, findOne) {
zval *zquery = 0, *zfields = 0, *file;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|zz", &zquery, &zfields) == FAILURE) {
return;
}
if (!zquery) {
MAKE_STD_ZVAL(zquery);
array_init(zquery);
}
else if (Z_TYPE_P(zquery) != IS_ARRAY) {
zval *temp;
convert_to_string(zquery);
MAKE_STD_ZVAL(temp);
array_init(temp);
add_assoc_string(temp, "filename", Z_STRVAL_P(zquery), 1);
zquery = temp;
}
else {
zval_add_ref(&zquery);
}
if (!zfields) {
MAKE_STD_ZVAL(zfields);
array_init(zfields);
}
else {
zval_add_ref(&zfields);
}
MAKE_STD_ZVAL(file);
MONGO_METHOD2(MongoCollection, findOne, file, getThis(), zquery, zfields);
if (Z_TYPE_P(file) == IS_NULL) {
RETVAL_NULL();
}
else {
zval temp;
object_init_ex(return_value, mongo_ce_GridFSFile);
MONGO_METHOD2(MongoGridFSFile, __construct, &temp, return_value, getThis(), file);
}
zval_ptr_dtor(&file);
zval_ptr_dtor(&zquery);
zval_ptr_dtor(&zfields);
}
PHP_METHOD(MongoGridFS, remove) {
zval *criteria = 0, *options = 0, *zfields, *zcursor, *chunks, *next, temp;
chunks = zend_read_property(mongo_ce_GridFS, getThis(), "chunks", strlen("chunks"), NOISY TSRMLS_CC);
ensure_gridfs_index(&temp, chunks TSRMLS_CC);
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|za", &criteria, &options) == FAILURE) {
return;
}
if (!criteria) {
MAKE_STD_ZVAL(criteria);
array_init(criteria);
}
else {
zval_add_ref(&criteria);
}
if (!options) {
MAKE_STD_ZVAL(options);
array_init(options);
}
else {
zval_add_ref(&options);
}
// { _id : 1 }
MAKE_STD_ZVAL(zfields);
array_init(zfields);
add_assoc_long(zfields, "_id", 1);
// cursor = db.fs.files.find(criteria, {_id : 1});
MAKE_STD_ZVAL(zcursor);
MONGO_METHOD2(MongoCollection, find, zcursor, getThis(), criteria, zfields);
zval_ptr_dtor(&zfields);
PHP_MONGO_CHECK_EXCEPTION3(&zcursor, &criteria, &options);
MAKE_STD_ZVAL(next);
MONGO_METHOD(MongoCursor, getNext, next, zcursor);
PHP_MONGO_CHECK_EXCEPTION4(&next, &zcursor, &criteria, &options);
while (Z_TYPE_P(next) != IS_NULL) {
zval **id;
zval *temp, *temp_return;
if (zend_hash_find(HASH_P(next), "_id", 4, (void**)&id) == FAILURE) {
// uh oh
continue;
}
MAKE_STD_ZVAL(temp);
array_init(temp);
zval_add_ref(id);
add_assoc_zval(temp, "files_id", *id);
MAKE_STD_ZVAL(temp_return);
ZVAL_NULL(temp_return);
MONGO_METHOD2(MongoCollection, remove, temp_return, chunks, temp, options);
zval_ptr_dtor(&temp);
zval_ptr_dtor(&temp_return);
zval_ptr_dtor(&next);
PHP_MONGO_CHECK_EXCEPTION3(&zcursor, &criteria, &options);
MAKE_STD_ZVAL(next);
MONGO_METHOD(MongoCursor, getNext, next, zcursor);
PHP_MONGO_CHECK_EXCEPTION4(&next, &zcursor, &criteria, &options);
}
zval_ptr_dtor(&next);
zval_ptr_dtor(&zcursor);
MONGO_METHOD2(MongoCollection, remove, return_value, getThis(), criteria, options);
zval_ptr_dtor(&criteria);
zval_ptr_dtor(&options);
}
PHP_METHOD(MongoGridFS, storeUpload) {
zval *extra = 0, *h, **file, **temp, **name = 0, *extra_param = 0;
char *filename = 0;
int file_len = 0, found_name = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z", &filename, &file_len, &extra) == FAILURE) {
return;
}
h = PG(http_globals)[TRACK_VARS_FILES];
if (zend_hash_find(Z_ARRVAL_P(h), filename, file_len+1, (void**)&file) == FAILURE) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "could not find uploaded file %s", filename);
return;
}
zend_hash_find(Z_ARRVAL_PP(file), "tmp_name", strlen("tmp_name")+1, (void**)&temp);
if (!temp || Z_TYPE_PP(temp) != IS_STRING) {
zend_throw_exception(mongo_ce_GridFSException, "tmp_name was not a string", 0 TSRMLS_CC);
return;
}
if (extra && Z_TYPE_P(extra) == IS_ARRAY) {
zval_add_ref(&extra);
extra_param = extra;
if (zend_hash_exists(HASH_P(extra), "filename", strlen("filename")+1)) {
found_name = 1;
}
}
else {
MAKE_STD_ZVAL(extra_param);
array_init(extra_param);
if (extra && Z_TYPE_P(extra) == IS_STRING) {
add_assoc_string(extra_param, "filename", Z_STRVAL_P(extra), 1);
found_name = 1;
}
}
if (!found_name &&
zend_hash_find(Z_ARRVAL_PP(file), "name", strlen("name")+1, (void**)&name) == SUCCESS &&
Z_TYPE_PP(name) == IS_STRING) {
add_assoc_string(extra_param, "filename", Z_STRVAL_PP(name), 1);
}
MONGO_METHOD2(MongoGridFS, storeFile, return_value, getThis(), *temp, extra_param);
zval_ptr_dtor(&extra_param);
}
/*
* New GridFS API
*/
PHP_METHOD(MongoGridFS, delete) {
zval *id, *criteria;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &id, mongo_ce_Id) == FAILURE) {
return;
}
MAKE_STD_ZVAL(criteria);
array_init(criteria);
add_assoc_zval(criteria, "_id", id);
zval_add_ref(&id);
MONGO_METHOD1(MongoGridFS, remove, return_value, getThis(), criteria);
zval_ptr_dtor(&criteria);
}
PHP_METHOD(MongoGridFS, get) {
zval *id, *criteria;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &id, mongo_ce_Id) == FAILURE) {
return;
}
MAKE_STD_ZVAL(criteria);
array_init(criteria);
add_assoc_zval(criteria, "_id", id);
zval_add_ref(&id);
MONGO_METHOD1(MongoGridFS, findOne, return_value, getThis(), criteria);
zval_ptr_dtor(&criteria);
}
PHP_METHOD(MongoGridFS, put) {
MONGO_METHOD_BASE(MongoGridFS, storeFile)(ZEND_NUM_ARGS(), return_value, NULL, getThis(), 0 TSRMLS_CC);
}
static zend_function_entry MongoGridFS_methods[] = {
PHP_ME(MongoGridFS, __construct, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, drop, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, find, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, storeFile, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, storeBytes, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, findOne, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, remove, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, storeUpload, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, delete, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, get, NULL, ZEND_ACC_PUBLIC)
PHP_ME(MongoGridFS, put, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
void mongo_init_MongoGridFS(TSRMLS_D) {
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "MongoGridFS", MongoGridFS_methods);
ce.create_object = php_mongo_collection_new;
mongo_ce_GridFS = zend_register_internal_class_ex(&ce, mongo_ce_Collection, "MongoCollection" TSRMLS_CC);
zend_declare_property_null(mongo_ce_GridFS, "chunks", strlen("chunks"), ZEND_ACC_PUBLIC TSRMLS_CC);
zend_declare_property_null(mongo_ce_GridFS, "filesName", strlen("filesName"), ZEND_ACC_PROTECTED TSRMLS_CC);
zend_declare_property_null(mongo_ce_GridFS, "chunksName", strlen("chunksName"), ZEND_ACC_PROTECTED TSRMLS_CC);
}
PHP_METHOD(MongoGridFSFile, __construct) {
zval *gridfs = 0, *file = 0;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Oa", &gridfs, mongo_ce_GridFS, &file) == FAILURE) {
return;
}
zend_update_property(mongo_ce_GridFSFile, getThis(), "gridfs", strlen("gridfs"), gridfs TSRMLS_CC);
zend_update_property(mongo_ce_GridFSFile, getThis(), "file", strlen("file"), file TSRMLS_CC);
}
PHP_METHOD(MongoGridFSFile, getFilename) {
zval *file = zend_read_property(mongo_ce_GridFSFile, getThis(), "file", strlen("file"), NOISY TSRMLS_CC);
if (zend_hash_find(HASH_P(file), "filename", strlen("filename")+1, (void**)&return_value_ptr) == SUCCESS) {
RETURN_ZVAL(*return_value_ptr, 1, 0);
}
RETURN_NULL();
}
PHP_METHOD(MongoGridFSFile, getSize) {
zval *file = zend_read_property(mongo_ce_GridFSFile, getThis(), "file", strlen("file"), NOISY TSRMLS_CC);
if (zend_hash_find(HASH_P(file), "length", strlen("length")+1, (void**)&return_value_ptr) == SUCCESS) {
RETURN_ZVAL(*return_value_ptr, 1, 0);
}
RETURN_NULL();
}
PHP_METHOD(MongoGridFSFile, write) {
char *filename = 0;
int filename_len, total = 0;
zval *gridfs, *file, *chunks, *query, *cursor, *sort;
zval **id;
FILE *fp;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &filename, &filename_len) == FAILURE) {
return;
}
gridfs = zend_read_property(mongo_ce_GridFSFile, getThis(), "gridfs", strlen("gridfs"), NOISY TSRMLS_CC);
file = zend_read_property(mongo_ce_GridFSFile, getThis(), "file", strlen("file"), NOISY TSRMLS_CC);
// make sure that there's an index on chunks so we can sort by chunk num
chunks = zend_read_property(mongo_ce_GridFS, gridfs, "chunks", strlen("chunks"), NOISY TSRMLS_CC);
ensure_gridfs_index(return_value, chunks TSRMLS_CC);
if (!filename) {
zval **temp;
zend_hash_find(HASH_P(file), "filename", strlen("filename")+1, (void**)&temp);
filename = Z_STRVAL_PP(temp);
}
fp = fopen(filename, "wb");
if (!fp) {
zend_throw_exception_ex(mongo_ce_GridFSException, 0 TSRMLS_CC, "could not open destination file %s", filename);
return;
}
zend_hash_find(HASH_P(file), "_id", strlen("_id")+1, (void**)&id);
MAKE_STD_ZVAL(query);
array_init(query);
zval_add_ref(id);
add_assoc_zval(query, "files_id", *id);
MAKE_STD_ZVAL(cursor);
MONGO_METHOD1(MongoCollection, find, cursor, chunks, query);
MAKE_STD_ZVAL(sort);
array_init(sort);
add_assoc_long(sort, "n", 1);
MONGO_METHOD1(MongoCursor, sort, cursor, cursor, sort);
if ((total = apply_to_cursor(cursor, copy_file, fp TSRMLS_CC)) == FAILURE) {
zend_throw_exception(mongo_ce_GridFSException, "error reading chunk of file", 0 TSRMLS_CC);
}
fclose(fp);
zval_ptr_dtor(&cursor);
zval_ptr_dtor(&sort);
zval_ptr_dtor(&query);
RETURN_LONG(total);
}
PHP_METHOD(MongoGridFSFile, getResource) {
php_stream * stream;
stream = gridfs_stream_init(getThis());
if (!stream || stream == FAILURE) {
zend_throw_exception(mongo_ce_GridFSException, "couldn't create a php_stream", 0 TSRMLS_CC);
return;
}
php_stream_to_zval(stream, return_value);