blob: 3d8d645a7761f7e8cc7d02d7c6cc439820c6dbb0 [file] [log] [blame]
Patrick Williamsc124f4f2015-09-15 14:41:29 -05001rpm: check if the argument(rootpath) exists or be writable
2
3When user execute the command "rpm -qai --root=$dir",if $dir doesn't
4exist or is unwritable as result of making a typo in rootpath,then
5it will create dirent $dir and subdirectory.
6So we should add the check function to fix it before creating relational
7subdirectory,and warn the incorrect rootpath to user. It just checks the
8rootpath reasonableness when the user input the argument(--root=/-r=).
9
10Upstream-Status: Pending
11
12Signed-off-by: Zhixiong Chi <zchi@windriver.com>
13---
14 rpmqv.c | 45 +++++++++++++++++++++++++++++++++++++++++++++
15 1 file changed, 45 insertions(+)
16
17diff --git a/rpmqv.c b/rpmqv.c
18index 40c42bd..88d85ab 100644
19--- a/rpmqv.c
20+++ b/rpmqv.c
21@@ -206,6 +206,8 @@ static struct poptOption optionsTable[] = {
22 POPT_TABLEEND
23 };
24
25+static int _rpmqv_rootpath_state = 0;
26+
27 #ifdef __MINT__
28 /* MiNT cannot dynamically increase the stack. */
29 long _stksize = 64 * 1024L;
30@@ -427,6 +429,41 @@ static void integrity_check(const char *progname, enum modes progmode_num)
31 }
32 #endif
33
34+/*check if the rootdir is writable or exists */
35+int access_file(const char *rootdir)
36+{
37+ int ret,rootdir_len;
38+
39+ if(rootdir == NULL) {
Patrick Williamsf1e5d692016-03-30 15:21:19 -050040+ return -1;
Patrick Williamsc124f4f2015-09-15 14:41:29 -050041+ }
42+
43+ rootdir_len = strlen(rootdir);
44+ /*make sure that dirent argument trailing is "/" */
45+ if(!(rootdir_len && rootdir[rootdir_len - 1] == '/')){
46+ char *t = (char *)malloc(rootdir_len + 2);
47+ *t = '\0';
48+ (void)stpcpy(stpcpy(t,rootdir),"/");
49+ ret = access(t,F_OK|W_OK);
50+ free(t);
51+ }else{
52+ ret = access(rootdir,F_OK|W_OK);
53+ }
54+ return ret;
55+}
56+
57+/*check if input the argument "--root/-r" */
58+void check_argument_root(int argc,char * const argv[])
59+{
60+ int i;
61+ for (i = 0; i < argc; i++) {
62+ if(strncmp(argv[i],"--root=",7) == 0 || strncmp(argv[i],"-r=",3) == 0) {
63+ _rpmqv_rootpath_state = 1;
64+ break;
65+ }
66+ }
67+}
68+
69 /*@-bounds@*/ /* LCL: segfault */
70 /*@-mods@*/ /* FIX: shrug */
71 #if !defined(__GLIBC__) && !defined(__LCLINT__)
72@@ -476,6 +513,8 @@ int main(int argc, const char ** argv)
73 int xx;
74 #endif
75
76+ check_argument_root(argc,(char *const *)argv);
77+
78 #if !defined(__GLIBC__) && !defined(__LCLINT__)
79 environ = envp;
80 #else
81@@ -715,6 +754,12 @@ int main(int argc, const char ** argv)
82 argerror(_("arguments to --root (-r) must begin with a /"));
83 break;
84 }
85+ if (_rpmqv_rootpath_state) {
86+ if (access_file(rpmioRootDir)) {
87+ fprintf(stderr, _("Invalid directory:%s, ensure it exists or be writable\n"),rpmioRootDir);
88+ exit(EXIT_FAILURE);
89+ }
90+ }
91 }
92
93 #if defined(RPM_VENDOR_OPENPKG) /* integrity-checking */
94--
951.9.1
96