blob: a3774bca23cbc3a16d3b58a2ee6be9ff3005ed66 [file] [log] [blame]
David Hendricks9e9fca22012-05-22 20:55:29 -07001/*
David Hendricks80cbf942015-10-30 18:24:40 -07002 * Copyright 2015, The Chromium OS Authors
David Hendricks9e9fca22012-05-22 20:55:29 -07003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * * Redistributions in binary form must reproduce the above
12 * copyright notice, this list of conditions and the following
13 * disclaimer in the documentation and/or other materials provided
14 * with the distribution.
15 * * Neither the name of Google Inc. nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 *
31 * scanft() is derived from mosys source,s which were released under the
32 * BSD license
33 */
34
35#include <dirent.h>
36#include <errno.h>
David Hendricks80cbf942015-10-30 18:24:40 -070037#include <glob.h>
David Hendricks9e9fca22012-05-22 20:55:29 -070038#include <limits.h>
39#include <stdio.h>
40#include <stdlib.h>
41#include <string.h>
42#include <unistd.h>
David Hendricks80cbf942015-10-30 18:24:40 -070043#include <arpa/inet.h>
David Hendricks9e9fca22012-05-22 20:55:29 -070044#include <sys/fcntl.h>
45#include <sys/stat.h>
46#include <sys/types.h>
47
Nikolai Artemiev7ecd7c42021-02-12 11:43:14 +110048#include "file.h"
David Hendricks9e9fca22012-05-22 20:55:29 -070049#include "flash.h"
50
Brian Norris09388942016-03-28 12:57:01 -070051/* Like strstr(), but allowing NULL bytes within haystack */
52static int __find_string(const char *haystack, size_t hlen, const char *needle)
53{
54 const char *p = haystack;
55 const char *end = haystack + hlen;
56
57 while (p < end) {
58 if (strstr(p, needle))
59 return 1;
60 p = memchr(p, '\0', end - p);
61 if (!p)
62 /* Not found? We're at the end */
63 return 0;
64 else
65 /* Skip past the NULL separator */
66 p++;
67 }
68
69 return 0;
70}
71
David Hendricks1d2f41b2015-11-03 15:48:20 -080072/* returns 1 if string if found, 0 if not, and <0 to indicate error */
73static int find_string(const char *path, const char *str)
David Hendricks9e9fca22012-05-22 20:55:29 -070074{
Brian Norris09388942016-03-28 12:57:01 -070075 char contents[4096];
76 int fd, ret;
77 ssize_t len;
David Hendricks9e9fca22012-05-22 20:55:29 -070078
Brian Norris09388942016-03-28 12:57:01 -070079 msg_pdbg("%s: checking path \"%s\" for contents \"%s\"\n",
80 __func__, path, str);
David Hendricks9e9fca22012-05-22 20:55:29 -070081
David Hendricks1d2f41b2015-11-03 15:48:20 -080082 fd = open(path, O_RDONLY, 0);
83 if (fd < 0) {
84 msg_gerr("Cannot open file \"%s\"\n", path);
85 ret = -1;
86 goto find_string_exit_0;
87 }
88
Brian Norris09388942016-03-28 12:57:01 -070089 /* mmap() (or even read() with a file length) would be nice but these
90 * might not be implemented for files in sysfs and procfs.
91 * Let's also leave room for a terminator. */
92 len = read(fd, contents, sizeof(contents) - 1);
93 if (len == -1) {
94 msg_gerr("Cannot read file \"%s\"\n", path);
95 ret = -1;
96 goto find_string_exit_1;
David Hendricks1d2f41b2015-11-03 15:48:20 -080097 }
Brian Norris09388942016-03-28 12:57:01 -070098 /* Terminate the contents, in case they weren't terminated for us */
99 contents[len++] = '\0';
100 ret = __find_string(contents, len, str);
David Hendricks1d2f41b2015-11-03 15:48:20 -0800101
102find_string_exit_1:
103 close(fd);
104find_string_exit_0:
David Hendricks9e9fca22012-05-22 20:55:29 -0700105 return ret;
106}
107
Brian Norris09388942016-03-28 12:57:01 -0700108/*
David Hendricks1d2f41b2015-11-03 15:48:20 -0800109 * scanft - scan filetree for file with option to search for content
David Hendricks9e9fca22012-05-22 20:55:29 -0700110 *
111 * @root: Where to begin search
112 * @filename: Name of file to search for
David Hendricks1d2f41b2015-11-03 15:48:20 -0800113 * @str: Optional NULL terminated string to search for
David Hendricks9e9fca22012-05-22 20:55:29 -0700114 * @symdepth: Maximum depth of symlinks to follow. A negative value means
115 * follow indefinitely. Zero means do not follow symlinks.
116 *
117 * The caller should be specific enough with root and symdepth arguments
118 * to avoid finding duplicate information (especially in sysfs).
119 *
Brian Norris09388942016-03-28 12:57:01 -0700120 * Also, note that we may only scan a bounded portion of the beginning of the
121 * file for a match.
122 *
David Hendricks9e9fca22012-05-22 20:55:29 -0700123 * returns allocated string with path of matching file if successful
124 * returns NULL to indicate failure
125 */
126const char *scanft(const char *root, const char *filename,
127 const char *str, int symdepth)
128{
129 DIR *dp;
130 struct dirent *d;
131 struct stat s;
132 const char *ret = NULL;
133
134 if (lstat(root, &s) < 0) {
135 msg_pdbg("%s: Error stat'ing %s: %s\n",
136 __func__, root, strerror(errno));
137 return NULL;
138 }
139
140 if (S_ISLNK(s.st_mode)) {
141 if (symdepth == 0) /* Leaf has been reached */
142 return NULL;
143 else if (symdepth > 0) /* Follow if not too deep in */
144 symdepth--;
David Hendricks0cc34e72013-11-12 15:06:42 -0800145 } else if (!S_ISDIR(s.st_mode)) {
146 return NULL;
David Hendricks9e9fca22012-05-22 20:55:29 -0700147 }
148
149 if ((dp = opendir(root)) == NULL)
150 return NULL;
151
152 while (!ret && (d = readdir(dp))) {
153 char newpath[PATH_MAX];
154
155 /* Skip "." and ".." */
156 if (!(strcmp(d->d_name, ".")) ||
157 !(strcmp(d->d_name, "..")))
158 continue;
159
160 snprintf(newpath, sizeof(newpath), "%s/%s", root, d->d_name);
161
162 if (!strcmp(d->d_name, filename)) {
David Hendricks1d2f41b2015-11-03 15:48:20 -0800163 if (!str || (find_string(newpath, str) == 1))
David Hendricks9e9fca22012-05-22 20:55:29 -0700164 ret = strdup(newpath);
165 }
166
167 if (!ret)
168 ret = scanft(newpath, filename, str, symdepth);
169 }
170
171 closedir(dp);
172 return ret;
173}