~ubuntu-branches/ubuntu/raring/reprepro/raring

« back to all changes in this revision

Viewing changes to dirs.c

  • Committer: Bazaar Package Importer
  • Author(s): Bernhard R. Link
  • Date: 2011-05-05 16:34:23 UTC
  • mfrom: (21.1.5 sid)
  • Revision ID: james.westby@ubuntu.com-20110505163423-x49kbdijyoubai4x
Tags: 4.6.0-1
* new release
- general cleanup
- new FilterSrcList
* increase Standards-Version, no changes needed

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
retvalue dirs_create(const char *dirname) {
34
34
        int ret, e;
35
35
 
36
 
        ret = mkdir(dirname,0775);
37
 
        if( ret == 0 ) {
38
 
                if( verbose > 1)
39
 
                        printf("Created directory \"%s\"\n",dirname);
 
36
        ret = mkdir(dirname, 0775);
 
37
        if (ret == 0) {
 
38
                if (verbose > 1)
 
39
                        printf("Created directory \"%s\"\n", dirname);
40
40
                return RET_OK;
41
 
        } else if( ret < 0 && ( e = errno ) != EEXIST ) {
 
41
        } else if (ret < 0 && (e = errno) != EEXIST) {
42
42
                fprintf(stderr, "Error %d creating directory \"%s\": %s\n",
43
43
                                e, dirname, strerror(e));
44
44
                return RET_ERROR;
53
53
        int i;
54
54
        retvalue r;
55
55
 
56
 
        for( p = filename+1, i = 1 ; *p != '\0' ; p++,i++) {
57
 
                if( *p == '/' ) {
58
 
                        h = strndup(filename,i);
 
56
        for (p = filename+1, i = 1 ; *p != '\0' ; p++, i++) {
 
57
                if (*p == '/') {
 
58
                        h = strndup(filename, i);
 
59
                        if (FAILEDTOALLOC(h))
 
60
                                return RET_ERROR_OOM;
59
61
                        r = dirs_create(h);
60
 
                        if( RET_WAS_ERROR(r) ) {
 
62
                        if (RET_WAS_ERROR(r)) {
61
63
                                free(h);
62
64
                                return r;
63
65
                        }
69
71
 
70
72
/* create dirname and any '/'-separated part of it */
71
73
retvalue dirs_make_recursive(const char *directory) {
72
 
        retvalue r,result;
 
74
        retvalue r, result;
73
75
 
74
 
        if( interrupted() ) {
 
76
        if (interrupted()) {
75
77
                return RET_ERROR_INTERRUPTED;
76
78
        }
77
79
        r = dirs_make_parent(directory);
78
80
        result = dirs_create(directory);
79
 
        RET_UPDATE(result,r);
 
81
        RET_UPDATE(result, r);
80
82
        return result;
81
83
}
82
84
 
89
91
        char *this;
90
92
        int e;
91
93
 
92
 
        if( interrupted() ) {
 
94
        if (interrupted()) {
93
95
                return RET_ERROR_INTERRUPTED;
94
96
        }
95
 
        while( len > 0 && directory[len-1] == '/' )
 
97
        while (len > 0 && directory[len-1] == '/')
96
98
                len--;
97
 
        while( len > 0 ) {
 
99
        while (len > 0) {
98
100
                this = strndup(directory, len);
99
 
                if( this == NULL )
 
101
                if (FAILEDTOALLOC(this))
100
102
                        return RET_ERROR_OOM;
101
103
                ret = mkdir(this, 0777);
102
104
                e = errno;
103
 
                if( ret == 0 ) {
104
 
                        if( verbose > 1)
 
105
                if (ret == 0) {
 
106
                        if (verbose > 1)
105
107
                                printf("Created directory \"%s\"\n", this);
106
 
                } else if( e == EEXIST ) {
 
108
                } else if (e == EEXIST) {
107
109
                        free(this);
108
110
                        break;
109
 
                /* normaly ENOENT should be the only problem, but check the others
110
 
                 * to be nice to annoying filesystems */
111
 
                } else if( e != ENOENT && e != EACCES && e != EPERM ) {
112
 
                        fprintf(stderr, "Cannot create directory \"%s\": %s(%d)\n",
 
111
                /* normaly ENOENT should be the only problem,
 
112
                 * but check the others to be nice to annoying filesystems */
 
113
                } else if (e != ENOENT && e != EACCES && e != EPERM) {
 
114
                        fprintf(stderr,
 
115
"Cannot create directory \"%s\": %s(%d)\n",
113
116
                                        this, strerror(e), e);
114
117
                        free(this);
115
118
                        return RET_ERRNO(e);
116
119
                }
117
120
                free(this);
118
121
                depth++;
119
 
                while( len > 0 && directory[len-1] != '/' )
 
122
                while (len > 0 && directory[len-1] != '/')
120
123
                        len--;
121
 
                while( len > 0 && directory[len-1] == '/' )
 
124
                while (len > 0 && directory[len-1] == '/')
122
125
                        len--;
123
126
        }
124
127
        check = depth;
125
 
        while( directory[len] == '/' )
 
128
        while (directory[len] == '/')
126
129
                len++;
127
 
        while( directory[len] != '\0' ) {
128
 
                while( directory[len] != '\0' && directory[len] != '/' )
 
130
        while (directory[len] != '\0') {
 
131
                while (directory[len] != '\0' && directory[len] != '/')
129
132
                        len++;
130
133
                this = strndup(directory, len);
131
 
                if( this == NULL )
 
134
                if (FAILEDTOALLOC(this))
132
135
                        return RET_ERROR_OOM;
133
136
                r = dirs_create(this);
134
137
                free(this);
135
 
                if( RET_WAS_ERROR(r) )
 
138
                if (RET_WAS_ERROR(r))
136
139
                        return r;
137
140
                // TODO: if we get RET_NOTHING here, reduce depth?
138
141
                check--;
139
 
                while( directory[len] == '/' )
 
142
                while (directory[len] == '/')
140
143
                        len++;
141
144
        }
142
145
        assert(check == 0);
149
152
        char *this;
150
153
        int ret;
151
154
 
152
 
        while( len > 0 && directory[len-1] == '/' )
 
155
        while (len > 0 && directory[len-1] == '/')
153
156
                len--;
154
 
        while( created > 0 && len > 0 ) {
 
157
        while (created > 0 && len > 0) {
155
158
                this = strndup(directory, len);
156
 
                if( this == NULL )
 
159
                if (FAILEDTOALLOC(this))
157
160
                        return;
158
161
                ret = rmdir(this);
159
 
                if( ret == 0 ) {
160
 
                        if( verbose > 1)
161
 
                                printf("Removed empty directory \"%s\"\n", this);
 
162
                if (ret == 0) {
 
163
                        if (verbose > 1)
 
164
                                printf(
 
165
"Removed empty directory \"%s\"\n",
 
166
                                        this);
162
167
                } else {
163
168
                        int e = errno;
164
 
                        if( e != ENOTEMPTY ) {
165
 
                                fprintf(stderr, "Error removing directory \"%s\": %s(%d)\n",
 
169
                        if (e != ENOTEMPTY) {
 
170
                                fprintf(stderr,
 
171
"Error removing directory \"%s\": %s(%d)\n",
166
172
                                                this, strerror(e), e);
167
173
                        }
168
174
                        free(this);
170
176
                }
171
177
                free(this);
172
178
                created--;
173
 
                while( len > 0 && directory[len-1] != '/' )
 
179
                while (len > 0 && directory[len-1] != '/')
174
180
                        len--;
175
 
                while( len > 0 && directory[len-1] == '/' )
 
181
                while (len > 0 && directory[len-1] == '/')
176
182
                        len--;
177
183
        }
178
184
        return;
179
185
}
180
186
 
181
 
retvalue dirs_getdirectory(const char *filename,char **directory) {
 
187
retvalue dirs_getdirectory(const char *filename, char **directory) {
182
188
        size_t len;
183
189
 
184
 
        assert( filename != NULL && *filename != '\0' );
 
190
        assert (filename != NULL && *filename != '\0');
185
191
 
186
192
        len = strlen(filename);
187
 
        while( len > 1 && filename[len-1] == '/' ) {
188
 
                len--;
189
 
        }
190
 
        while( len > 0 && filename[len-1] != '/' ) {
191
 
                len--;
192
 
        }
193
 
        if( len == 0 ) {
 
193
        while (len > 1 && filename[len-1] == '/') {
 
194
                len--;
 
195
        }
 
196
        while (len > 0 && filename[len-1] != '/') {
 
197
                len--;
 
198
        }
 
199
        if (len == 0) {
194
200
                *directory = strdup(".");
195
201
        } else {
196
 
                if( len == 1 )
 
202
                if (len == 1)
197
203
                        *directory = strdup("/");
198
204
                else
199
 
                        *directory = strndup(filename,len-1);
 
205
                        *directory = strndup(filename, len-1);
200
206
        }
201
 
        if( *directory == NULL )
 
207
        if (FAILEDTOALLOC(*directory))
202
208
                return RET_ERROR_OOM;
203
209
        else
204
210
                return RET_OK;
207
213
const char *dirs_basename(const char *filename) {
208
214
        const char *bn;
209
215
 
210
 
        bn = strrchr(filename,'/');
211
 
        if( bn == NULL )
 
216
        bn = strrchr(filename, '/');
 
217
        if (bn == NULL)
212
218
                return filename;
213
219
        // not really suited for the basename of directories,
214
220
        // things like /bla/blub/ will give emtpy string...