/[imapfilter]/imapfilter/file.c
ViewVC logotype

Contents of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.59 - (show annotations)
Sat Feb 7 23:54:16 2004 UTC (20 years, 2 months ago) by lefcha
Branch: MAIN
Changes since 1.58: +13 -16 lines
File MIME type: text/plain
Removed CHECK_PERMISSIONS compilation option and added variable to control permissions checking.

1 #include <stdio.h>
2 #include <unistd.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <limits.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
9
10 #include "config.h"
11 #include "imapfilter.h"
12 #include "pathnames.h"
13
14
15 extern char *home;
16 extern unsigned int options;
17
18
19 /*
20 * Create imapfilter's home directory.
21 */
22 int
23 create_homedir(void)
24 {
25 char hd[PATH_MAX];
26
27 snprintf(hd, PATH_MAX, "%s/%s", home, PATHNAME_HOME_DIR);
28
29 if (!exists_dir(hd)) {
30 if (mkdir(hd, S_IRUSR | S_IWUSR | S_IXUSR))
31 error("could not create directory %s; %s\n", hd,
32 strerror(errno));
33 }
34 return 0;
35 }
36
37
38 /*
39 * Check if a file exists.
40 */
41 int
42 exists_file(char *fname)
43 {
44 struct stat fs;
45
46 if (access(fname, F_OK))
47 return 0;
48
49 stat(fname, &fs);
50 if (!S_ISREG(fs.st_mode)) {
51 error("file %s not a regular file\n", fname);
52 return ERROR_FILE_OPEN;
53 }
54 return 1;
55 }
56
57
58 /*
59 * Check if a directory exists.
60 */
61 int
62 exists_dir(char *dname)
63 {
64 struct stat ds;
65
66 if (access(dname, F_OK))
67 return 0;
68
69 stat(dname, &ds);
70 if (!S_ISDIR(ds.st_mode)) {
71 error("file %s not a directory\n", dname);
72 return ERROR_FILE_OPEN;
73 }
74 return 1;
75 }
76
77
78 /*
79 * Create a file with the specified permissions.
80 */
81 int
82 create_file(char *fname, mode_t mode)
83 {
84 int fd;
85
86 fd = 0;
87
88 if (!exists_file(fname)) {
89 fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
90 if (fd == -1) {
91 error("could not create file %s; %s\n", fname,
92 strerror(errno));
93 return ERROR_FILE_OPEN;
94 }
95 close(fd);
96 }
97 return 0;
98 }
99
100
101 /*
102 * Check the permissions of a file.
103 */
104 int
105 check_file_perms(char *fname, mode_t mode)
106 {
107 struct stat fs;
108
109 if (!(options & OPTION_PERMISSIONS))
110 return 0;
111
112 if (stat(fname, &fs)) {
113 error("getting file %s status; %s\n", fname,
114 strerror(errno));
115 return ERROR_TRIVIAL;
116 }
117 if (!S_ISREG(fs.st_mode)) {
118 error("file %s not a regular file\n", fname);
119 return ERROR_TRIVIAL;
120 }
121 if ((fs.st_mode & 00777) != mode) {
122 error("warning: file's %s mode should be %o not %o\n", fname,
123 mode, fs.st_mode & 00777);
124 return ERROR_TRIVIAL;
125 }
126 return 0;
127 }
128
129
130 /*
131 * Check the permissions of a directory.
132 *
133 int
134 check_dir_perms(char *dname, mode_t mode)
135 {
136 struct stat ds;
137
138 if (!(options & OPTION_PERMISSIONS))
139 return 0;
140
141 if (stat(dname, &ds)) {
142 error("getting file %s status; %s\n", dname,
143 strerror(errno));
144 return ERROR_TRIVIAL;
145 }
146 if (!S_ISDIR(ds.st_mode)) {
147 error("file %s not a directory\n", dname);
148 return ERROR_TRIVIAL;
149 }
150 if ((ds.st_mode & 00777) != mode) {
151 error("warning: dir's %s mode should be %o not %o\n", dname,
152 mode, ds.st_mode & 00777);
153 return ERROR_TRIVIAL;
154 }
155 return 0;
156 }*/

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26