The CtdlWriteObject() API no longer uses a temp file
authorArt Cancro <ajc@citadel.org>
Mon, 11 Aug 2008 19:57:40 +0000 (19:57 +0000)
committerArt Cancro <ajc@citadel.org>
Mon, 11 Aug 2008 19:57:40 +0000 (19:57 +0000)
citadel/modules/calendar/serv_calendar.c
citadel/modules/openid/serv_openid_rp.c
citadel/modules/vcard/serv_vcard.c
citadel/msgbase.c
citadel/msgbase.h

index b141778887e17e3908ea9dbc13b326a7e04e610a..41bedf0268047948043c0daec435e99abed8e7b6 100644 (file)
@@ -105,8 +105,6 @@ icalcomponent *ical_encapsulate_subcomponent(icalcomponent *subcomp) {
  * to the currently selected room.
  */
 void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
-       char temp[PATH_MAX];
-       FILE *fp = NULL;
        char *ser = NULL;
        icalcomponent *encaps = NULL;
        struct CtdlMessage *msg = NULL;
@@ -131,24 +129,16 @@ void ical_write_to_cal(struct ctdluser *u, icalcomponent *cal) {
 
        /* If the caller supplied a user, write to that user's default calendar room */
        if (u) {
-               /* Make a temp file out of it */
-               CtdlMakeTempFileName(temp, sizeof temp);
-               fp = fopen(temp, "w");
-               if (fp != NULL) {
-                       fwrite(ser, strlen(ser), 1, fp);
-                       fclose(fp);
-               
-                       /* This handy API function does all the work for us. */
-                       CtdlWriteObject(USERCALENDARROOM,       /* which room */
-                               "text/calendar",        /* MIME type */
-                               temp,                   /* temp file */
-                               u,                      /* which user */
-                               0,                      /* not binary */
-                               0,                      /* don't delete others of this type */
-                               0                       /* no flags */
-                       );
-                       unlink(temp);
-               }
+               /* This handy API function does all the work for us. */
+               CtdlWriteObject(USERCALENDARROOM,       /* which room */
+                       "text/calendar",        /* MIME type */
+                       ser,                    /* data */
+                       strlen(ser)+1,          /* length */
+                       u,                      /* which user */
+                       0,                      /* not binary */
+                       0,                      /* don't delete others of this type */
+                       0                       /* no flags */
+               );
        }
 
        /* If the caller did not supply a user, write to the currently selected room */
index 05fbccea1efc87b07a97bdd8563d88937e29e615..2fb909784a37cb1e940594f113aebd0497e7b107 100644 (file)
@@ -254,20 +254,12 @@ void populate_vcard_from_sreg(HashList *sreg_keys) {
 
        /* Only save the vCard if there is some useful data in it */
        if (pop > 0) {
-               char temp[PATH_MAX];
-               FILE *fp;
                char *ser;
-       
-               CtdlMakeTempFileName(temp, sizeof temp);
                ser = vcard_serialize(v);
                if (ser) {
-                       fp = fopen(temp, "w");
-                       if (fp) {
-                               fwrite(ser, strlen(ser), 1, fp);
-                               fclose(fp);
-                               CtdlWriteObject(USERCONFIGROOM, "text/x-vcard", temp, &CC->user, 0, 0, 0);
-                               unlink(temp);
-                       }
+                       CtdlWriteObject(USERCONFIGROOM, "text/x-vcard",
+                               ser, strlen(ser)+1, &CC->user, 0, 0, 0
+                       );
                        free(ser);
                }
        }
index 2f126f40852b3cae4e2e44225c715c6f1ab26064..a6f6f9d514f22a78364a4d6feeb9ff6cfa8b6363 100644 (file)
@@ -794,22 +794,13 @@ struct vCard *vcard_get_user(struct ctdluser *u) {
  * Write our config to disk
  */
 void vcard_write_user(struct ctdluser *u, struct vCard *v) {
-       char temp[PATH_MAX];
-       FILE *fp;
        char *ser;
 
-       CtdlMakeTempFileName(temp, sizeof temp);
        ser = vcard_serialize(v);
-
-       fp = fopen(temp, "w");
-       if (fp == NULL) return;
        if (ser == NULL) {
-               fprintf(fp, "begin:vcard\r\nend:vcard\r\n");
-       } else {
-               fwrite(ser, strlen(ser), 1, fp);
-               free(ser);
+               ser = strdup("begin:vcard\r\nend:vcard\r\n");
        }
-       fclose(fp);
+       if (!ser) return;
 
        /* This handy API function does all the work for us.
         * NOTE: normally we would want to set that last argument to 1, to
@@ -817,15 +808,16 @@ void vcard_write_user(struct ctdluser *u, struct vCard *v) {
         * have to, because the vcard_upload_beforesave() hook above
         * is going to notice what we're trying to do, and delete the old vCard.
         */
-       CtdlWriteObject(USERCONFIGROOM, /* which room */
-                       VCARD_MIME_TYPE,/* MIME type */
-                       temp,           /* temp file */
-                       u,              /* which user */
-                       0,              /* not binary */
-                       0,              /* don't delete others of this type */
-                       0);             /* no flags */
-
-       unlink(temp);
+       CtdlWriteObject(USERCONFIGROOM,         /* which room */
+                       VCARD_MIME_TYPE,        /* MIME type */
+                       ser,                    /* data */
+                       strlen(ser)+1,          /* length */
+                       u,                      /* which user */
+                       0,                      /* not binary */
+                       0,                      /* don't delete others of this type */
+                       0);                     /* no flags */
+
+       free(ser);
 }
 
 
index 6dedd7ee3c1c4121c33cabcd96ddd75d4b89f502..ccee9818d2574549e63cd821cc49a1a3c0ff6ade 100644 (file)
@@ -4424,24 +4424,21 @@ void TDAP_AdjRefCount(long msgnum, int incr)
  * Note: this could be much more efficient.  Right now we use two temporary
  * files, and still pull the message into memory as with all others.
  */
-void CtdlWriteObject(char *req_room,           /* Room to stuff it in */
-                       char *content_type,     /* MIME type of this object */
-                       char *tempfilename,     /* Where to fetch it from */
+void CtdlWriteObject(char *req_room,                   /* Room to stuff it in */
+                       char *content_type,             /* MIME type of this object */
+                       char *raw_message,              /* Data to be written */
+                       off_t raw_length,               /* Size of raw_message */
                        struct ctdluser *is_mailbox,    /* Mailbox room? */
-                       int is_binary,          /* Is encoding necessary? */
-                       int is_unique,          /* Del others of this type? */
-                       unsigned int flags      /* Internal save flags */
+                       int is_binary,                  /* Is encoding necessary? */
+                       int is_unique,                  /* Del others of this type? */
+                       unsigned int flags              /* Internal save flags */
                        )
 {
 
-       FILE *fp;
        struct ctdlroom qrbuf;
        char roomname[ROOMNAMELEN];
        struct CtdlMessage *msg;
-
-       char *raw_message = NULL;
        char *encoded_message = NULL;
-       off_t raw_length = 0;
 
        if (is_mailbox != NULL) {
                MailboxName(roomname, sizeof roomname, is_mailbox, req_room);
@@ -4450,24 +4447,10 @@ void CtdlWriteObject(char *req_room,            /* Room to stuff it in */
                safestrncpy(roomname, req_room, sizeof(roomname));
        }
 
-       fp = fopen(tempfilename, "rb");
-       if (fp == NULL) {
-               CtdlLogPrintf(CTDL_CRIT, "Cannot open %s: %s\n",
-                       tempfilename, strerror(errno));
-               return;
-       }
-       fseek(fp, 0L, SEEK_END);
-       raw_length = ftell(fp);
-       rewind(fp);
        CtdlLogPrintf(CTDL_DEBUG, "Raw length is %ld\n", (long)raw_length);
 
-       raw_message = malloc((size_t)raw_length + 2);
-       fread(raw_message, (size_t)raw_length, 1, fp);
-       fclose(fp);
-
        if (is_binary) {
-               encoded_message = malloc((size_t)
-                       (((raw_length * 134) / 100) + 4096 ) );
+               encoded_message = malloc((size_t) (((raw_length * 134) / 100) + 4096 ) );
        }
        else {
                encoded_message = malloc((size_t)(raw_length + 4096));
@@ -4495,7 +4478,6 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
                );
        }
        else {
-               raw_message[raw_length] = 0;
                memcpy(
                        &encoded_message[strlen(encoded_message)],
                        raw_message,
@@ -4503,8 +4485,6 @@ void CtdlWriteObject(char *req_room,              /* Room to stuff it in */
                );
        }
 
-       free(raw_message);
-
        CtdlLogPrintf(CTDL_DEBUG, "Allocating\n");
        msg = malloc(sizeof(struct CtdlMessage));
        memset(msg, 0, sizeof(struct CtdlMessage));
@@ -4594,20 +4574,9 @@ char *CtdlGetSysConfig(char *sysconfname) {
        return(conf);
 }
 
-void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
-       char temp[PATH_MAX];
-       FILE *fp;
-
-       CtdlMakeTempFileName(temp, sizeof temp);
 
-       fp = fopen(temp, "w");
-       if (fp == NULL) return;
-       fprintf(fp, "%s", sysconfdata);
-       fclose(fp);
-
-       /* this handy API function does all the work for us */
-       CtdlWriteObject(SYSCONFIGROOM, sysconfname, temp, NULL, 0, 1, 0);
-       unlink(temp);
+void CtdlPutSysConfig(char *sysconfname, char *sysconfdata) {
+       CtdlWriteObject(SYSCONFIGROOM, sysconfname, sysconfdata, (strlen(sysconfdata)+1), NULL, 0, 1, 0);
 }
 
 
index 754656d58ff0d02270db87a149f514ef54e111f9..56097736107ef9174d53c6a97963c95a4a4e9e36 100644 (file)
@@ -119,8 +119,15 @@ int CtdlForEachMessage(int mode,
                         void (*CallBack) (long, void *),
                        void *userdata);
 int CtdlDeleteMessages(char *, long *, int, char *);
-void CtdlWriteObject(char *, char *, char *, struct ctdluser *,
-                       int, int, unsigned int);
+void CtdlWriteObject(char *req_room,                   /* Room to stuff it in */
+                       char *content_type,             /* MIME type of this object */
+                       char *raw_message,              /* Data to be written */
+                       off_t raw_length,               /* Size of raw_message */
+                       struct ctdluser *is_mailbox,    /* Mailbox room? */
+                       int is_binary,                  /* Is encoding necessary? */
+                       int is_unique,                  /* Del others of this type? */
+                       unsigned int flags              /* Internal save flags */
+);
 struct CtdlMessage *CtdlFetchMessage(long msgnum, int with_body);
 void CtdlFreeMessage(struct CtdlMessage *msg);
 void serialize_message(struct ser_ret *, struct CtdlMessage *);