1 /*  Copyright 1998-2002,2009 Alain Knaff.
2  *  This file is part of mtools.
3  *
4  *  Mtools is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  Mtools 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
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with Mtools.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "sysincludes.h"
19 #include "msdos.h"
20 #include "stream.h"
21 #include "mtools.h"
22 #include "fsP.h"
23 #include "file.h"
24 #include "htable.h"
25 #include "mainloop.h"
26 #include <dirent.h>
27 
28 typedef struct Dir_t {
29 	Class_t *Class;
30 	int refs;
31 	Stream_t *Next;
32 	Stream_t *Buffer;
33 
34 	struct MT_STAT statbuf;
35 	char *pathname;
36 	DIR *dir;
37 #ifdef HAVE_FCHDIR
38 	int fd;
39 #endif
40 } Dir_t;
41 
42 /*#define FCHDIR_MODE*/
43 
get_dir_data(Stream_t * Stream,time_t * date,mt_size_t * size,int * type,int * address)44 static int get_dir_data(Stream_t *Stream, time_t *date, mt_size_t *size,
45 			int *type, int *address)
46 {
47 	DeclareThis(Dir_t);
48 
49 	if(date)
50 		*date = This->statbuf.st_mtime;
51 	if(size)
52 		*size = (mt_size_t) This->statbuf.st_size;
53 	if(type)
54 		*type = 1;
55 	if(address)
56 		*address = 0;
57 	return 0;
58 }
59 
dir_free(Stream_t * Stream)60 static int dir_free(Stream_t *Stream)
61 {
62 	DeclareThis(Dir_t);
63 
64 	Free(This->pathname);
65 	closedir(This->dir);
66 	return 0;
67 }
68 
69 static Class_t DirClass = {
70 	0, /* read */
71 	0, /* write */
72 	0, /* flush */
73 	dir_free, /* free */
74 	0, /* get_geom */
75 	get_dir_data ,
76 	0, /* pre-allocate */
77 	0, /* get_dosConvert */
78 	0 /* discard */
79 };
80 
81 #ifdef HAVE_FCHDIR
82 #define FCHDIR_MODE
83 #endif
84 
85 int unix_dir_loop(Stream_t *Stream, MainParam_t *mp);
86 int unix_loop(Stream_t *Stream, MainParam_t *mp, char *arg,
87 	      int follow_dir_link);
88 
unix_dir_loop(Stream_t * Stream,MainParam_t * mp)89 int unix_dir_loop(Stream_t *Stream, MainParam_t *mp)
90 {
91 	DeclareThis(Dir_t);
92 	struct dirent *entry;
93 	char *newName;
94 	int ret=0;
95 
96 #ifdef FCHDIR_MODE
97 	int fd;
98 
99 	fd = open(".", O_RDONLY);
100 	if(chdir(This->pathname) < 0) {
101 		fprintf(stderr, "Could not chdir into %s (%s)\n",
102 			This->pathname, strerror(errno));
103 		return -1;
104 	}
105 #endif
106 	while((entry=readdir(This->dir)) != NULL) {
107 		if(got_signal)
108 			break;
109 		if(isSpecial(entry->d_name))
110 			continue;
111 #ifndef FCHDIR_MODE
112 		newName = malloc(strlen(This->pathname) + 1 +
113 				 strlen(entry->d_name) + 1);
114 		if(!newName) {
115 			ret = ERROR_ONE;
116 			break;
117 		}
118 		strcpy(newName, This->pathname);
119 		strcat(newName, "/");
120 		strcat(newName, entry->d_name);
121 #else
122 		newName = entry->d_name;
123 #endif
124 		ret |= unix_loop(Stream, mp, newName, 0);
125 #ifndef FCHDIR_MODE
126 		free(newName);
127 #endif
128 	}
129 #ifdef FCHDIR_MODE
130 	if(fchdir(fd) < 0)
131 		perror("Could not chdir back to ..");
132 	close(fd);
133 #endif
134 	return ret;
135 }
136 
OpenDir(const char * filename)137 Stream_t *OpenDir(const char *filename)
138 {
139 	Dir_t *This;
140 
141 	This = New(Dir_t);
142 
143 	This->Class = &DirClass;
144 	This->Next = 0;
145 	This->refs = 1;
146 	This->Buffer = 0;
147 	This->pathname = malloc(strlen(filename)+1);
148 	if(This->pathname == NULL) {
149 		Free(This);
150 		return NULL;
151 	}
152 	strcpy(This->pathname, filename);
153 
154 	if(MT_STAT(filename, &This->statbuf) < 0) {
155 		Free(This->pathname);
156 		Free(This);
157 		return NULL;
158 	}
159 
160 	This->dir = opendir(filename);
161 	if(!This->dir) {
162 		Free(This->pathname);
163 		Free(This);
164 		return NULL;
165 	}
166 
167 	return (Stream_t *) This;
168 }
169