Index: library/mongodb/nsfmongo.c =================================================================== diff -u -rcef3de5c4f65e767d0c66389bacc77bc3c2e5a68 -r0067fc15903b586ffad2a459ce1cf7294c8158d3 --- library/mongodb/nsfmongo.c (.../nsfmongo.c) (revision cef3de5c4f65e767d0c66389bacc77bc3c2e5a68) +++ library/mongodb/nsfmongo.c (.../nsfmongo.c) (revision 0067fc15903b586ffad2a459ce1cf7294c8158d3) @@ -683,7 +683,7 @@ NsfMongoCollectionDelete(Tcl_Interp *interp, mongoc_collection_t *collectionPtr, Tcl_Obj *conditionObj) { - int objc, result = TCL_OK, status; + int objc, result, status; Tcl_Obj **objv; bson_t query[1]; bson_error_t bsonError; @@ -900,7 +900,7 @@ mongoc_update_flags_t updateFlags = MONGOC_UPDATE_NO_VALIDATE; /* for dbrefs */ bson_error_t bsonError; bson_t cond[1], values[1]; - int objc, result = TCL_OK, success; + int objc, result, success; Tcl_Obj **objv; result = Tcl_ListObjGetElements(interp, conditionObj, &objc, &objv); @@ -927,7 +927,7 @@ result = NsfPrintError(interp, "mongo::collection::delete: error: %s", bsonError.message); } - return TCL_OK; + return result; } /*********************************************************************** @@ -1125,15 +1125,13 @@ mongoc_gridfs_t *gridfsPtr, CONST char *value, CONST char *name, CONST char *contenttype) { - int n, result = TCL_OK; + int result = TCL_OK; mongoc_gridfs_file_opt_t fileOpts = {NULL}; mongoc_gridfs_file_t *gridFile; - fprintf(stderr, "0 withSource %d\n", withSource); if (withSource == GridfilesourceNULL) { withSource = GridfilesourceFileIdx; } - fprintf(stderr, "1 withSource %d\n", withSource); fileOpts.filename = name; fileOpts.content_type = contenttype; @@ -1151,9 +1149,14 @@ uint8_t buf[MONGOC_GRIDFS_READ_CHUNK]; struct iovec iov = { buf, 0 }; int fd = open(value, O_RDONLY); + + if (fd < 1) { + mongoc_gridfs_file_destroy(gridFile); + return NsfPrintError(interp, "nsf::gridfile::create: cannot open file '%s' for reading", value); + } for (;; ) { - n = read(fd, iov.iov_base, MONGOC_GRIDFS_READ_CHUNK); + int n = read(fd, iov.iov_base, MONGOC_GRIDFS_READ_CHUNK); if (n > 0) { iov.iov_len = n; n = mongoc_gridfs_file_writev(gridFile, &iov, 1, 0); @@ -1251,7 +1254,7 @@ Tcl_Obj *queryObj) { mongoc_gridfs_file_t* gridFilePtr; bson_error_t bsonError; - int result = TCL_OK, objc; + int result, objc; bson_t query[1]; Tcl_Obj **objv; Index: library/mongodb/nx-mongo.tcl =================================================================== diff -u -rcef3de5c4f65e767d0c66389bacc77bc3c2e5a68 -r0067fc15903b586ffad2a459ce1cf7294c8158d3 --- library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision cef3de5c4f65e767d0c66389bacc77bc3c2e5a68) +++ library/mongodb/nx-mongo.tcl (.../nx-mongo.tcl) (revision 0067fc15903b586ffad2a459ce1cf7294c8158d3) @@ -103,33 +103,28 @@ unset :gridFs :gridFsName } - :public object method "gridfs store_file" {local remote {mime text/plain}} { - ::mongo::gridfs::store_file ${:gridFs} $local $remote $mime + :public object method "gridfs create" {{-source file} value name {mime text/plain}} { + ::mongo::gridfile::create -source $source ${:gridFs} $value $name $mime } - :public object method "gridfs store_string" {string remote {mime text/plain}} { - ::mongo::gridfs::store_string ${:gridFs} $string $remote $mime - } - :public object method "gridfs list" {name} { - if {[string first * $name] == -1} { - set info [::mongo::query ${:mongoConn} ${:db}.${:gridFsName}.files \ - [list \$query document [list filename string $name]] \ - -limit 1] + :public object method "gridfs list" {{-all:switch false} query} { + set coll [:collection ${:db}.${:gridFsName}.files] + if {!$all} { + set info [::mongo::collection::query $coll $query -limit 1] return [lindex $info 0] } else { - ns_log notice "::mongo::query ${:mongoConn} ${:db}.${:gridFsName}.files" - set info [::mongo::query ${:mongoConn} ${:db}.${:gridFsName}.files {}] + set info [::mongo::collection::query $coll {}] return $info } } :public object method "gridfs update" {id bson} { - ::mongo::update ${:mongoConn} ${:db}.${:gridFsName}.files \ + ::mongo::collection::update [:collection ${:db}.${:gridFsName}.files] \ [list _id oid $id] $bson } - :public object method "file content" {name} { - set f [mongo::gridfile::open ${:gridFs} $name] + :public object method "file content" {query} { + set f [mongo::gridfile::open ${:gridFs} $query] set content "" while {1} { append content [set chunk [mongo::gridfile::read $f 4096]] @@ -141,9 +136,9 @@ return $content } - :public object method "gridfs set attribute" {file attribute value} { - set info [::nx::mongo::db gridfs list $file] - if {$info eq ""} {error "no such file <$file> stored in gridfs"} + :public object method "gridfs set attribute" {query attribute value} { + set info [::nx::mongo::db gridfs list $query] + if {$info eq ""} {error "no such file <$query> stored in gridfs"} foreach {att type v} $info { dict set d $att $v } if {[dict exists $d $attribute] && [dict get $d $attribute] eq $value} { # right value, nothing to do @@ -165,11 +160,11 @@ nx::mongo::db gridfs update [dict get $d _id] $bson } - :public object method "gridfs map" {file url} { - ::nx::mongo::db gridfs set attribute $file url $url + :public object method "gridfs map" {query url} { + ::nx::mongo::db gridfs set attribute $query url $url } :public object method "gridfs mapped" {url} { - set info [::mongo::query ${:mongoConn} ${:db}.${:gridFsName}.files \ + set info [::mongo::collection::query [:collection ${:db}.${:gridFsName}.files] \ [list \$query document [list url string $url]] \ -limit 1] return [lindex $info 0]