Skip to content

Commit

Permalink
modpost: add read_text_file() and get_line() helpers
Browse files Browse the repository at this point in the history
modpost uses grab_file() to open a file, but it is not suitable for
a text file because the mmap'ed file is not terminated by null byte.
Actually, I see some issues for the use of grab_file().

The new helper, read_text_file() loads the whole file content into a
malloc'ed buffer, and appends a null byte. Then, get_line() reads
each line.

To handle text files, I intend to replace as follows:

  grab_file()    -> read_text_file()
  get_new_line() -> get_line()

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
  • Loading branch information
Masahiro Yamada committed Jun 6, 2020
1 parent 4ddea2f commit ac5100f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
49 changes: 49 additions & 0 deletions scripts/mod/modpost.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,55 @@ void *do_nofail(void *ptr, const char *expr)
return ptr;
}

char *read_text_file(const char *filename)
{
struct stat st;
size_t nbytes;
int fd;
char *buf;

fd = open(filename, O_RDONLY);
if (fd < 0) {
perror(filename);
exit(1);
}

if (fstat(fd, &st) < 0) {
perror(filename);
exit(1);
}

buf = NOFAIL(malloc(st.st_size + 1));

nbytes = st.st_size;

while (nbytes) {
ssize_t bytes_read;

bytes_read = read(fd, buf, nbytes);
if (bytes_read < 0) {
perror(filename);
exit(1);
}

nbytes -= bytes_read;
}
buf[st.st_size] = '\0';

close(fd);

return buf;
}

char *get_line(char **stringp)
{
/* do not return the unwanted extra line at EOF */
if (*stringp && **stringp == '\0')
return NULL;

return strsep(stringp, "\n");
}

/* A list of all modules we processed */
static struct module *modules;

Expand Down
2 changes: 2 additions & 0 deletions scripts/mod/modpost.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ void add_moddevtable(struct buffer *buf, struct module *mod);
void get_src_version(const char *modname, char sum[], unsigned sumlen);

/* from modpost.c */
char *read_text_file(const char *filename);
char *get_line(char **stringp);
void *grab_file(const char *filename, unsigned long *size);
char* get_next_line(unsigned long *pos, void *file, unsigned long size);
void release_file(void *file, unsigned long size);
Expand Down

0 comments on commit ac5100f

Please sign in to comment.