summaryrefslogtreecommitdiff
blob: 56217d7016c1d60ea932c4d72435b50751708220 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
--- elc/update.c	2006-07-04 20:03:48.000000000 +0100
+++ elc/update.c	2006-07-04 23:34:01.000000000 +0100
@@ -17,6 +17,18 @@
 #include	<sys/stat.h>
 #endif	//WINDOWS
 
+#ifndef S_ISDIR
+#define S_ISDIR(x) ((x) & S_IFMT) == S_IFDIR)
+#endif // S_ISDIR
+
+#ifdef WINDOWS
+#define CHMOD(file) 0
+#define MKDIR(file) mkdir(file)
+#else
+#define CHMOD(file) chmod(file, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP)
+#define MKDIR(file) (mkdir(file, S_IRWXU | S_IRWXG) || chmod(file, S_IRWXU | S_IRWXG | S_ISGID))
+#endif //WINDOWS
+
 int update_attempt_count;   // count how many update attempts have been tried (hopefully diff servers)
 int temp_counter;           // collision prevention during downloads just incase more then one ever starts
 int update_busy;            // state & lockout control to prevent two updates running at the saem rime
@@ -165,18 +177,15 @@
 			strcpy(update_server, update_servers[0]);
 		}
 		// failsafe, try to make sure the directory is there
-		if(mkdir_res < 0){
-#ifdef  WINDOWS
-			mkdir_res= mkdir("./tmp");
-#else   //WINDOWS
-			mkdir_res= mkdir("./tmp", 0777);
-#endif  //WINDOWS
-		}
+		if(mkdir_res < 0)
+			mkdir_res= MKDIR("./tmp");
 		sprintf(filename, "./tmp/temp000.dat");
 		++temp_counter;
 		fp= my_fopen(filename, "wb+");
 		if(fp){
+			CHMOD(filename);
 			sprintf(filename, "http://%s/updates%d%d%d/files.lst", update_server, VER_MAJOR, VER_MINOR, VER_RELEASE);
+	
 			http_threaded_get_file(update_server, filename, fp, NULL, EVENT_UPDATES_DOWNLOADED);
 		}
 		// and keep running until we get a response
@@ -288,6 +297,7 @@
 			buffer[sizeof(buffer)-1]= '\0';
 			fp= my_fopen(download_temp_file, "wb+");
 			if(fp){
+				CHMOD(download_temp_file);
 				// build the prope URL to download
 				download_cur_file= download_queue[--download_queue_size];
 				download_cur_md5= download_MD5s[download_queue_size];
@@ -305,8 +315,6 @@
 // finish up on one file that just downloaded
 void    handle_file_download(struct http_get_struct *get)
 {
-	int sts;
-
 	if(!get){   // huh? what are you doing?
 		return;
 	}
@@ -317,26 +325,51 @@
 		// the download was successful
 		// replace the current file
 		// TODO: check for remove/rename errors
-		remove(download_cur_file);
-		sts= rename(download_temp_file, download_cur_file);
 
-		// check for errors
-		if(!sts){
-			// TODO: make the restart more intelligent
-			if(allow_restart){
-				restart_required++;
-			}
-		} else {
+ 		// First, check directory exists
+ 		char *dir = (char *) malloc (strlen (download_cur_file) + 1);
+ 		char *slash;
+ 		struct stat stats;
+ 
+ 		strcpy (dir, download_cur_file);
+ 		slash = dir;
+ 
+ 		// Skip over leading slashes.
+ 		while (*slash == '/')
+ 			slash++;
+ 
+		while (1){
+			slash = strchr (slash, '/');
+			if (slash == NULL)
+				break;
+
+			*slash = '\0';
+ 			if (! (stat (dir, &stats) == 0 && S_ISDIR (stats.st_mode) ) )
+				if (MKDIR(dir)!= 0) {
+ 					log_error("cannot create directory %s", dir);
+ 					break;
+ 				}
+ 			*slash++ = '/';
+ 
+ 			// Avoid unnecessary calls to mkdir when given
+ 			// file names containing multiple adjacent slashes.
+ 			while (*slash == '/')
+ 				slash++;
+ 		}
+ 
+  		remove(download_cur_file);
+ 		if (rename(download_temp_file, download_cur_file)){
+ 			remove(download_temp_file);
 			log_error("Unable to finish processing of %d (%d)", download_cur_file, errno);
 			// the final renamed failed, no restart permitted
 			allow_restart= 0;
 			restart_required= 0;
+		} else {
+			if(allow_restart)
+				restart_required++;
 		}
-	} else {
-		// and make sure we can't restart since we had a total failure
-		allow_restart= 0;
-		restart_required= 0;
 	}
+
 	// release the filename
 	free(download_cur_file);
 	free(download_cur_md5);
@@ -358,6 +391,7 @@
 		sprintf(download_temp_file, "./tmp/temp%03d.dat", ++temp_counter);
 		fp= my_fopen(download_temp_file, "wb+");
 		if(fp){
+			CHMOD(download_temp_file);
 			// build the prope URL to download
 			download_cur_file= download_queue[--download_queue_size];
 			download_cur_md5= download_MD5s[download_queue_size];