HPCToolkit
unlink.c
Go to the documentation of this file.
1 // -*-Mode: C++;-*- // technically C99
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 /*
48  * Function to recursively unlink a directory hierarchy.
49  */
50 
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <dirent.h>
54 #include <string.h>
55 #include <unistd.h>
56 
57 #include "unlink.h"
58 
59 
60 /*
61  * Unlink the tree hierarchy rooted at "path" (helper function).
62  * path_len = strlen(path),
63  * max_len = length of buffer for path.
64  *
65  * Stevens describes this technique of using a single buffer for the
66  * path names in "Advanced Programming in the Unix Environment",
67  * Section 4.21 (Reading Directories).
68  *
69  * Returns: 0 on success, or else the number of unlink() or rmdir()
70  * calls that failed.
71  */
72 static int
73 do_unlink_tree(char *path, int path_len, int max_len)
74 {
75  DIR *dirp;
76  struct dirent *dent;
77  struct stat sb;
78  int len, num_failures;
79 
80  /*
81  * Use unlink(2) for anything that's not a directory.
82  */
83  if (lstat(path, &sb) != 0)
84  return (1);
85  if (! S_ISDIR(sb.st_mode))
86  return (unlink(path) ? 1 : 0);
87 
88  /*
89  * Recursively unlink directory entries, except "." and "..".
90  */
91  dirp = opendir(path);
92  if (dirp == NULL)
93  return (1);
94  num_failures = 0;
95  path[path_len] = '/';
96  while ((dent = readdir(dirp)) != NULL) {
97  if (strncmp(dent->d_name, ".", 2) == 0 ||
98  strncmp(dent->d_name, "..", 3) == 0)
99  continue;
100  len = strlen(dent->d_name);
101  if (path_len + len + 2 >= max_len) {
102  num_failures++;
103  } else {
104  strcpy(&path[path_len + 1], dent->d_name);
105  num_failures += do_unlink_tree(path, path_len + len + 1, max_len);
106  }
107  }
108  path[path_len] = 0;
109  closedir(dirp);
110 
111  /*
112  * Finally, rmdir(2) the directory.
113  */
114  if (rmdir(path) != 0)
115  num_failures++;
116 
117  return (num_failures);
118 }
119 
120 /*
121  * Unlink the file hierarchy rooted at "path".
122  *
123  * Returns: 0 on success, or else the number of unlink() or rmdir()
124  * calls that failed.
125  */
126 #define BUF_LEN 4096
127 int
128 unlink_tree(char *path)
129 {
130  char buf[BUF_LEN];
131  int len;
132 
133  len = strlen(path);
134  if (len >= BUF_LEN)
135  return (1);
136  strcpy(buf, path);
137  return do_unlink_tree(buf, len, BUF_LEN);
138 }
Definition: fmt.c:108
static long num_failures
Definition: mem.c:90
#define NULL
Definition: ElfHelper.cpp:85