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

Contents of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.63 - (show annotations)
Fri Feb 13 12:17:15 2004 UTC (20 years, 1 month ago) by lefcha
Branch: MAIN
CVS Tags: HEAD
Changes since 1.62: +5 -5 lines
File MIME type: text/plain
Stylistic changes.

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 options_t opts;
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);
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 } else {
34 check_dir_perms(hd, S_IRUSR | S_IWUSR | S_IXUSR);
35 }
36 return 0;
37 }
38
39
40 /*
41 * Check if a file exists.
42 */
43 int
44 exists_file(char *fname)
45 {
46 struct stat fs;
47
48 if (access(fname, F_OK))
49 return 0;
50
51 stat(fname, &fs);
52 if (!S_ISREG(fs.st_mode)) {
53 error("file %s not a regular file\n", fname);
54 return ERROR_FILEOPEN;
55 }
56 return 1;
57 }
58
59
60 /*
61 * Check if a directory exists.
62 */
63 int
64 exists_dir(char *dname)
65 {
66 struct stat ds;
67
68 if (access(dname, F_OK))
69 return 0;
70
71 stat(dname, &ds);
72 if (!S_ISDIR(ds.st_mode)) {
73 error("file %s not a directory\n", dname);
74 return ERROR_FILEOPEN;
75 }
76 return 1;
77 }
78
79
80 /*
81 * Create a file with the specified permissions.
82 */
83 int
84 create_file(char *fname, mode_t mode)
85 {
86 int fd;
87
88 fd = 0;
89
90 if (!exists_file(fname)) {
91 fd = open(fname, O_CREAT | O_WRONLY | O_TRUNC, mode);
92 if (fd == -1) {
93 error("could not create file %s; %s\n", fname,
94 strerror(errno));
95 return ERROR_FILEOPEN;
96 }
97 close(fd);
98 }
99 return 0;
100 }
101
102
103 /*
104 * Check the permissions of a file.
105 */
106 int
107 check_file_perms(char *fname, mode_t mode)
108 {
109 struct stat fs;
110
111 if (opts.verbosity <= -1)
112 return 0;
113
114 if (stat(fname, &fs)) {
115 error("getting file %s status; %s\n", fname,
116 strerror(errno));
117 return ERROR_TRIVIAL;
118 }
119 if (!S_ISREG(fs.st_mode)) {
120 error("file %s not a regular file\n", fname);
121 return ERROR_TRIVIAL;
122 }
123 if ((fs.st_mode & 00777) != mode) {
124 error("warning: file's %s mode should be %o not %o\n", fname,
125 mode, fs.st_mode & 00777);
126 return ERROR_TRIVIAL;
127 }
128 return 0;
129 }
130
131
132 /*
133 * Check the permissions of a directory.
134 */
135 int
136 check_dir_perms(char *dname, mode_t mode)
137 {
138 struct stat ds;
139
140 if (opts.verbosity <= -1)
141 return 0;
142
143 if (stat(dname, &ds)) {
144 error("getting file %s status; %s\n", dname,
145 strerror(errno));
146 return ERROR_TRIVIAL;
147 }
148 if (!S_ISDIR(ds.st_mode)) {
149 error("file %s not a directory\n", dname);
150 return ERROR_TRIVIAL;
151 }
152 if ((ds.st_mode & 00777) != mode) {
153 error("warning: dir's %s mode should be %o not %o\n", dname,
154 mode, ds.st_mode & 00777);
155 return ERROR_TRIVIAL;
156 }
157 return 0;
158 }

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26