forked from Minki/linux
fixdep: use malloc() and read() to load dep_file to buffer
Commit dee81e9886
("fixdep: faster CONFIG_ search") changed how to
read files in which CONFIG options are searched. It used malloc()
and read() instead of mmap() because it needed to zero-terminate the
buffer in order to use strstr(). print_deps() was left untouched
since there was no reason to change it.
Now, I have two motivations to change it in the same way.
- do_config_file() and print_deps() do quite similar things; they
open a file, load it onto memory, and pass it to a parser function.
If we use malloc() and read() for print_deps() too, we can factor
out the common code. (I will do this in the next commit.)
- parse_dep_file() copies each token to a temporary buffer because
it needs to zero-terminate it to be passed to printf(). It is not
possible to modify the buffer directly because it is mmap'ed with
O_RDONLY. If we load the file content into a malloc'ed buffer, we
can insert '\0' after each token, and save memcpy(). (I will do
this in the commit after next.)
Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
This commit is contained in:
parent
41f92cffba
commit
01b5cbe701
@ -104,7 +104,6 @@
|
|||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <sys/mman.h>
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@ -308,24 +307,27 @@ static void do_config_file(const char *filename)
|
|||||||
* assignments are parsed not only by make, but also by the rather simple
|
* assignments are parsed not only by make, but also by the rather simple
|
||||||
* parser in scripts/mod/sumversion.c.
|
* parser in scripts/mod/sumversion.c.
|
||||||
*/
|
*/
|
||||||
static void parse_dep_file(void *map, size_t len)
|
static void parse_dep_file(char *m)
|
||||||
{
|
{
|
||||||
char *m = map;
|
|
||||||
char *end = m + len;
|
|
||||||
char *p;
|
char *p;
|
||||||
char s[PATH_MAX];
|
char s[PATH_MAX];
|
||||||
int is_target;
|
int is_last, is_target;
|
||||||
int saw_any_target = 0;
|
int saw_any_target = 0;
|
||||||
int is_first_dep = 0;
|
int is_first_dep = 0;
|
||||||
|
|
||||||
while (m < end) {
|
while (1) {
|
||||||
/* Skip any "white space" */
|
/* Skip any "white space" */
|
||||||
while (m < end && (*m == ' ' || *m == '\\' || *m == '\n'))
|
while (*m == ' ' || *m == '\\' || *m == '\n')
|
||||||
m++;
|
m++;
|
||||||
|
|
||||||
|
if (!*m)
|
||||||
|
break;
|
||||||
|
|
||||||
/* Find next "white space" */
|
/* Find next "white space" */
|
||||||
p = m;
|
p = m;
|
||||||
while (p < end && *p != ' ' && *p != '\\' && *p != '\n')
|
while (*p && *p != ' ' && *p != '\\' && *p != '\n')
|
||||||
p++;
|
p++;
|
||||||
|
is_last = (*p == '\0');
|
||||||
/* Is the token we found a target name? */
|
/* Is the token we found a target name? */
|
||||||
is_target = (*(p-1) == ':');
|
is_target = (*(p-1) == ':');
|
||||||
/* Don't write any target names into the dependency file */
|
/* Don't write any target names into the dependency file */
|
||||||
@ -373,6 +375,10 @@ static void parse_dep_file(void *map, size_t len)
|
|||||||
do_config_file(s);
|
do_config_file(s);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (is_last)
|
||||||
|
break;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Start searching for next token immediately after the first
|
* Start searching for next token immediately after the first
|
||||||
* "whitespace" character that follows this token.
|
* "whitespace" character that follows this token.
|
||||||
@ -391,40 +397,42 @@ static void parse_dep_file(void *map, size_t len)
|
|||||||
printf("$(deps_%s):\n", target);
|
printf("$(deps_%s):\n", target);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void print_deps(void)
|
static void print_deps(const char *filename)
|
||||||
{
|
{
|
||||||
struct stat st;
|
struct stat st;
|
||||||
int fd;
|
int fd;
|
||||||
void *map;
|
char *buf;
|
||||||
|
|
||||||
fd = open(depfile, O_RDONLY);
|
fd = open(filename, O_RDONLY);
|
||||||
if (fd < 0) {
|
if (fd < 0) {
|
||||||
fprintf(stderr, "fixdep: error opening depfile: ");
|
fprintf(stderr, "fixdep: error opening depfile: ");
|
||||||
perror(depfile);
|
perror(filename);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
if (fstat(fd, &st) < 0) {
|
if (fstat(fd, &st) < 0) {
|
||||||
fprintf(stderr, "fixdep: error fstat'ing depfile: ");
|
fprintf(stderr, "fixdep: error fstat'ing depfile: ");
|
||||||
perror(depfile);
|
perror(filename);
|
||||||
exit(2);
|
exit(2);
|
||||||
}
|
}
|
||||||
if (st.st_size == 0) {
|
if (st.st_size == 0) {
|
||||||
fprintf(stderr,"fixdep: %s is empty\n",depfile);
|
|
||||||
close(fd);
|
close(fd);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
map = mmap(NULL, st.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
|
buf = malloc(st.st_size + 1);
|
||||||
if ((long) map == -1) {
|
if (!buf) {
|
||||||
perror("fixdep: mmap");
|
perror("fixdep: malloc");
|
||||||
close(fd);
|
exit(2);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
if (read(fd, buf, st.st_size) != st.st_size) {
|
||||||
parse_dep_file(map, st.st_size);
|
perror("fixdep: read");
|
||||||
|
exit(2);
|
||||||
munmap(map, st.st_size);
|
}
|
||||||
|
buf[st.st_size] = '\0';
|
||||||
close(fd);
|
close(fd);
|
||||||
|
|
||||||
|
parse_dep_file(buf);
|
||||||
|
|
||||||
|
free(buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
@ -440,7 +448,7 @@ int main(int argc, char *argv[])
|
|||||||
cmdline = argv[3];
|
cmdline = argv[3];
|
||||||
|
|
||||||
print_cmdline();
|
print_cmdline();
|
||||||
print_deps();
|
print_deps(depfile);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user