summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTilman Sauerbeck <tilman@code-monkey.de>2016-01-30 09:57:39 +0100
committerJuergen Daubert <jue@jue.li>2016-01-30 12:52:06 +0100
commita44631cd68deab2b5cc3316ed94998a44018bcfb (patch)
treeee35a189c1fabfaac2a9efb5c799cf768f0c1e56
parentba955ea3c852885b0303f16ca2e3d9da0f3a800b (diff)
downloadprt-utils-a44631cd68deab2b5cc3316ed94998a44018bcfb.tar.gz
prt-utils-a44631cd68deab2b5cc3316ed94998a44018bcfb.tar.xz
revdep: When mmap()-ing a file, don't keep the fd around.
We can close the file as soon as mmap() succeeded.
-rw-r--r--revdep/package.c9
-rw-r--r--revdep/utility.c24
-rw-r--r--revdep/utility.h4
3 files changed, 19 insertions, 18 deletions
diff --git a/revdep/package.c b/revdep/package.c
index 2385040..4acb723 100644
--- a/revdep/package.c
+++ b/revdep/package.c
@@ -26,7 +26,6 @@
extern PackageList *packages_load_from_database(const char *path)
{
- int fd;
char *text;
unsigned int length;
char name[1024];
@@ -98,7 +97,7 @@ extern PackageList *packages_load_from_database(const char *path)
}
}
- if(open_file_in_memory(path, &fd, &text, &length) == -1)
+ if(open_file_in_memory(path, &text, &length) == -1)
{
return NULL;
}
@@ -111,18 +110,18 @@ extern PackageList *packages_load_from_database(const char *path)
if(parse_file_in_memory(text, parse_callback) == -1)
{
packages_free(pkgs);
- close_file_in_memory(fd, text, length);
+ close_file_in_memory(text, length);
return NULL;
}
if(pkgs->length == 0)
{
packages_free(pkgs);
- close_file_in_memory(fd, text, length);
+ close_file_in_memory(text, length);
return NULL;
}
- close_file_in_memory(fd, text, length);
+ close_file_in_memory(text, length);
return pkgs;
}
diff --git a/revdep/utility.c b/revdep/utility.c
index fcf6b41..67247e1 100644
--- a/revdep/utility.c
+++ b/revdep/utility.c
@@ -143,46 +143,48 @@ extern unsigned int string_crc32(const char *s)
return hash;
}
-extern int open_file_in_memory(const char *path, int *fd, char **text, unsigned int *length)
+extern int open_file_in_memory(const char *path, char **text, unsigned int *length)
{
struct stat st;
+ int fd;
- if(path == NULL || path[0] == '\0' || fd == NULL || text == NULL || length == NULL)
+ if(path == NULL || path[0] == '\0' || text == NULL || length == NULL)
{
errno = EINVAL;
return -1;
}
- if((fd[0] = open(path, O_RDONLY)) == -1)
+ if((fd = open(path, O_RDONLY)) == -1)
{
return -1;
}
- if(fstat(fd[0], &st) == -1)
+ if(fstat(fd, &st) == -1)
{
- close(fd[0]);
+ close(fd);
return -1;
}
- if((text[0] = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd[0], 0)) == NULL)
+ if((text[0] = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) == NULL)
{
- close(fd[0]);
+ close(fd);
return -1;
}
+ // We don't need to keep the fd around.
+ close(fd);
+
length[0] = st.st_size;
return 0;
}
-extern void close_file_in_memory(int fd, char *text, unsigned int length)
+extern void close_file_in_memory(char *text, unsigned int length)
{
- if(fd == -1 || text == NULL || length == 0)
+ if(text == NULL || length == 0)
return;
munmap(text, length);
-
- close(fd);
}
extern int parse_file_in_memory(char *text, parse_cb_t cb)
diff --git a/revdep/utility.h b/revdep/utility.h
index d98ade1..34c02b1 100644
--- a/revdep/utility.h
+++ b/revdep/utility.h
@@ -37,7 +37,7 @@ extern char *xstrdup(const char *in);
extern void logger(int level, const char *fmt, ...) __attribute__((format(printf,2,3)));
extern char *strtrim(char *s);
extern unsigned int string_crc32(const char *s);
-extern int open_file_in_memory(const char *path, int *fd, char **text, unsigned int *length);
-extern void close_file_in_memory(int fd, char *text, unsigned int length);
+extern int open_file_in_memory(const char *path, char **text, unsigned int *length);
+extern void close_file_in_memory(char *text, unsigned int length);
extern int parse_file_in_memory(char *text, parse_cb_t cb);
extern int get_ld_for_file(const char *pkg, const char *path, const char **ld);

Generated by cgit