Browse code

add dircopy()

git-svn: trunk@2130

Tomasz Kojm authored on 2006/07/27 21:19:34
Showing 3 changed files
... ...
@@ -1,3 +1,7 @@
1
+Thu Jul 27 14:18:57 CEST 2006 (tk)
2
+----------------------------------
3
+  * shared/misc.c: add dircopy()
4
+
1 5
 Wed Jul 26 17:00:59 CEST 2006 (tk)
2 6
 ----------------------------------
3 7
   * libclamav/readdb.c: add support for incremental directories
... ...
@@ -132,7 +132,9 @@ int filecopy(const char *src, const char *dest)
132 132
 
133 133
 #else
134 134
 	char buffer[FILEBUFF];
135
-	int s, d, bytes;
135
+	int s, d, bytes, ret;
136
+	struct stat sb;
137
+
136 138
 
137 139
     if((s = open(src, O_RDONLY)) == -1)
138 140
 	return -1;
... ...
@@ -146,9 +148,13 @@ int filecopy(const char *src, const char *dest)
146 146
 	write(d, buffer, bytes);
147 147
 
148 148
     close(s);
149
-
150 149
     /* njh@bandsman.co.uk: check result of close for NFS file */
151
-    return close(d);
150
+    ret = close(d);
151
+
152
+    stat(src, &sb);
153
+    chmod(dest, sb.st_mode);
154
+
155
+    return ret;
152 156
 
153 157
 #endif
154 158
 
... ...
@@ -210,6 +216,50 @@ int rmdirs(const char *dirname)
210 210
     return 0;
211 211
 }
212 212
 
213
+int dircopy(const char *src, const char *dest)
214
+{
215
+	DIR *dd;
216
+	struct dirent *dent;
217
+	struct stat sb;
218
+	char spath[512], dpath[512];
219
+
220
+
221
+    if(stat(dest, &sb) == -1) {
222
+	if(mkdir(dest, 0700)) {
223
+	    mprintf("!dircopy: Can't create temporary directory %s\n", dest);
224
+	    return -1;
225
+	}
226
+    }
227
+
228
+    if((dd = opendir(src)) == NULL) {
229
+        mprintf("!dircopy: Can't open directory %s\n", src);
230
+        return -1;
231
+    }
232
+
233
+    while((dent = readdir(dd))) {
234
+#ifndef C_INTERIX
235
+	if(dent->d_ino)
236
+#endif
237
+	{
238
+	    if(!strcmp(dent->d_name, ".") || !strcmp(dent->d_name, ".."))
239
+		continue;
240
+
241
+	    snprintf(spath, sizeof(spath), "%s/%s", src, dent->d_name);
242
+	    snprintf(dpath, sizeof(dpath), "%s/%s", dest, dent->d_name);
243
+
244
+	    if(filecopy(spath, dpath) == -1) {
245
+		mprintf("!dircopy: Can't copy %s to %s\n", spath, dpath);
246
+		rmdirs(dest);
247
+		closedir(dd);
248
+		return -1;
249
+	    }
250
+	}
251
+    }
252
+
253
+    closedir(dd);
254
+    return 0;
255
+}
256
+
213 257
 int isnumb(const char *str)
214 258
 {
215 259
     while(*str) {
... ...
@@ -27,6 +27,7 @@ void print_version(void);
27 27
 int filecopy(const char *src, const char *dest);
28 28
 int isnumb(const char *str);
29 29
 int rmdirs(const char *dirname);
30
+int dircopy(const char *src, const char *dest);
30 31
 int cvd_unpack(const char *cvd, const char *destdir);
31 32
 
32 33
 #endif