mirror of https://gitlab.com/nakst/essence
				
				
				
			add github cache for download files
This commit is contained in:
		
							parent
							
								
									2ff51cd560
								
							
						
					
					
						commit
						34ee41e39b
					
				| 
						 | 
					@ -1,6 +1,6 @@
 | 
				
			||||||
MIT License
 | 
					MIT License
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Copyright (c) 2017-2021 nakst and other contributors (see CONTRIBUTING.md).
 | 
					Copyright (c) 2017-2022 nakst and other contributors (see CONTRIBUTING.md).
 | 
				
			||||||
 | 
					
 | 
				
			||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
					Permission is hereby granted, free of charge, to any person obtaining a copy
 | 
				
			||||||
of this software and associated documentation files (the "Software"), to deal
 | 
					of this software and associated documentation files (the "Software"), to deal
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
							
								
								
									
										32
									
								
								util/build.c
								
								
								
								
							
							
						
						
									
										32
									
								
								util/build.c
								
								
								
								
							| 
						 | 
					@ -1171,6 +1171,7 @@ void GetSource(const char *parameters, const char *checksum) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	char name[1024];
 | 
						char name[1024];
 | 
				
			||||||
	strcpy(name, "bin/cache/");
 | 
						strcpy(name, "bin/cache/");
 | 
				
			||||||
 | 
						char *nameWithoutDirectory = name + 10;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	const char *extension = url;
 | 
						const char *extension = url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -1193,15 +1194,39 @@ void GetSource(const char *parameters, const char *checksum) {
 | 
				
			||||||
		exit(1);
 | 
							exit(1);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						char *alternateURL = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						if (checksum) {
 | 
				
			||||||
 | 
							// If we're verifying the checksum of the file, then it should be okay to try downloading it from a non-official mirror.
 | 
				
			||||||
 | 
							const char *alternateURLPrefix = "https://github.com/nakst/cdn/raw/main/cache/";
 | 
				
			||||||
 | 
							alternateURL = (char *) malloc(strlen(alternateURLPrefix) + strlen(nameWithoutDirectory) + 1);
 | 
				
			||||||
 | 
							strcpy(alternateURL, alternateURLPrefix);
 | 
				
			||||||
 | 
							strcat(alternateURL, nameWithoutDirectory);
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	FILE *f = fopen(name, "rb");
 | 
						FILE *f = fopen(name, "rb");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	if (f) {
 | 
						if (f) {
 | 
				
			||||||
 | 
							// The file is cached.
 | 
				
			||||||
		fclose(f);
 | 
							fclose(f);
 | 
				
			||||||
	} else if (CallSystemF("curl -L %s > %s", url, name)) {
 | 
						} else {
 | 
				
			||||||
		CallSystemF("rm %s", name); // Remove partially downloaded file.
 | 
							if (alternateURL) {
 | 
				
			||||||
		exit(1);
 | 
								fprintf(stderr, "Attempting to download from '%s' with fallback '%s'...\n", alternateURL, url);
 | 
				
			||||||
 | 
							} else {
 | 
				
			||||||
 | 
								fprintf(stderr, "Attempting to download from '%s'...\n", url);
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							
 | 
				
			||||||
 | 
							if (!alternateURL || CallSystemF("curl -f -L %s > %s", alternateURL, name)) {
 | 
				
			||||||
 | 
								if (CallSystemF("curl -L %s > %s", url, name)) {
 | 
				
			||||||
 | 
									CallSystemF("rm %s", name); // Remove partially downloaded file.
 | 
				
			||||||
 | 
									fprintf(stderr, "Error: Could not download the file at '%s'. Exiting.\n", url);
 | 
				
			||||||
 | 
									exit(1);
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						free(alternateURL);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#ifdef __APPLE__
 | 
					#ifdef __APPLE__
 | 
				
			||||||
#define SHA256SUM "sha256 -a 256"
 | 
					#define SHA256SUM "sha256 -a 256"
 | 
				
			||||||
#else
 | 
					#else
 | 
				
			||||||
| 
						 | 
					@ -1220,6 +1245,7 @@ void GetSource(const char *parameters, const char *checksum) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		if (CallSystemF(SHA256SUM " %s | grep %.*s > /dev/null", name, checksumLength, checksum)) {
 | 
							if (CallSystemF(SHA256SUM " %s | grep %.*s > /dev/null", name, checksumLength, checksum)) {
 | 
				
			||||||
			fprintf(stderr, "Checksum mismatch for file '%s' downloaded from '%s'.\n", name, url);
 | 
								fprintf(stderr, "Checksum mismatch for file '%s' downloaded from '%s'.\n", name, url);
 | 
				
			||||||
 | 
								CallSystemF("rm %s", name);
 | 
				
			||||||
			exit(1);
 | 
								exit(1);
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue