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

Contents of /imapfilter/file.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.57.2.1 - (show annotations)
Fri Aug 8 00:28:29 2003 UTC (20 years, 8 months ago) by lefcha
Branch: release-0_9-patches
Changes since 1.57: +10 -10 lines
File MIME type: text/plain
Removed chdir() to HOME and corrected header includes.

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

webmaster@linux.gr
ViewVC Help
Powered by ViewVC 1.1.26