]> code.citadel.org Git - citadel.git/blobdiff - citadel/server/room_ops.c
DRY fetch of msglists.
[citadel.git] / citadel / server / room_ops.c
index 1566e74b0a85261426a9bd1994816048a0beca8b..b8f8113d73777329353f4d69237379088cc5dcc5 100644 (file)
@@ -601,6 +601,29 @@ int CtdlIsNonEditable(struct ctdlroom *qrbuf) {
 }
 
 
+// Retrieve a list of all messages (message numbers) in the specified room.
+// Returns the number of messages in the room, allocates a pointer to the array and stuffs it in the supplied location.
+// Caller must free that memory.
+// Returns (-1) if error.
+int CtdlFetchMsgList(long roomnum, long **msgs) {
+       int num_msgs = 0;
+        struct cdbdata *cdbfr;
+
+        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
+       if (cdbfr == NULL) {
+               syslog(LOG_ERR, "room_ops: no msglist for room %ld", roomnum);
+               *msgs = NULL;
+               return (-1);
+       }
+
+       *msgs = malloc(cdbfr->len);
+       memcpy(*msgs, cdbfr->ptr, cdbfr->len);
+               num_msgs = cdbfr->len / sizeof(long);
+               cdb_free(cdbfr);
+       return(num_msgs);
+}
+
+
 // Make the specified room the current room for this session.  No validation
 // or access control is done here -- the caller should make sure that the
 // specified room exists and is ok to access.
@@ -616,7 +639,6 @@ void CtdlUserGoto(char *where, int display_result, int transiently, int *retmsgs
        int raideflag;
        struct visit vbuf;
        char truncated_roomname[ROOMNAMELEN];
-        struct cdbdata *cdbfr;
        long *msglist = NULL;
        int num_msgs = 0;
        unsigned int original_v_flags;
@@ -667,13 +689,7 @@ void CtdlUserGoto(char *where, int display_result, int transiently, int *retmsgs
                info = 1;
        }
 
-        cdbfr = cdb_fetch(CDB_MSGLISTS, &CC->room.QRnumber, sizeof(long));
-        if (cdbfr != NULL) {
-               msglist = (long *) cdbfr->ptr;
-               cdbfr->ptr = NULL;                      // CtdlUserGoto() now owns this memory (this requires attention if we move to LMDB)
-               num_msgs = cdbfr->len / sizeof(long);
-               cdb_free(cdbfr);
-       }
+       num_msgs = CtdlFetchMsgList(CC->room.QRnumber, &msglist);
 
        total_messages = 0;
        for (a=0; a<num_msgs; ++a) {