~ubuntu-branches/ubuntu/wily/attr/wily

« back to all changes in this revision

Viewing changes to .pc/05-eliminate_a_memory_leak.patch/libattr/attr_copy_action.c

  • Committer: Bazaar Package Importer
  • Author(s): Anibal Monsalve Salazar
  • Date: 2010-06-08 13:58:30 UTC
  • Revision ID: james.westby@ubuntu.com-20100608135830-x0956e00lg2bu3g8
Tags: 1:2.4.44-2
* Source format is 3.0 (quilt)
* Add upstream patches 
  02-fix_memory_leak_in_attr_copy_action.patch
  03-pull_in_string.h.patch
  05-eliminate_a_memory_leak.patch
  06-eliminate_a_double_free.patch
  07-fix_thinko_in_restore.patch
* Fix debhelper-but-no-misc-depends
* Fix out-of-date-standards-version

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  Copyright (C) 2006 Andreas Gruenbacher <agruen@suse.de>, SuSE Linux AG.
 
3
 
 
4
  This program is free software; you can redistribute it and/or
 
5
  modify it under the terms of the GNU Lesser General Public
 
6
  License as published by the Free Software Foundation; either
 
7
  version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
  This program is distributed in the hope that it will be useful,
 
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
  Lesser General Public License for more details.
 
13
 
 
14
  You should have received a copy of the GNU Lesser General Public License
 
15
  along with this manual.  If not, see <http://www.gnu.org/licenses/>.
 
16
*/
 
17
 
 
18
#include <alloca.h>
 
19
#include <stdio.h>
 
20
#include <stdlib.h>
 
21
#include <errno.h>
 
22
#include <string.h>
 
23
#include <stdarg.h>
 
24
#include <fnmatch.h>
 
25
 
 
26
#include "attr/libattr.h"
 
27
#define ERROR_CONTEXT_MACROS
 
28
#include "error_context.h"
 
29
 
 
30
#define ATTR_CONF "/etc/xattr.conf"
 
31
 
 
32
struct attr_action {
 
33
        struct attr_action *next;
 
34
        char *pattern;
 
35
        int action;
 
36
};
 
37
 
 
38
static struct attr_action *attr_actions;
 
39
 
 
40
static void
 
41
free_attr_actions(void)
 
42
{
 
43
        struct attr_action *tmp;
 
44
 
 
45
        while (attr_actions) {
 
46
                tmp = attr_actions->next;
 
47
                free(attr_actions->pattern);
 
48
                free(attr_actions);
 
49
                attr_actions = tmp;
 
50
        }
 
51
}
 
52
 
 
53
static int
 
54
attr_parse_attr_conf(struct error_context *ctx)
 
55
{
 
56
        char *text = NULL, *t;
 
57
        size_t size_guess = 4096, len;
 
58
        FILE *file;
 
59
        char *pattern = NULL;
 
60
        struct attr_action *new;
 
61
        int action;
 
62
 
 
63
        if (attr_actions)
 
64
                return 0;
 
65
 
 
66
repeat:
 
67
        if ((file = fopen(ATTR_CONF, "r")) == NULL) {
 
68
                if (errno == ENOENT)
 
69
                        return 0;
 
70
                goto fail;
 
71
        }
 
72
 
 
73
        text = malloc(size_guess + 1);
 
74
        if (!text)
 
75
                goto fail;
 
76
 
 
77
        len = fread(text, 1, size_guess, file);
 
78
        if (ferror(file))
 
79
                goto fail;
 
80
        if (!feof(file)) {
 
81
                fclose(file);
 
82
                file = NULL;
 
83
                free(text);
 
84
                size_guess *= 2;
 
85
                goto repeat;
 
86
        }
 
87
        fclose(file);
 
88
        file = NULL;
 
89
 
 
90
        text[len] = 0;
 
91
        t = text;
 
92
        for (;;) {
 
93
                t += strspn(t, " \t\n");
 
94
                len = strcspn(t, " \t\n#");
 
95
                if (t[len] == '#') {
 
96
                        if (len)
 
97
                                goto parse_error;
 
98
                        t += strcspn(t, "\n");
 
99
                        continue;
 
100
                } else if (t[len] == 0)
 
101
                        break;
 
102
                else if (t[len] == '\n')
 
103
                        goto parse_error;
 
104
                pattern = strndup(t, len);
 
105
                if (!pattern)
 
106
                        goto fail;
 
107
                t += len;
 
108
 
 
109
                t += strspn(t, " \t");
 
110
                len = strcspn(t, " \t\n#");
 
111
                if (len == 4 && !strncmp(t, "skip", 4))
 
112
                        action = ATTR_ACTION_SKIP;
 
113
                else if (len == 11 && !strncmp(t, "permissions", 11))
 
114
                        action = ATTR_ACTION_PERMISSIONS;
 
115
                else
 
116
                        goto parse_error;
 
117
                t += len;
 
118
                t += strspn(t, " \t");
 
119
                if (*t != '#' && *t != '\n')
 
120
                        goto parse_error;
 
121
 
 
122
                new = malloc(sizeof(struct attr_action));
 
123
                if (!new)
 
124
                        goto parse_error;
 
125
                new->next = attr_actions;
 
126
                new->pattern = pattern;
 
127
                new->action = action;
 
128
                attr_actions = new;
 
129
 
 
130
                t += strcspn(t, "\n");
 
131
        }
 
132
        return 0;
 
133
 
 
134
parse_error:
 
135
        errno = EINVAL;
 
136
 
 
137
fail:
 
138
        {
 
139
                const char *q = quote (ctx, ATTR_CONF);
 
140
                error (ctx, "%s", q);
 
141
                quote_free (ctx, q);
 
142
        }
 
143
 
 
144
        free(pattern);
 
145
        if (file)
 
146
                fclose(file);
 
147
        free(text);
 
148
        free_attr_actions();
 
149
        return -1;
 
150
}
 
151
 
 
152
int
 
153
attr_copy_action(const char *name, struct error_context *ctx)
 
154
{
 
155
        struct attr_action *action = attr_actions;
 
156
 
 
157
        if (!attr_parse_attr_conf(ctx)) {
 
158
                for (action = attr_actions; action; action = action->next) {
 
159
                        if (!fnmatch(action->pattern, name, 0))
 
160
                                return action->action;
 
161
                }
 
162
        }
 
163
        return 0;
 
164
}