-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdelete.cpp
61 lines (50 loc) · 1.57 KB
/
delete.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/********************************************************************************/
/* Name: Priyendu Mori */
/* Roll no: 2018201103 */
/********************************************************************************/
#include "header.h"
#include "macro.h"
extern ofstream myfile;
/*
this is a helper function that deletes
files and directories recursively
*/
void deleteHelper(string path){
string fullpath;
DIR *dir;
struct stat stat_dir, stat_entry;
struct dirent *entry;
stat(path.c_str(), &stat_dir);
if ((dir = opendir(path.c_str())) == NULL) {
commands(true);
}
while ((entry = readdir(dir)) != NULL) {
if (!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, ".."))
continue;
fullpath=path;
fullpath.append("/").append(entry->d_name);
stat(fullpath.c_str(), &stat_entry);
if (S_ISDIR(stat_entry.st_mode) != 0) {
deleteHelper(fullpath);
continue;
}
if (unlink(fullpath.c_str()) != 0) commands(true);
}
if (rmdir(path.c_str()) != 0) commands(true);
closedir(dir);
}
/*
this function deletes directory recursively
using deleteHelper
*/
void deleteDirectory(string path){
string fullpath=getWholePath(path);
deleteHelper(fullpath);
}
/*
this function deletes a file
*/
void deleteFile(string filename){
string fullpath=getWholePath(filename);
if (unlink(fullpath.c_str()) != 0) commands(true);
}