r10592: Add "adfgets()" function, which allocates and reads a line from a fd.
authorJelmer Vernooij <jelmer@samba.org>
Wed, 28 Sep 2005 21:54:29 +0000 (21:54 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:39:08 +0000 (13:39 -0500)
Also add torture tests for this function and file_{load,save}. I've hardcoded
a file name here.. should I handle that neater somehow?

source/lib/util_file.c
source/torture/config.mk
source/torture/local/util_file.c [new file with mode: 0644]
source/torture/torture.c

index 156d09aaf5b6a002b66aeb0e7af771dccd855fa4..77066e78cc2bf055c0a488cacde1deedc393e4bd 100644 (file)
@@ -2,6 +2,8 @@
  * Unix SMB/CIFS implementation.
  * SMB parameters and setup
  * Copyright (C) Andrew Tridgell 1992-1998 Modified by Jeremy Allison 1995.
+ *
+ * Added afdgets() Jelmer Vernooij 2005
  * 
  * This program is free software; you can redistribute it and/or modify it under
  * the terms of the GNU General Public License as published by the Free
@@ -96,6 +98,52 @@ char *fgets_slash(char *s2,int maxlen,XFILE *f)
   return(s);
 }
 
+/* Read one line (data until next newline or eof) and allocate it */
+char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
+{
+       char *data = NULL;
+       ssize_t alloc_size = 0, offset = 0, ret;
+       int p;
+
+       if (hint <= 0) hint = 0x100;
+
+       do {
+               alloc_size += hint;
+
+               data = talloc_realloc(mem_ctx, data, char, alloc_size);
+
+               if (!data)
+                       return NULL;
+
+               ret = read(fd, data + offset, hint);
+
+               if (ret == -1) {
+                       talloc_free(data);
+                       return NULL;
+               }
+
+               /* Find newline */
+               for (p = 0; p < ret; p++) {
+                       if (data[offset + p] == '\n')
+                               break;
+               }
+
+               if (p < ret) {
+                       data[offset + p] = '\0';
+
+                       /* Go back to position of newline */
+                       lseek(fd, p - ret + 1, SEEK_CUR);
+                       return data;
+               }
+
+               offset += ret;
+
+       } while (ret == hint);
+
+       data[offset] = '\0';
+
+       return data;
+}
 
 
 /****************************************************************************
@@ -287,7 +335,7 @@ void file_lines_slashcont(char **lines)
 /*
   save a lump of data into a file. Mostly used for debugging 
 */
-BOOL file_save(const char *fname, void *packet, size_t length)
+BOOL file_save(const char *fname, const void *packet, size_t length)
 {
        int fd;
        fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0644);
index 12665c8e80f65a24d26002fcd735badf0332ecfd..2aac08e3adad926c0941a5808a173917b34c7790 100644 (file)
@@ -149,7 +149,8 @@ ADD_OBJ_FILES = \
                torture/local/socket.o \
                torture/local/irpc.o \
                torture/local/resolve.o \
-               torture/local/util_strlist.o
+               torture/local/util_strlist.o \
+               torture/local/util_file.o
 REQUIRED_SUBSYSTEMS = \
                LIBSMB \
                MESSAGING
diff --git a/source/torture/local/util_file.c b/source/torture/local/util_file.c
new file mode 100644 (file)
index 0000000..5c5047d
--- /dev/null
@@ -0,0 +1,101 @@
+/* 
+   Unix SMB/CIFS implementation.
+
+   util_file testing
+
+   Copyright (C) Jelmer Vernooij 2005
+   
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 2 of the License, or
+   (at your option) any later version.
+   
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+   
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+*/
+
+#include "includes.h"
+#include "system/filesys.h"
+
+#define TEST_FILENAME "utilfile.test"
+#define TEST_LINE1 "This is list line 1..."
+#define TEST_LINE2 ".. and this is line 2"
+#define TEST_LINE3 "and end of the file"
+
+#define TEST_DATA TEST_LINE1 "\n" TEST_LINE2 "\n" TEST_LINE3
+
+static BOOL test_file_load_save(TALLOC_CTX *mem_ctx)
+{
+       BOOL ret;
+       size_t len;
+       char *data;
+       
+       ret = file_save(TEST_FILENAME, TEST_DATA, strlen(TEST_DATA));
+       if (!ret)
+               return False;
+
+       data = file_load(TEST_FILENAME, &len, mem_ctx);
+       if (!data) 
+               return False;
+
+       if (len != strlen(TEST_DATA))
+               return False;
+       
+       if (memcmp(data, TEST_DATA, len) != 0)
+               return False;
+
+       unlink(TEST_FILENAME);
+
+       return True;
+}
+
+static BOOL test_afdgets(TALLOC_CTX *mem_ctx)
+{
+       int fd;
+       char *line;
+       
+       if (!file_save(TEST_FILENAME, (const void *)TEST_DATA, strlen(TEST_DATA)))
+               return False;
+
+       fd = open(TEST_FILENAME, O_RDONLY);
+       
+       if (fd == -1) 
+               return False;
+
+       line = afdgets(fd, mem_ctx, 8);
+       if (strcmp(line, TEST_LINE1) != 0) 
+               return False;
+
+       line = afdgets(fd, mem_ctx, 8);
+       if (strcmp(line, TEST_LINE2) != 0) 
+               return False;
+
+       line = afdgets(fd, mem_ctx, 8);
+       if (strcmp(line, TEST_LINE3) != 0) 
+               return False;
+
+       close(fd);
+
+       unlink(TEST_FILENAME);
+
+       return True;
+}
+
+BOOL torture_local_util_file(void) 
+{
+       BOOL ret = True;
+       TALLOC_CTX *mem_ctx = talloc_init("test_util_file");
+
+       ret &= test_file_load_save(mem_ctx);
+       ret &= test_afdgets(mem_ctx);
+
+       talloc_free(mem_ctx);
+
+       return ret;
+}
index e4284d5ba0258794b669138cdca2ddcac9b98de5..dfc63e6bd60910af9756c1d3c925b91f3c5a039e 100644 (file)
@@ -2322,6 +2322,7 @@ static struct {
        {"LOCAL-IRPC",  torture_local_irpc, 0},
        {"LOCAL-BINDING", torture_local_binding_string, 0},
        {"LOCAL-STRLIST", torture_local_util_strlist, 0},
+       {"LOCAL-FILE", torture_local_util_file, 0},
        {"LOCAL-IDTREE", torture_local_idtree, 0},
        {"LOCAL-SOCKET", torture_local_socket, 0},
        {"LOCAL-PAC", torture_pac, 0},