Imported Upstream version 4.0.18
[platform/upstream/mtools.git] / unixdir.c
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
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
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 };
78
79 #ifdef HAVE_FCHDIR
80 #define FCHDIR_MODE
81 #endif
82
83 int unix_dir_loop(Stream_t *Stream, MainParam_t *mp); 
84 int unix_loop(Stream_t *Stream, MainParam_t *mp, char *arg, 
85               int follow_dir_link);
86
87 int unix_dir_loop(Stream_t *Stream, MainParam_t *mp)
88 {
89         DeclareThis(Dir_t);
90         struct dirent *entry;
91         char *newName;
92         int ret=0;
93
94 #ifdef FCHDIR_MODE
95         int fd;
96
97         fd = open(".", O_RDONLY);
98         if(chdir(This->pathname) < 0) {
99                 fprintf(stderr, "Could not chdir into %s (%s)\n",
100                         This->pathname, strerror(errno));
101                 return -1;
102         }
103 #endif
104         while((entry=readdir(This->dir)) != NULL) {
105                 if(got_signal)
106                         break;
107                 if(isSpecial(entry->d_name))
108                         continue;
109 #ifndef FCHDIR_MODE
110                 newName = malloc(strlen(This->pathname) + 1 + 
111                                  strlen(entry->d_name) + 1);
112                 if(!newName) {
113                         ret = ERROR_ONE;
114                         break;
115                 }
116                 strcpy(newName, This->pathname);
117                 strcat(newName, "/");
118                 strcat(newName, entry->d_name);
119 #else
120                 newName = entry->d_name;
121 #endif
122                 ret |= unix_loop(Stream, mp, newName, 0);
123 #ifndef FCHDIR_MODE
124                 free(newName);
125 #endif
126         }
127 #ifdef FCHDIR_MODE
128         if(fchdir(fd) < 0)
129                 perror("Could not chdir back to ..");
130         close(fd);
131 #endif
132         return ret;
133 }
134
135 Stream_t *OpenDir(const char *filename)
136 {
137         Dir_t *This;
138
139         This = New(Dir_t);
140         
141         This->Class = &DirClass;
142         This->Next = 0;
143         This->refs = 1;
144         This->Buffer = 0;
145         This->pathname = malloc(strlen(filename)+1);
146         if(This->pathname == NULL) {
147                 Free(This);
148                 return NULL;
149         }
150         strcpy(This->pathname, filename);
151
152         if(MT_STAT(filename, &This->statbuf) < 0) {
153                 Free(This->pathname);
154                 Free(This);
155                 return NULL;
156         }
157
158         This->dir = opendir(filename);
159         if(!This->dir) {
160                 Free(This->pathname);
161                 Free(This);
162                 return NULL;
163         }
164
165         return (Stream_t *) This;
166 }