22 #include "fileutils.h"
30 #define MAIN_ERR_PREFIX "FUTIL: "
53 static int fu_compar(
const struct_posix_dirent** a,
54 const struct_posix_dirent** b)
78 for(i = 0; i < strlen(path); ++i)
81 if(
'a' <= path[i] &&
'z' >= path[i])
continue;
82 if(
'A' <= path[i] &&
'Z' >= path[i])
continue;
84 if(
'0' <= path[i] &&
'9' >= path[i])
continue;
86 if(
'.' == path[i])
continue;
87 if(
'_' == path[i])
continue;
88 if(
'-' == path[i])
continue;
90 if(
'/' == path[i])
continue;
93 PRINT_ERROR(
"Path or pathname contains forbidden characters");
128 if(NULL == path ||
'/' != path[0])
134 while (0 != path[pos])
137 p = strchr(&path[pos], (
int)
'/');
140 len = (size_t) (p - &path[pos]);
141 pos += len + (size_t) 1;
146 len = (size_t) (&path[strlen(path)] - &path[pos]);
156 pcomp = (
char*) posix_malloc(pos + (
size_t) 1);
159 PRINT_ERROR(
"Can't allocate memory for path component");
162 memcpy((
void*) pcomp, (
void*) path, pos);
167 if(
'/' == pcomp[pos - (
size_t) 1])
169 pcomp[pos - (size_t) 1] = 0;
174 rv = posix_mkdir(pcomp, perm);
175 if(!(0 == rv || (-1 == rv && POSIX_EEXIST == posix_errno)))
180 posix_free((
void*) pcomp);
214 struct_posix_stat stat_dummy;
216 if(NULL == state) { res = posix_stat(pathname, &stat_dummy); }
217 else { res = posix_stat(pathname, state); }
250 do { *filedesc = posix_open(pathname, mode, perm); }
251 while(-1 == *filedesc && POSIX_EINTR == posix_errno);
260 do { rv = posix_fcntl(*filedesc, POSIX_F_SETFD, POSIX_FD_CLOEXEC); }
261 while(-1 == rv && POSIX_EINTR == posix_errno);
265 posix_close(*filedesc);
295 if (NULL != *stream) { fclose(*stream); }
300 if (NULL == stream && NULL != filedesc)
302 if (-1 != *filedesc) { posix_close(*filedesc); }
304 if (NULL != filedesc) { *filedesc = -1; }
331 struct_posix_flock fl;
333 fl.l_type = POSIX_F_WRLCK;
334 fl.l_whence = POSIX_SEEK_SET;
337 res = posix_fcntl(filedesc, POSIX_F_SETLK, &fl);
357 return(posix_unlink(pathname));
377 *stream = posix_fdopen(filedesc, mode);
409 do { res = fflush(stream); }
410 while(EOF == res && POSIX_EINTR == posix_errno);
416 do { res = posix_fsync(filedesc); }
417 while(-1 == res && POSIX_EINTR == posix_errno);
456 if(i + (
size_t) 1 >= *len)
459 if(!*len) { *len = 256; }
460 p = posix_realloc((
void*) *buffer, *len *= (
size_t) 2);
461 if(NULL == p) {
break; }
462 else { *buffer = p; }
465 rv = posix_read(filedesc, &(*buffer)[i], *len - i - (
size_t) 1);
466 if(-1 == rv && POSIX_EINTR == posix_errno) {
continue; }
467 if(-1 == rv) {
break; }
468 else { i += (size_t) rv; }
470 if(!rv) { (*buffer)[i] = 0; res = 0;
break; }
476 posix_free((
void*) *buffer);
507 posix_ssize_t rv = -1;
512 rv = posix_read(filedesc, (
void*) &buffer[i], *len - i);
513 if((posix_ssize_t) -1 == rv && POSIX_EINTR == posix_errno) {
continue; }
515 if((posix_ssize_t) -1 != rv) { i += (size_t) rv; }
else {
break; }
517 if(-1 == rv) {
PRINT_ERROR(
"Error while reading from FD"); }
550 rv = posix_write(filedesc, (
void*) &buffer[i], len - i);
551 if((posix_ssize_t) -1 == rv && POSIX_EINTR == posix_errno) {
continue; }
552 if((posix_ssize_t) -1 != rv) { i += (size_t) rv; }
else {
break; }
554 if(len == i) { res = 0; }
555 if(res) {
PRINT_ERROR(
"Writing data block to FD failed"); }
585 struct_posix_dirent** content;
586 struct_posix_stat state;
595 if(len &&
'/' == dir[0])
598 path = (
char*) posix_malloc(len + (
size_t) 1);
606 if(
'/' == path[len - (
size_t) 1]) { path[len - (size_t) 1] = 0; }
608 num = posix_scandir(path, &content, NULL, fu_compar);
609 if(0 > num) {
PRINT_ERROR(
"Cannot scan directory"); }
614 for(i = 0; i < (size_t) num; ++i)
616 entry = content[i]->d_name;
621 if(!strcmp(
".", entry)) {
continue; }
622 if(!strcmp(
"..", entry)) {
continue; }
623 pathname = (
char*) posix_malloc(strlen(path) + strlen(entry)
631 strcpy(pathname, path);
632 strcat(pathname,
"/");
633 strcat(pathname, entry);
635 #if CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI
637 rv = posix_lstat(pathname, &state);
640 rv = posix_stat(pathname, &state);
650 if(POSIX_S_ISDIR(state.st_mode))
664 posix_free((
void*) pathname);
667 rv = posix_rmdir(path);
670 while(num--) { posix_free((
void*) content[num]); }
671 posix_free((
void*) content);
673 posix_free((
void*) path);