1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
29a3993d4SArnaldo Carvalho de Melo #include "path.h"
3fa0d9846SArnaldo Carvalho de Melo #include "cache.h"
49a3993d4SArnaldo Carvalho de Melo #include <linux/kernel.h>
5175729fcSArnaldo Carvalho de Melo #include <limits.h>
69a3993d4SArnaldo Carvalho de Melo #include <stdio.h>
7fa0d9846SArnaldo Carvalho de Melo #include <string.h>
89a3993d4SArnaldo Carvalho de Melo #include <sys/types.h>
99a3993d4SArnaldo Carvalho de Melo #include <sys/stat.h>
1006c3f2aaSJiri Olsa #include <dirent.h>
119a3993d4SArnaldo Carvalho de Melo #include <unistd.h>
1286470930SIngo Molnar
cleanup_path(char * path)1386470930SIngo Molnar static char *cleanup_path(char *path)
1486470930SIngo Molnar {
1586470930SIngo Molnar /* Clean it up */
1686470930SIngo Molnar if (!memcmp(path, "./", 2)) {
1786470930SIngo Molnar path += 2;
1886470930SIngo Molnar while (*path == '/')
1986470930SIngo Molnar path++;
2086470930SIngo Molnar }
2186470930SIngo Molnar return path;
2286470930SIngo Molnar }
2386470930SIngo Molnar
mkpath(char * path_buf,size_t sz,const char * fmt,...)24*370ce164SIan Rogers char *mkpath(char *path_buf, size_t sz, const char *fmt, ...)
2586470930SIngo Molnar {
2686470930SIngo Molnar va_list args;
2786470930SIngo Molnar unsigned len;
2886470930SIngo Molnar
2986470930SIngo Molnar va_start(args, fmt);
30*370ce164SIan Rogers len = vsnprintf(path_buf, sz, fmt, args);
3186470930SIngo Molnar va_end(args);
32*370ce164SIan Rogers if (len >= sz)
33*370ce164SIan Rogers strncpy(path_buf, "/bad-path/", sz);
34*370ce164SIan Rogers return cleanup_path(path_buf);
3586470930SIngo Molnar }
369a3993d4SArnaldo Carvalho de Melo
path__join(char * bf,size_t size,const char * path1,const char * path2)379a3993d4SArnaldo Carvalho de Melo int path__join(char *bf, size_t size, const char *path1, const char *path2)
389a3993d4SArnaldo Carvalho de Melo {
399a3993d4SArnaldo Carvalho de Melo return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
409a3993d4SArnaldo Carvalho de Melo }
419a3993d4SArnaldo Carvalho de Melo
path__join3(char * bf,size_t size,const char * path1,const char * path2,const char * path3)429a3993d4SArnaldo Carvalho de Melo int path__join3(char *bf, size_t size, const char *path1, const char *path2, const char *path3)
439a3993d4SArnaldo Carvalho de Melo {
449a3993d4SArnaldo Carvalho de Melo return scnprintf(bf, size, "%s%s%s%s%s", path1, path1[0] ? "/" : "",
459a3993d4SArnaldo Carvalho de Melo path2, path2[0] ? "/" : "", path3);
469a3993d4SArnaldo Carvalho de Melo }
479a3993d4SArnaldo Carvalho de Melo
is_regular_file(const char * file)489a3993d4SArnaldo Carvalho de Melo bool is_regular_file(const char *file)
499a3993d4SArnaldo Carvalho de Melo {
509a3993d4SArnaldo Carvalho de Melo struct stat st;
519a3993d4SArnaldo Carvalho de Melo
529a3993d4SArnaldo Carvalho de Melo if (stat(file, &st))
539a3993d4SArnaldo Carvalho de Melo return false;
549a3993d4SArnaldo Carvalho de Melo
559a3993d4SArnaldo Carvalho de Melo return S_ISREG(st.st_mode);
569a3993d4SArnaldo Carvalho de Melo }
5706c3f2aaSJiri Olsa
5806c3f2aaSJiri Olsa /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
is_directory(const char * base_path,const struct dirent * dent)5906c3f2aaSJiri Olsa bool is_directory(const char *base_path, const struct dirent *dent)
6006c3f2aaSJiri Olsa {
6106c3f2aaSJiri Olsa char path[PATH_MAX];
6206c3f2aaSJiri Olsa struct stat st;
6306c3f2aaSJiri Olsa
6441204da4SCarsten Haitzler snprintf(path, sizeof(path), "%s/%s", base_path, dent->d_name);
6506c3f2aaSJiri Olsa if (stat(path, &st))
6606c3f2aaSJiri Olsa return false;
6706c3f2aaSJiri Olsa
6806c3f2aaSJiri Olsa return S_ISDIR(st.st_mode);
6906c3f2aaSJiri Olsa }
7041204da4SCarsten Haitzler
is_executable_file(const char * base_path,const struct dirent * dent)7141204da4SCarsten Haitzler bool is_executable_file(const char *base_path, const struct dirent *dent)
7241204da4SCarsten Haitzler {
7341204da4SCarsten Haitzler char path[PATH_MAX];
7441204da4SCarsten Haitzler struct stat st;
7541204da4SCarsten Haitzler
7641204da4SCarsten Haitzler snprintf(path, sizeof(path), "%s/%s", base_path, dent->d_name);
7741204da4SCarsten Haitzler if (stat(path, &st))
7841204da4SCarsten Haitzler return false;
7941204da4SCarsten Haitzler
8041204da4SCarsten Haitzler return !S_ISDIR(st.st_mode) && (st.st_mode & S_IXUSR);
8141204da4SCarsten Haitzler }
82