Mercurial > dovecot > core-2.2
view src/lib/unlink-directory.c @ 805:5ac361acb316 HEAD
Marked all non-trivial buffer modifications with @UNSAFE tag. Several
cleanups and a couple of minor bugfixes.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Wed, 18 Dec 2002 17:15:41 +0200 |
parents | 3b1985cbc908 |
children | 35abd7a5d381 |
line wrap: on
line source
/* unlink-directory.c : Unlink directory with everything under it. Copyright (c) 2002 Timo Sirainen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "lib.h" #include "unlink-directory.h" #include <unistd.h> #include <dirent.h> #include <sys/stat.h> int unlink_directory(const char *dir) { DIR *dirp; struct dirent *d; struct stat st; char path[1024]; dirp = opendir(dir); if (dirp == NULL) return FALSE; while ((d = readdir(dirp)) != NULL) { if (d->d_name[0] == '.' && (d->d_name[1] == '\0' || (d->d_name[1] == '.' && d->d_name[2] == '\0'))) { /* skip . and .. */ continue; } i_snprintf(path, sizeof(path), "%s/%s", dir, d->d_name); if (unlink(path) == -1 && errno != ENOENT) { int old_errno = errno; if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) { if (!unlink_directory(path)) return FALSE; } else { /* so it wasn't a directory, unlink() again to get correct errno */ errno = old_errno; return FALSE; } } } (void)closedir(dirp); if (rmdir(dir) == -1) return FALSE; return TRUE; }