- WriteStream API changed: now returns the number of bytes written
authorJulien Kerihuel <j.kerihuel@openchange.org>
Fri, 28 Sep 2007 06:01:58 +0000 (06:01 +0000)
committerJulien Kerihuel <j.kerihuel@openchange.org>
Fri, 28 Sep 2007 06:01:58 +0000 (06:01 +0000)
- WriteStream man page updated
- 16 bytes extra-data bug fixed when sending attachments in openchangeclient
- Set open mode to 0600 when attachments are stored on the filesystem

doc/man/man3/WriteStream.3
libmapi/IStream.c
torture/mapi_sendattach.c
utils/openchangeclient.c

index 40efa666d36e8e6cad95b72354c4a407d852cf3f..d2740a83e7d684ba40d6f30d84ea8e89899a6428 100644 (file)
 .\" Process this file with
 .\" groff -man -Tascii WriteStream.3
 .\"
-.TH WRITESTREAM 3 2007-04-23 "OpenChange libmapi 0.2" "OpenChange Programmer's Manual"
+.TH WRITESTREAM 3 2007-09-27 "OpenChange libmapi 0.6" "OpenChange Programmer's Manual"
 .SH NAME
 WriteStream \- Write buffer to a stream
 .SH SYNOPSIS
 .nf
 .B #include <libmapi/libmapi.h>
 .sp
-.BI "enum MAPISTATUS WriteStream(mapi_object_t *" obj_stream ", DATA_BLOB *" data ");"
+.BI "enum MAPISTATUS WriteStream(mapi_object_t *" obj_stream ", DATA_BLOB *" data ", uint16_t *" read_size ");"
 
 .fi
 .SH DESCRIPTION
@@ -41,7 +41,9 @@ This function writes the stream specified as a
 in 
 .IR data
 to the stream
-.IR obj_stream .
+.IR obj_stream
+and returns the number of bytes written in
+.B read_size
 
 .SH RETURN VALUE
 The function returns
