]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libfrog/projects.c
xfsprogs: remove write-only variables
[thirdparty/xfsprogs-dev.git] / libfrog / projects.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (c) 2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
5 */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include "project.h"
11
12 #define PROJID "/etc/projid"
13 #define PROJECT_PATHS "/etc/projects"
14 char *projid_file;
15 char *projects_file;
16
17 static FILE *projects;
18 static fs_project_t p;
19 static char projects_buffer[512];
20
21 static FILE *project_paths;
22 static fs_project_path_t pp;
23 static char project_paths_buffer[1024];
24
25 void
26 setprfiles(void)
27 {
28 if (!projid_file)
29 projid_file = PROJID;
30 if (!projects_file)
31 projects_file = PROJECT_PATHS;
32 }
33
34 void
35 setprent(void)
36 {
37 setprfiles();
38 projects = fopen(projid_file, "r");
39 }
40
41 void
42 setprpathent(void)
43 {
44 setprfiles();
45 project_paths = fopen(projects_file, "r");
46 }
47
48 void
49 endprent(void)
50 {
51 if (projects)
52 fclose(projects);
53 projects = NULL;
54 }
55
56 void
57 endprpathent(void)
58 {
59 if (project_paths)
60 fclose(project_paths);
61 project_paths = NULL;
62 }
63
64 fs_project_t *
65 getprent(void)
66 {
67 char *idstart, *idend;
68 size_t size = sizeof(projects_buffer) - 1;
69
70 if (!projects)
71 return NULL;
72 for (;;) {
73 if (!fgets(projects_buffer, size, projects))
74 break;
75 /*
76 * /etc/projid file format -- "name:id\n", ignore "^#..."
77 */
78 if (projects_buffer[0] == '#')
79 continue;
80 idstart = strchr(projects_buffer, ':');
81 if (!idstart)
82 continue;
83 if ((idstart + 1) - projects_buffer >= size)
84 continue;
85 idend = strchr(idstart+1, ':');
86 if (idend)
87 *idend = '\0';
88 *idstart = '\0';
89 p.pr_prid = atoi(idstart+1);
90 p.pr_name = &projects_buffer[0];
91 return &p;
92 }
93
94 return NULL;
95 }
96
97 fs_project_t *
98 getprnam(
99 char *name)
100 {
101 fs_project_t *p = NULL;
102
103 setprent();
104 while ((p = getprent()) != NULL)
105 if (strcmp(p->pr_name, name) == 0)
106 break;
107 endprent();
108 return p;
109 }
110
111 fs_project_t *
112 getprprid(
113 prid_t prid)
114 {
115 fs_project_t *p = NULL;
116
117 setprent();
118 while ((p = getprent()) != NULL)
119 if (p->pr_prid == prid)
120 break;
121 endprent();
122 return p;
123 }
124
125 fs_project_path_t *
126 getprpathent(void)
127 {
128 char *nmstart, *nmend;
129 size_t size = sizeof(project_paths_buffer) - 1;
130
131 if (!project_paths)
132 return NULL;
133 for (;;) {
134 if (!fgets(project_paths_buffer, size, project_paths))
135 break;
136 /*
137 * /etc/projects format -- "id:pathname\n", ignore "^#..."
138 */
139 if (project_paths_buffer[0] == '#')
140 continue;
141 nmstart = strchr(project_paths_buffer, ':');
142 if (!nmstart)
143 continue;
144 if ((nmstart + 1) - project_paths_buffer >= size)
145 continue;
146 nmend = strchr(nmstart + 1, '\n');
147 if (nmend)
148 *nmend = '\0';
149 *nmstart = '\0';
150 pp.pp_pathname = nmstart + 1;
151 pp.pp_prid = atoi(&project_paths_buffer[0]);
152 return &pp;
153 }
154
155 return NULL;
156 }
157
158
159 int
160 getprojid(
161 const char *name,
162 int fd,
163 prid_t *projid)
164 {
165 struct fsxattr fsx;
166
167 if (xfsctl(name, fd, FS_IOC_FSGETXATTR, &fsx)) {
168 perror("FS_IOC_FSGETXATTR");
169 return -1;
170 }
171 *projid = fsx.fsx_projid;
172 return 0;
173 }
174
175 int
176 setprojid(
177 const char *name,
178 int fd,
179 prid_t projid)
180 {
181 struct fsxattr fsx;
182 int error;
183
184 if ((error = xfsctl(name, fd, FS_IOC_FSGETXATTR, &fsx)) == 0) {
185 fsx.fsx_projid = projid;
186 error = xfsctl(name, fd, FS_IOC_FSSETXATTR, &fsx);
187 }
188 return error;
189 }