summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorJelmer Vernooij <jelmer@samba.org>2008-10-12 17:34:43 +0200
committerJelmer Vernooij <jelmer@samba.org>2008-10-12 17:34:43 +0200
commit1b99d8fbb591bedb375c1251d5d29a5674e1b74a (patch)
tree5e4cf4d115081057750349373184ad7df8894e61 /lib
parent652f0e601da0d1d2e2c8b9281bbee9fa399d9877 (diff)
downloadsamba-1b99d8fbb591bedb375c1251d5d29a5674e1b74a.tar.gz
samba-1b99d8fbb591bedb375c1251d5d29a5674e1b74a.tar.bz2
samba-1b99d8fbb591bedb375c1251d5d29a5674e1b74a.zip
Use common util_file code.
Diffstat (limited to 'lib')
-rw-r--r--lib/util/params.c2
-rw-r--r--lib/util/util.h8
-rw-r--r--lib/util/util_file.c59
-rw-r--r--lib/util/util_tdb.c4
4 files changed, 53 insertions, 20 deletions
diff --git a/lib/util/params.c b/lib/util/params.c
index 3a9e2b9505..c03edec272 100644
--- a/lib/util/params.c
+++ b/lib/util/params.c
@@ -510,7 +510,7 @@ static myFILE *OpenConfFile( const char *FileName )
ret = talloc(talloc_autofree_context(), myFILE);
if (!ret) return NULL;
- ret->buf = file_load(FileName, &ret->size, ret);
+ ret->buf = file_load(FileName, &ret->size, 0, ret);
if( NULL == ret->buf )
{
DEBUG( 1,
diff --git a/lib/util/util.h b/lib/util/util.h
index 5cecad7350..720aa537a7 100644
--- a/lib/util/util.h
+++ b/lib/util/util.h
@@ -522,12 +522,12 @@ _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint);
/**
load a file into memory from a fd.
**/
-_PUBLIC_ char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char *fd_load(int fd, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx);
/**
load a file into memory
**/
-_PUBLIC_ char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx);
/**
mmap (if possible) or read a file
@@ -538,14 +538,14 @@ _PUBLIC_ void *map_file(const char *fname, size_t size);
load a file into memory and return an array of pointers to lines in the file
must be freed with talloc_free().
**/
-_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx);
/**
load a fd into memory and return an array of pointers to lines in the file
must be freed with talloc_free(). If convert is true calls unix_to_dos on
the list.
**/
-_PUBLIC_ char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx);
+_PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx);
/**
take a list of lines and modify them to produce a list where \ continues
diff --git a/lib/util/util_file.c b/lib/util/util_file.c
index c3e22196c0..176ff75e02 100644
--- a/lib/util/util_file.c
+++ b/lib/util/util_file.c
@@ -22,6 +22,11 @@
#include "includes.h"
#include "system/shmem.h"
#include "system/filesys.h"
+#if _SAMBA_BUILD_ == 3
+#undef malloc
+#undef realloc
+#define realloc_p(p, type, count) (type *)realloc_array(p, sizeof(type), count, false)
+#endif
/**
* @file
@@ -160,23 +165,30 @@ _PUBLIC_ char *afdgets(int fd, TALLOC_CTX *mem_ctx, size_t hint)
/**
load a file into memory from a fd.
**/
-_PUBLIC_ char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
+_PUBLIC_ char *fd_load(int fd, size_t *psize, size_t maxsize, TALLOC_CTX *mem_ctx)
{
struct stat sbuf;
char *p;
+ size_t size;
if (fstat(fd, &sbuf) != 0) return NULL;
- p = (char *)talloc_size(mem_ctx, sbuf.st_size+1);
+ size = sbuf.st_size;
+
+ if (maxsize) {
+ size = MIN(size, maxsize);
+ }
+
+ p = (char *)talloc_size(mem_ctx, size+1);
if (!p) return NULL;
- if (read(fd, p, sbuf.st_size) != sbuf.st_size) {
+ if (read(fd, p, size) != size) {
talloc_free(p);
return NULL;
}
- p[sbuf.st_size] = 0;
+ p[size] = 0;
- if (size) *size = sbuf.st_size;
+ if (psize) *psize = size;
return p;
}
@@ -184,7 +196,7 @@ _PUBLIC_ char *fd_load(int fd, size_t *size, TALLOC_CTX *mem_ctx)
/**
load a file into memory
**/
-_PUBLIC_ char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
+_PUBLIC_ char *file_load(const char *fname, size_t *size, size_t maxsize, TALLOC_CTX *mem_ctx)
{
int fd;
char *p;
@@ -194,7 +206,7 @@ _PUBLIC_ char *file_load(const char *fname, size_t *size, TALLOC_CTX *mem_ctx)
fd = open(fname,O_RDONLY);
if (fd == -1) return NULL;
- p = fd_load(fd, size, mem_ctx);
+ p = fd_load(fd, size, maxsize, mem_ctx);
close(fd);
@@ -224,7 +236,7 @@ _PUBLIC_ void *map_file(const char *fname, size_t size)
}
#endif
if (!p) {
- p = file_load(fname, &s2, talloc_autofree_context());
+ p = file_load(fname, &s2, 0, talloc_autofree_context());
if (!p) return NULL;
if (s2 != size) {
DEBUG(1,("incorrect size for %s - got %d expected %d\n",
@@ -237,12 +249,31 @@ _PUBLIC_ void *map_file(const char *fname, size_t size)
return p;
}
+/**
+ unmap or free memory
+**/
+
+bool unmap_file(void *start, size_t size)
+{
+#ifdef HAVE_MMAP
+ if (munmap( start, size ) != 0) {
+ DEBUG( 1, ("map_file: Failed to unmap address %p "
+ "of size %u - %s\n",
+ start, (unsigned int)size, strerror(errno) ));
+ return false;
+ }
+ return true;
+#else
+ talloc_free(start);
+ return true;
+#endif
+}
/**
parse a buffer into lines
'p' will be freed on error, and otherwise will be made a child of the returned array
**/
-static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
+char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *mem_ctx)
{
int i;
char *s, **ret;
@@ -288,12 +319,12 @@ static char **file_lines_parse(char *p, size_t size, int *numlines, TALLOC_CTX *
load a file into memory and return an array of pointers to lines in the file
must be freed with talloc_free().
**/
-_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, TALLOC_CTX *mem_ctx)
+_PUBLIC_ char **file_lines_load(const char *fname, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
{
char *p;
size_t size;
- p = file_load(fname, &size, mem_ctx);
+ p = file_load(fname, &size, maxsize, mem_ctx);
if (!p) return NULL;
return file_lines_parse(p, size, numlines, mem_ctx);
@@ -304,12 +335,12 @@ load a fd into memory and return an array of pointers to lines in the file
must be freed with talloc_free(). If convert is true calls unix_to_dos on
the list.
**/
-_PUBLIC_ char **fd_lines_load(int fd, int *numlines, TALLOC_CTX *mem_ctx)
+_PUBLIC_ char **fd_lines_load(int fd, int *numlines, size_t maxsize, TALLOC_CTX *mem_ctx)
{
char *p;
size_t size;
- p = fd_load(fd, &size, mem_ctx);
+ p = fd_load(fd, &size, maxsize, mem_ctx);
if (!p) return NULL;
return file_lines_parse(p, size, numlines, mem_ctx);
@@ -402,3 +433,5 @@ _PUBLIC_ bool large_file_support(const char *path)
close(fd);
return ret == 0;
}
+
+
diff --git a/lib/util/util_tdb.c b/lib/util/util_tdb.c
index 299f5f7c6a..2d6012c9f4 100644
--- a/lib/util/util_tdb.c
+++ b/lib/util/util_tdb.c
@@ -40,12 +40,12 @@ TDB_DATA make_tdb_data(const uint8_t *dptr, size_t dsize)
TDB_DATA string_tdb_data(const char *string)
{
- return make_tdb_data((const uint8 *)string, string ? strlen(string) : 0 );
+ return make_tdb_data((const uint8_t *)string, string ? strlen(string) : 0 );
}
TDB_DATA string_term_tdb_data(const char *string)
{
- return make_tdb_data((const uint8 *)string, string ? strlen(string) + 1 : 0);
+ return make_tdb_data((const uint8_t *)string, string ? strlen(string) + 1 : 0);
}
/****************************************************************************