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