]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - repair/dir_stack.c
fixup warnings with -Wno-parentheses removed.
[thirdparty/xfsprogs-dev.git] / repair / dir_stack.c
1 /*
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11 *
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <libxfs.h>
34 #include "dir_stack.h"
35 #include "err_protos.h"
36
37 /*
38 * a directory stack for holding directories while
39 * we traverse filesystem hierarchy subtrees.
40 * names are kind of misleading as this is really
41 * implemented as an inode stack. so sue me...
42 */
43
44 static dir_stack_t dirstack_freelist;
45 static int dirstack_init = 0;
46
47 void
48 dir_stack_init(dir_stack_t *stack)
49 {
50 stack->cnt = 0;
51 stack->head = NULL;
52
53 if (dirstack_init == 0) {
54 dirstack_init = 1;
55 dir_stack_init(&dirstack_freelist);
56 }
57
58 stack->cnt = 0;
59 stack->head = NULL;
60
61 return;
62 }
63
64 static void
65 dir_stack_push(dir_stack_t *stack, dir_stack_elem_t *elem)
66 {
67 ASSERT(stack->cnt > 0 || (stack->cnt == 0 && stack->head == NULL));
68
69 elem->next = stack->head;
70 stack->head = elem;
71 stack->cnt++;
72
73 return;
74 }
75
76 static dir_stack_elem_t *
77 dir_stack_pop(dir_stack_t *stack)
78 {
79 dir_stack_elem_t *elem;
80
81 if (stack->cnt == 0) {
82 ASSERT(stack->head == NULL);
83 return(NULL);
84 }
85
86 elem = stack->head;
87
88 ASSERT(elem != NULL);
89
90 stack->head = elem->next;
91 elem->next = NULL;
92 stack->cnt--;
93
94 return(elem);
95 }
96
97 void
98 push_dir(dir_stack_t *stack, xfs_ino_t ino)
99 {
100 dir_stack_elem_t *elem;
101
102 if (dirstack_freelist.cnt == 0) {
103 if ((elem = malloc(sizeof(dir_stack_elem_t))) == NULL) {
104 do_error(
105 "couldn't malloc dir stack element, try more swap\n");
106 exit(1);
107 }
108 } else {
109 elem = dir_stack_pop(&dirstack_freelist);
110 }
111
112 elem->ino = ino;
113
114 dir_stack_push(stack, elem);
115
116 return;
117 }
118
119 xfs_ino_t
120 pop_dir(dir_stack_t *stack)
121 {
122 dir_stack_elem_t *elem;
123 xfs_ino_t ino;
124
125 elem = dir_stack_pop(stack);
126
127 if (elem == NULL)
128 return(NULLFSINO);
129
130 ino = elem->ino;
131 elem->ino = NULLFSINO;
132
133 dir_stack_push(&dirstack_freelist, elem);
134
135 return(ino);
136 }