File: elcwft.c

package info (click to toggle)
integrit 4.1-7
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie, trixie-proposed-updates
  • size: 1,688 kB
  • sloc: ansic: 4,227; makefile: 384; sh: 340; perl: 224
file content (193 lines) | stat: -rw-r--r-- 4,855 bytes parent folder | download | duplicates (5)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
integrit - file integrity verification system
Copyright (C) 2006 Ed L. Cashin

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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
/*
 * Looks like struct dirent's on my Linux box always have DT_UNKNOWN
 * as their d_type member:
 * 
 * if (result)
 *    printf("type: %d %s DT_UNKNOWN\t",
 *           result->d_type, (result->d_type == DT_UNKNOWN) ? "==" : "!=");
 *
 * ... as the docs say they might.
 */

/* This file tree walker silently ignores files that vanish between the
 * time the readdir is done and the time the lstat is done.
 */
#include	<config.h>
#include	<stdio.h>
#include	<stdlib.h>
#include	<unistd.h>
#include	<string.h>
#include	<sys/stat.h>
#include	<sys/types.h>
#include	<dirent.h>
#include	<errno.h>
#include	"xstrdup.h"
#include	"xstradd.h"
#include	"elcwft.h"
#include	"elcwft_p.h"
#include	"elcerror.h"
#include	"elcerror_p.h"
#ifdef		ELC_FIND_LEAKS
#include	"leakfind.h"
#endif

typedef struct wftnode {		/* walk file tree node */
  DIR			*dir;
  struct wftnode	*next;
} wftnode;

inline static int is_directory(const char *path, struct stat *sb)
{
    if (lstat(path, sb) == -1)
      return -1;
    return S_ISDIR(sb->st_mode);
}

inline static wftnode *endoflist(wftnode *head)
{
    wftnode	*np;
    
    for (np = head; np->next; np = np->next)
      if (np->next && (! np->next->dir)) {
	/* clean up used node */
	free(np->next);
	np->next	 = NULL;
	break;
      }
    return np;
}

inline static int backupdir(char *dirname)
{
    char	*lastslash	 = strrchr(dirname, '/');
    if (! lastslash) {
      errno	 = EINVAL;
      return -1;
    }
    *lastslash	 = '\0';

    return 0;
}

/* the function below is tied to the internal workings of
 * walk_file_tree, and it helps make the big loop in walk_file_tree
 * more readable/managable. */
inline static int finishdir(wftnode *np, wftnode *head,
			    int *done, char *curr_dirname)
{
    if (np == head) {
      *done	 = 1;
      closedir(np->dir);
      return 0;
    }

    closedir(np->dir);
    np->dir	 = NULL; /* signal to endoflist that we're done here */
    if (backupdir(curr_dirname) == -1)
      return -1;

    return 0;
}

/* the old parameters were:
 * const char *rootname, wft_cb_t callback, void *cbdata */
int walk_file_tree(wft_context_t *ctx)
{
    DIR			*root	 = opendir(ctx->rootname);
    wftnode		dirs;
    wftnode		*np;
    char		*curr_dirname	 = xstrdup(ctx->rootname);
    char		*path		 = NULL;
    struct dirent	*result;
    struct stat		statbuf;
    int			ret, cb_ret;
    int			done	 = 0;
    int			err	 = 0;

    dirs.dir	 = root;
    dirs.next	 = NULL;

    if (! root)
      return WFT_ERROR;

    while (! done) {
      /* find the deepest directory and maintain list */
      np	 = endoflist(&dirs);

      /* while we don't want to go deeper into file tree */
      while (! np->next) {
	result	 = readdir(np->dir);
	if (!result) {
	  /* no more entries in this dir */
	  if (finishdir(np, &dirs, &done, curr_dirname) == -1)
	    return WFT_ERROR;
	  break;
	}
	if (!strcmp(result->d_name, ".") || !strcmp(result->d_name, "..")) {
	  /* skip the current and parent dir */
	  continue;
	}

	if (path)
	  free(path);
	path	= xstradd(curr_dirname, "/", result->d_name, NULL);
	if ((ret = is_directory(path, &statbuf)) == -1) {
	  if (errno == ENOENT)	/* no such file or directory */
	    continue;		/* silently skip the vanished file */
	  else
	    return WFT_ERROR;
	}
	cb_ret	= ctx->callback(path, &statbuf, ctx->cb_data);
	switch (cb_ret) {
	case WFT_ERROR:
	  if (ctx->options & WFT_STOP_ON_ERR)
	    return WFT_ERROR;
          err = 1;

	case WFT_PRUNE:
	  continue;
	}

	if (ret == 1) { /* directory */
	  if (! (np->next = malloc(sizeof(* np->next))) )
	    return WFT_ERROR;
	  if (! (np->next->dir = opendir(path)) ) {
	    warn(__FUNCTION__, "Warning: cannot open directory (%s): %s",
		 path, strerror(errno));
	    if (ctx->options & WFT_STOP_ON_ERR)
	      return WFT_ERROR;
	    err = 1;
	    continue;
	  }
	  free(curr_dirname);
	  curr_dirname		 = xstrdup(path);
	  np->next->next	 = NULL;
	}
      } /* end iterating through a given dir */
    } /* end while not done */

    if (path)
      free(path);
    free(curr_dirname);
    return (err? WFT_SOME_ERR: WFT_PROCEED);
}