1 | #include "sysincludes.h"
|
---|
2 | #include "msdos.h"
|
---|
3 | #include "stream.h"
|
---|
4 | #include "mtools.h"
|
---|
5 | #include "fsP.h"
|
---|
6 | #include "file.h"
|
---|
7 | #include "htable.h"
|
---|
8 | #include "mainloop.h"
|
---|
9 | #include <dirent.h>
|
---|
10 |
|
---|
11 | typedef struct Dir_t {
|
---|
12 | Class_t *Class;
|
---|
13 | int refs;
|
---|
14 | Stream_t *Next;
|
---|
15 | Stream_t *Buffer;
|
---|
16 |
|
---|
17 | struct stat stat;
|
---|
18 | char *pathname;
|
---|
19 | DIR *dir;
|
---|
20 | #ifdef HAVE_FCHDIR
|
---|
21 | int fd;
|
---|
22 | #endif
|
---|
23 | } Dir_t;
|
---|
24 |
|
---|
25 | /*#define FCHDIR_MODE*/
|
---|
26 |
|
---|
27 | static int get_dir_data(Stream_t *Stream, time_t *date, mt_size_t *size,
|
---|
28 | int *type, int *address)
|
---|
29 | {
|
---|
30 | DeclareThis(Dir_t);
|
---|
31 |
|
---|
32 | if(date)
|
---|
33 | *date = This->stat.st_mtime;
|
---|
34 | if(size)
|
---|
35 | *size = (mt_size_t) This->stat.st_size;
|
---|
36 | if(type)
|
---|
37 | *type = 1;
|
---|
38 | if(address)
|
---|
39 | *address = 0;
|
---|
40 | return 0;
|
---|
41 | }
|
---|
42 |
|
---|
43 | static int dir_free(Stream_t *Stream)
|
---|
44 | {
|
---|
45 | DeclareThis(Dir_t);
|
---|
46 |
|
---|
47 | Free(This->pathname);
|
---|
48 | closedir(This->dir);
|
---|
49 | return 0;
|
---|
50 | }
|
---|
51 |
|
---|
52 | static Class_t DirClass = {
|
---|
53 | 0, /* read */
|
---|
54 | 0, /* write */
|
---|
55 | 0, /* flush */
|
---|
56 | dir_free, /* free */
|
---|
57 | 0, /* get_geom */
|
---|
58 | get_dir_data ,
|
---|
59 | 0 /* pre-allocate */
|
---|
60 | };
|
---|
61 |
|
---|
62 | #ifdef HAVE_FCHDIR
|
---|
63 | #define FCHDIR_MODE
|
---|
64 | #endif
|
---|
65 |
|
---|
66 | int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
|
---|
67 | int unix_loop(Stream_t *Stream, MainParam_t *mp, char *arg,
|
---|
68 | int follow_dir_link);
|
---|
69 |
|
---|
70 | int unix_dir_loop(Stream_t *Stream, MainParam_t *mp)
|
---|
71 | {
|
---|
72 | DeclareThis(Dir_t);
|
---|
73 | struct dirent *entry;
|
---|
74 | char *newName;
|
---|
75 | int ret=0;
|
---|
76 |
|
---|
77 | #ifdef FCHDIR_MODE
|
---|
78 | int fd;
|
---|
79 |
|
---|
80 | fd = open(".", O_RDONLY);
|
---|
81 | chdir(This->pathname);
|
---|
82 | #endif
|
---|
83 | while((entry=readdir(This->dir)) != NULL) {
|
---|
84 | if(got_signal)
|
---|
85 | break;
|
---|
86 | if(isSpecial(entry->d_name))
|
---|
87 | continue;
|
---|
88 | #ifndef FCHDIR_MODE
|
---|
89 | newName = malloc(strlen(This->pathname) + 1 +
|
---|
90 | strlen(entry->d_name) + 1);
|
---|
91 | if(!newName) {
|
---|
92 | ret = ERROR_ONE;
|
---|
93 | break;
|
---|
94 | }
|
---|
95 | strcpy(newName, This->pathname);
|
---|
96 | strcat(newName, "/");
|
---|
97 | strcat(newName, entry->d_name);
|
---|
98 | #else
|
---|
99 | newName = entry->d_name;
|
---|
100 | #endif
|
---|
101 | ret |= unix_loop(Stream, mp, newName, 0);
|
---|
102 | #ifndef FCHDIR_MODE
|
---|
103 | free(newName);
|
---|
104 | #endif
|
---|
105 | }
|
---|
106 | #ifdef FCHDIR_MODE
|
---|
107 | fchdir(fd);
|
---|
108 | close(fd);
|
---|
109 | #endif
|
---|
110 | return ret;
|
---|
111 | }
|
---|
112 |
|
---|
113 | Stream_t *OpenDir(Stream_t *Stream, const char *filename)
|
---|
114 | {
|
---|
115 | Dir_t *This;
|
---|
116 |
|
---|
117 | This = New(Dir_t);
|
---|
118 |
|
---|
119 | This->Class = &DirClass;
|
---|
120 | This->Next = 0;
|
---|
121 | This->refs = 1;
|
---|
122 | This->Buffer = 0;
|
---|
123 | This->pathname = malloc(strlen(filename)+1);
|
---|
124 | if(This->pathname == NULL) {
|
---|
125 | Free(This);
|
---|
126 | return NULL;
|
---|
127 | }
|
---|
128 | strcpy(This->pathname, filename);
|
---|
129 |
|
---|
130 | if(stat(filename, &This->stat) < 0) {
|
---|
131 | Free(This->pathname);
|
---|
132 | Free(This);
|
---|
133 | return NULL;
|
---|
134 | }
|
---|
135 |
|
---|
136 | This->dir = opendir(filename);
|
---|
137 | if(!This->dir) {
|
---|
138 | Free(This->pathname);
|
---|
139 | Free(This);
|
---|
140 | return NULL;
|
---|
141 | }
|
---|
142 |
|
---|
143 | return (Stream_t *) This;
|
---|
144 | }
|
---|