ze-filter  (ze-filter-0.8.0-develop-180218)
zeFileTools.c
Go to the documentation of this file.
1 
2 /*
3  *
4  * ze-filter - Mail Server Filter for sendmail
5  *
6  * Copyright (c) 2001-2018 - Jose-Marcio Martins da Cruz
7  *
8  * Auteur : Jose Marcio Martins da Cruz
9  * jose.marcio.mc@gmail.org
10  *
11  * Historique :
12  * Creation : Mon Jun 30 23:57:04 CEST 2014
13  *
14  * This program is free software - GPL v2.,
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  */
21 
22 
23 #include <ze-sys.h>
24 #include <libze.h>
25 
26 /* ****************************************************************************
27  * *
28  * *
29  **************************************************************************** */
30 char *
31 zeMyBasename(out, in, size)
32  char *out;
33  char *in;
34  size_t size;
35 {
36  char *p, *t;
37 
38  if (in == NULL || out == NULL)
39  return NULL;
40 
41  if ((t = strdup(in)) == NULL)
42  return NULL;
43 
44  if ((p = strrchr(t, '/')) != NULL && *(p + 1) == '\0')
45  *p = '\0';
46 
47  if ((p = strrchr(t, '/')) != NULL) {
48  p++;
49  } else
50  p = t;
51 
52  strlcpy(out, p, size);
53  FREE(t);
54  return out;
55 }
56 
57 /* ****************************************************************************
58  * *
59  * *
60  ******************************************************************************/
61 
62 char *
64  char *path;
65 {
66  while (path != NULL && strlen(path) > 0) {
67  char *p;
68 
69  p = strchr(path, '/');
70  if (p == NULL)
71  return path;
72  path = ++p;
73  }
74 
75  return path;
76 }
77 
78 /* ****************************************************************************
79  * *
80  * *
81  ******************************************************************************/
82 static int
83 ssp_flock(fd, cmd, type)
84  int fd;
85  int cmd;
86  short type;
87 {
88  flock_t lock;
89 
90  memset(&lock, 0, sizeof (lock));
91  lock.l_type = type;
92  lock.l_whence = SEEK_SET;
93  lock.l_start = 0;
94  lock.l_len = 0;
95 
96  return fcntl(fd, cmd, &lock);
97 }
98 
99 bool
101  int fd;
102 {
103  if (ssp_flock(fd, F_SETLKW, F_WRLCK) < 0) {
104  ZE_LogSysError("lock error");
105  /*
106  * exit (EX_SOFTWARE);
107  */
108  return FALSE;
109  }
110  return TRUE;
111 }
112 
113 bool
115  int fd;
116 {
117  if (ssp_flock(fd, F_SETLK, F_UNLCK) < 0) {
118  ZE_LogSysError("lock error");
119  /*
120  * exit (EX_SOFTWARE);
121  */
122  return FALSE;
123  }
124  return TRUE;
125 }
126 
127 /* ****************************************************************************
128  * *
129  * *
130  ******************************************************************************/
131 size_t
133  char *fname;
134 {
135  struct stat fstat;
136 
137  if (fname == NULL)
138  return 0;
139 
140  if (stat(fname, &fstat) == 0)
141  return fstat.st_size;
142 
143 #if 0
144  ZE_LogSysError("stat(%s) error", fname);
145 #endif
146 
147  return 0;
148 }
149 
150 /* ****************************************************************************
151  * *
152  * *
153  ******************************************************************************/
154 size_t
156  int fd;
157 {
158  struct stat st;
159 
160  if (fd < 0)
161  return 0;
162 
163  if (fstat(fd, &st) == 0)
164  return st.st_size;
165 
166  ZE_LogSysError("fstat error");
167 
168  return 0;
169 }
170 
171 /* ****************************************************************************
172  * *
173  * *
174  ******************************************************************************/
175 int
176 zeReadLn(fd, buf, size)
177  int fd;
178  char *buf;
179  size_t size;
180 {
181  char *p = buf;
182 
183  if (fd < 0)
184  return -1;
185 
186  *p = '\0';
187  while (size > 0) {
188  int n;
189 
190  n = read(fd, p, 1);
191  if (n == 0)
192  break;
193  if (n < 0) {
194  if (errno == EINTR)
195  continue;
196  ZE_LogSysError("read error");
197  break;
198  }
199 
200  if (*p == '\r')
201  continue;
202  if (*p == '\n')
203  break;
204  p++;
205  size--;
206  }
207  *p = '\0';
208  return strlen(buf);
209 }
210 
211 /* ****************************************************************************
212  * *
213  * *
214  ******************************************************************************/
215 bool
216 zeRemoveDir(dirname)
217  char *dirname;
218 {
219  DIR *dir;
220  struct dirent *p;
221  struct stat st;
222  char fname[PATH_MAX];
223  bool r = TRUE;
224 
225  if ((dir = opendir(dirname)) != NULL) {
226  while (r && (p = readdir(dir)) != NULL) {
227  if ((strcmp(p->d_name, ".") == 0) || (strcmp(p->d_name, "..") == 0))
228  continue;
229  snprintf(fname, sizeof (fname), "%s/%s", dirname, p->d_name);
230  ZE_LogMsgInfo(9, "ENTRY : %s", fname);
231  if (stat(fname, &st) == 0) {
232  if (S_ISDIR(st.st_mode))
233  r = zeRemoveDir(fname);
234  else
235  unlink(fname);
236  } else {
237  ZE_LogSysError("lstat(%s) ", fname);
238  r = FALSE;
239  }
240  }
241  closedir(dir);
242  } else {
243  ZE_LogSysError("opendir(%s) :", dirname);
244  r = FALSE;
245  }
246 
247  if (r && rmdir(dirname) != 0) {
248  ZE_LogSysError("rmdir(%s) :", dirname);
249  r = FALSE;
250  }
251 
252  return r;
253 }
254 
255 /* ****************************************************************************
256  * *
257  * *
258  ******************************************************************************/
259 bool
261  char *dir;
262 {
263  int r = 0;
264  struct stat buf;
265 
266  if ((r = stat(dir, &buf)) != 0) {
267  ZE_LogSysError("stat(%s) error", dir);
268  return FALSE;
269  }
270 
271  if (S_ISFIFO(buf.st_mode))
272  ZE_MessageInfo(0, "%s : FIFO", dir);
273 
274  if (S_ISCHR(buf.st_mode))
275  ZE_MessageInfo(0, "%s : CHR", dir);
276 
277  if (S_ISDIR(buf.st_mode))
278  ZE_MessageInfo(0, "%s : DIR", dir);
279 
280  if (S_ISBLK(buf.st_mode))
281  ZE_MessageInfo(0, "%s : BLK", dir);
282 
283 #if 0
284  if (S_ISSOCK(buf.st_mode))
285  ZE_MessageInfo(0, "%s : SOCK", dir);
286 #endif
287 
288  if (S_ISREG(buf.st_mode))
289  ZE_MessageInfo(0, "%s : REG", dir);
290 
291  ZE_MessageInfo(0, " mode : %4o", buf.st_mode);
292  ZE_MessageInfo(0, " uid : %4d", buf.st_uid);
293  ZE_MessageInfo(0, " gid : %4d", buf.st_gid);
294 
295  return TRUE;
296 }
297 
298 /* ****************************************************************************
299  * *
300  * *
301  ******************************************************************************/
302 int
303 zeFdPrintf(int fd, char *format, ...)
304 {
305  va_list arg;
306  char s[4096];
307  int ret = 0;
308 
309  va_start(arg, format);
310  vsnprintf(s, sizeof (s), format, arg);
311  va_end(arg);
312 
313  if ((ret = write(fd, s, strlen(s))) != strlen(s))
314  ZE_LogSysError("error on FD_PRINTF");
315  return ret;
316 }
int zeReadLn(int fd, char *buf, size_t size)
Definition: zeFileTools.c:176
#define strrchr
Definition: ze-sys.h:219
bool zmFileUnlock(int fd)
Definition: zeFileTools.c:114
#define FREE(x)
Definition: macros.h:37
#define ZE_LogMsgInfo(level,...)
Definition: zeSyslog.h:110
#define FALSE
Definition: macros.h:160
#define strlcpy
Definition: zeString.h:32
bool zmFileLock(int fd)
Definition: zeFileTools.c:100
char * zeMyBasename(char *out, char *in, size_t size)
Definition: zeFileTools.c:31
bool zeShowDirInfo(char *dir)
Definition: zeFileTools.c:260
#define strchr
Definition: ze-sys.h:218
#define ZE_MessageInfo(level,...)
Definition: zeSyslog.h:90
#define TRUE
Definition: macros.h:157
#define ZE_LogSysError(...)
Definition: zeSyslog.h:129
int zeFdPrintf(int fd, char *format,...)
Definition: zeFileTools.c:303
bool zeRemoveDir(char *dirname)
Definition: zeFileTools.c:216
char * zeBasename(char *path)
Definition: zeFileTools.c:63
size_t zeGetFileSize(char *fname)
Definition: zeFileTools.c:132
size_t zeGetFdSize(int fd)
Definition: zeFileTools.c:155