/* * tools/lib/lvm_rm_rf.c * * Copyright (C) 2000 Klaus Strebel, Germany * * Janury 2000 * * * This LVM library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This LVM library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this LVM library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA * */ #include #include int lvm_rm_rf (char *dirname) { DIR *dir_pointer; struct dirent *dp; int my_err=0; char *indirname_buffer; /* Without buffering of parm, the recursion segfaults at the 3. level! */ char *dirname_buffer; if (dirname != NULL && strlen(dirname) > 0) { indirname_buffer = malloc(strlen(dirname)+2); sprintf(indirname_buffer, "%s%c", dirname, 0); if (unlink(indirname_buffer) == 0) { free(indirname_buffer); return 0; } if (errno != EISDIR) { my_err = errno; fprintf(stderr, "lvm_rm_rf: ERROR unlinking <%s>:%ld\n", indirname_buffer,my_err); free(indirname_buffer); return my_err; } dir_pointer = opendir(indirname_buffer); for (dp = readdir(dir_pointer); dp != NULL; dp = readdir(dir_pointer)) { dirname_buffer = malloc(strlen(indirname_buffer)+strlen(dp->d_name)+2); sprintf(dirname_buffer, "%s/%s%c", indirname_buffer, dp->d_name, 0); if ((strcmp(dp->d_name, ".") != 0) && (strcmp(dp->d_name, "..") != 0)) { if (unlink(dirname_buffer) != 0) { if (errno == EISDIR) { my_err=lvm_rm_rf (dirname_buffer); } else { my_err=errno; fprintf(stderr, "lvm_rm_rf: ERROR unlinking <%s>:%ld\n", dirname_buffer,my_err); } } } free(dirname_buffer); } closedir(dir_pointer); if (my_err == 0) my_err = rmdir(indirname_buffer); free(indirname_buffer); return my_err; } return 0; }