index 7fa3b74aa1a0e4b43842b60cb7095f918ff043d4..466affdec6c06d73d5d7269739a6d9baeb17cbc7 100644 (file)
@@ -157,7 +157,7 @@ _PUBLIC_ enum MAPISTATUS ReadStream(mapi_object_t *obj_stream, unsigned char *bu
  * Write buffer to the stream
  */
 
-_PUBLIC_ enum MAPISTATUS WriteStream(mapi_object_t *obj_stream, DATA_BLOB *blob)
+_PUBLIC_ enum MAPISTATUS WriteStream(mapi_object_t *obj_stream, DATA_BLOB *blob, uint16_t *read_size)
 {
        struct mapi_request     *mapi_request;
        struct mapi_response    *mapi_response;
@@ -205,6 +205,8 @@ _PUBLIC_ enum MAPISTATUS WriteStream(mapi_object_t *obj_stream, DATA_BLOB *blob)
        retval = mapi_response->mapi_repl->error_code;
        MAPI_RETVAL_IF(retval, retval, mem_ctx);
 
+       *read_size = mapi_response->mapi_repl->u.mapi_WriteStream.size;
+
        talloc_free(mapi_response);
        talloc_free(mem_ctx);
        
index 4fa7f71d64d835d4aa6991f8aeaf6fc6a0775680..765682860bb56aa14f3f0e0c4562b9ce385c9f83 100644 (file)
@@ -186,6 +186,7 @@ bool torture_rpc_mapi_sendattach(struct torture_context *torture)
                int             fd;
                struct stat     sb;
                uint32_t        read_size;
+               uint16_t        buf_readsize;
                uint8_t         buf[0x7000];
 
                if (stat(filename, &sb) != 0) return false;
@@ -202,7 +203,7 @@ bool torture_rpc_mapi_sendattach(struct torture_context *torture)
                        memcpy(blob.data, buf, read_size);
                        
                        errno = 0;
-                       retval = WriteStream(&obj_stream, &blob);
+                       retval = WriteStream(&obj_stream, &blob, &buf_readsize);
                        mapi_errstr("WriteStream", GetLastError());
                        talloc_free(blob.data);
                }
index 9149f6a281afc4b8d749b1d435a9b5caabbbf2df..5a024728cc47fe028aefed34da90fb38cd4c8698 100644 (file)
@@ -245,11 +245,9 @@ static bool store_attachment(mapi_object_t obj_attach, const char *filename, uin
        enum MAPISTATUS retval;
        char            *path;
        mapi_object_t   obj_stream;
-       uint32_t        stream_size;
        uint32_t        read_size;
        int             fd;
        unsigned char   buf[MAX_READ_SIZE];
-       uint32_t        max_read_size = MAX_READ_SIZE;
 
        if (!filename || !size) return false;
 
@@ -263,7 +261,7 @@ static bool store_attachment(mapi_object_t obj_attach, const char *filename, uin
        }
 
        path = talloc_asprintf(mem_ctx, "%s/%s", oclient->store_folder, filename);
-       if ((fd = open(path, O_CREAT|O_WRONLY)) == -1) return false;
+       if ((fd = open(path, O_CREAT|O_WRONLY, S_IWUSR|S_IRUSR)) == -1) return false;
 
        retval = OpenStream(&obj_attach, PR_ATTACH_DATA_BIN, 0, &obj_stream);
        if (retval != MAPI_E_SUCCESS) return false;
@@ -273,15 +271,15 @@ static bool store_attachment(mapi_object_t obj_attach, const char *filename, uin
                if (retval != MAPI_E_SUCCESS) goto error;
                write(fd, buf, read_size);
                close(fd);
+       } else {
+               do {
+                       retval = ReadStream(&obj_stream, buf, MAX_READ_SIZE, &read_size);
+                       if (retval != MAPI_E_SUCCESS) goto error;
+                       write(fd, buf, read_size);
+               } while (read_size);
+               close(fd);
        }
 
-       for (stream_size = 0; stream_size < size; stream_size += 0x4000) {
-               retval = ReadStream(&obj_stream, buf, max_read_size, &read_size);
-               if (retval != MAPI_E_SUCCESS) goto error;
-               write(fd, buf, read_size);
-       }
-       close(fd);
-
        mapi_object_release(&obj_stream);
        close(fd);
        talloc_free(mem_ctx);
@@ -351,7 +349,8 @@ static enum MAPISTATUS openchangeclient_fetchmail(mapi_object_t *obj_store,
 
        printf("MAILBOX (%d messages)\n", count);
 
-       while ((retval = QueryRows(&obj_table, 0xa, TBL_ADVANCE, &rowset)) != MAPI_E_NOT_FOUND && rowset.cRows) {
+       while ((retval = QueryRows(&obj_table, count, TBL_ADVANCE, &rowset)) != MAPI_E_NOT_FOUND && rowset.cRows) {
+               count -= rowset.cRows;
                for (i = 0; i < rowset.cRows; i++) {
                        mapi_object_init(&obj_message);
                        retval = OpenMessage(obj_store,
@@ -578,6 +577,7 @@ static bool openchangeclient_stream(TALLOC_CTX *mem_ctx, mapi_object_t obj_paren
        DATA_BLOB       stream;
        uint32_t        size;
        uint32_t        offset;
+       uint16_t        read_size;
 
        /* Open a stream on the parent for the given property */
        retval = OpenStream(&obj_parent, mapitag, access_flags, &obj_stream);
@@ -589,37 +589,33 @@ static bool openchangeclient_stream(TALLOC_CTX *mem_ctx, mapi_object_t obj_paren
                stream.length = bin.cb;
                stream.data = talloc_size(mem_ctx, bin.cb);
                memcpy(stream.data, bin.lpb, bin.cb);
-               retval = WriteStream(&obj_stream, &stream);
+               retval = WriteStream(&obj_stream, &stream, &read_size);
                talloc_free(stream.data);
                if (retval != MAPI_E_SUCCESS) return false;
-
-               return true;
        } else {
-               for (size = 0, offset = - MAX_READ_SIZE; size <= bin.cb; size += MAX_READ_SIZE) {
-                       offset += MAX_READ_SIZE;
-                       stream.length = MAX_READ_SIZE;
-                       stream.data = talloc_size(mem_ctx, MAX_READ_SIZE);
-                       memcpy(stream.data, bin.lpb + offset, MAX_READ_SIZE);
-
-                       errno = 0;
-                       retval = WriteStream(&obj_stream, &stream);
-                       printf(".");
-                       fflush(0);
-                       if (retval != MAPI_E_SUCCESS) return false;
-                       talloc_free(stream.data);
-               }
-               if (size > bin.cb) {
-                       size = bin.cb - offset;         
+               /* initialize values */
+               size = MAX_READ_SIZE;
+               offset = 0;
+               while (offset <= bin.cb) {
                        stream.length = size;
                        stream.data = talloc_size(mem_ctx, size);
                        memcpy(stream.data, bin.lpb + offset, size);
-                       
-                       errno = 0;
-                       retval = WriteStream(&obj_stream, &stream);
-                       printf(".\n");
-                       fflush(0);
-                       if (retval != MAPI_E_SUCCESS) return false;
+
+                       retval = WriteStream(&obj_stream, &stream, &read_size);
                        talloc_free(stream.data);
+                       if (retval != MAPI_E_SUCCESS) return false;
+                       printf(".");
+                       fflush(0);
+
+                       /* Exit when there is nothing left to write */
+                       if (!read_size) return true;
+
+                       offset += read_size;
+                       size = read_size;
+
+                       if ((offset + size) > bin.cb) {
+                               size = bin.cb - offset;
+                       }
                }
        }
 
@@ -1656,6 +1652,7 @@ int main(int argc, const char *argv[])
                {"dtend", 0, POPT_ARG_STRING, NULL, OPT_MAPI_ENDDATE, "Set the event end date"},
                {"busystatus", 0, POPT_ARG_STRING, NULL, OPT_MAPI_BUSYSTATUS, "Set the item busy status"},
                {"taskstatus", 0, POPT_ARG_STRING, NULL, OPT_MAPI_TASKSTATUS, "Set the task status"},
+               {"priority", 0, POPT_ARG_STRING, NULL, OPT_MAPI_PRIORITY, "Set the task priority"},
                {"email", 0, POPT_ARG_STRING, NULL, OPT_MAPI_EMAIL, "set the email address"},
                {"fullname", 0, POPT_ARG_STRING, NULL, OPT_MAPI_FULLNAME, "set the full name"},
                {"cardname", 0, POPT_ARG_STRING, NULL, OPT_MAPI_CARDNAME, "set a contact card name"},