-
Notifications
You must be signed in to change notification settings - Fork 1
/
tree.c
124 lines (104 loc) · 3.33 KB
/
tree.c
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* -*- c-set-style: "K&R"; c-basic-offset: 8 -*-
*
* This file is part of PRoot.
*
* Copyright (C) 2014 STMicroelectronics
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This program 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA.
*/
#include <stdio.h> /* fprintf(3), */
#include <errno.h> /* EBUSY, */
#include <dirent.h> /* DT_*, */
#include <talloc.h>
#include <uthash.h>
#include "vfs/tree.h"
#include "vfs/node.h"
/**
* Print in @file a human readable format of @root, then perform
* recursively the same for @root node's children.
*/
void print_tree_(const Node *root, FILE *file, size_t indentation)
{
Node *child;
size_t i;
for (i = 0; i < indentation; i++)
fprintf(file, " ");
fprintf(file, "%s [type: ", root->name);
switch (root->type) {
case DT_REG: fprintf(file, "regular"); break;
case DT_DIR: fprintf(file, "directory"); break;
case DT_LNK: fprintf(file, "symlink"); break;
case DT_BLK: fprintf(file, "block dev."); break;
case DT_CHR: fprintf(file, "char. dev."); break;
case DT_FIFO: fprintf(file, "fifo"); break;
case DT_SOCK: fprintf(file, "socket"); break;
default: fprintf(file, "unknown (%x)", root->type); break;
}
fprintf(file, "; actual path: %s", root->path_.actual);
fprintf(file, "; virtual path: %s", root->path_.virtual);
fprintf(file, "; evaluator: %p", root->evaluator);
fprintf(file, "; special: %d", root->special);
fprintf(file, "]\n");
/* No child deletion, so no need for HASH_ITER. */
for (child = root->children; child != NULL; child = child->hh.next)
print_tree_(child, file, indentation + 2);
}
/**
* Delete recursively @parent's children. This function returns
* -EBUSY if a children is referenced elsewhere, otherwise the number
* of deleted nodes.
*/
static ssize_t delete_children(Node *parent)
{
size_t nb_deleted_nodes = 0;
ssize_t status;
Node *child;
Node *tmp;
HASH_ITER(hh, parent->children, child, tmp) {
size_t reference_count;
status = delete_children(child);
if (status < 0)
return status;
nb_deleted_nodes += status;
reference_count = talloc_reference_count(child);
if (reference_count > 1)
return -EBUSY;
HASH_DEL(parent->children, child);
TALLOC_FREE(child);
nb_deleted_nodes++;
}
return nb_deleted_nodes;
}
/**
* Delete recursively @root. This function returns -EBUSY if @root
* node or one of its children is referenced elsewhere, otherwise the
* number of deleted nodes. */
ssize_t delete_tree(Node *root)
{
size_t nb_deleted_nodes = 0;
size_t reference_count;
ssize_t status;
status = delete_children(root);
if (status < 0)
return status;
nb_deleted_nodes += status;
reference_count = talloc_reference_count(root);
if (reference_count > 1)
return -EBUSY;
TALLOC_FREE(root);
nb_deleted_nodes++;
return nb_deleted_nodes;
}