Add write permissions check on output folder

This commit is contained in:
Mark Naughton
2023-05-19 18:37:55 +01:00
parent 413077a5d9
commit 018904f0ec
2 changed files with 54 additions and 1 deletions

View File

@@ -1504,7 +1504,8 @@ gb_internal bool init_build_paths(String init_filename) {
if (build_context.metrics.os == TargetOs_windows) {
output_extension = STR_LIT("exe");
} else if (build_context.cross_compiling && selected_target_metrics->metrics == &target_essence_amd64) {
output_extension = make_string(nullptr, 0);
// Do nothing: we don't want the .bin extension
// when cross compiling
} else if (path_is_directory(last_path_element(bc->build_paths[BuildPath_Main_Package].basename))) {
// Add .bin extension to avoid collision
// with package directory name
@@ -1624,6 +1625,22 @@ gb_internal bool init_build_paths(String init_filename) {
return false;
}
if (path_is_directory(bc->build_paths[BuildPath_Output])) {
String output_file = path_to_string(ha, bc->build_paths[BuildPath_Output]);
defer (gb_free(ha, output_file.text));
gb_printf_err("Output path %.*s is a directory.\n", LIT(output_file));
return false;
}
//nocheckin char const *pathname = (char *)bc->build_paths[BuildPath_Output].basename.text;
if (!write_directory(bc->build_paths[BuildPath_Output].basename)) {
String output_file = path_to_string(ha, bc->build_paths[BuildPath_Output]);
defer (gb_free(ha, output_file.text));
gb_printf_err("No write permissions for output path: %.*s\n", LIT(output_file));
return false;
}
if (bc->target_features_string.len != 0) {
enable_target_feature({}, bc->target_features_string);
}

View File

@@ -419,7 +419,43 @@ gb_internal ReadDirectoryError read_directory(String path, Array<FileInfo> *fi)
return ReadDirectory_None;
}
#else
#error Implement read_directory
#endif
#if !defined(GB_SYSTEM_WINDOWS)
gb_internal bool write_directory(String path) {
char const *pathname = (char *) path.text;
if (access(pathname, W_OK) < 0) {
return false;
}
return true;
}
#else
gb_internal bool write_directory(String path) {
String16wstr = string_to_string16(heap_allocator(), path);
LPCWSTR wdirectory_name = wstr.text;
HANDLE directory = CreateFileW(wdirectory_name,
GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
NULL);
if (directory == INVALID_HANDLE_VALUE) {
DWORD error_code = GetLastError();
if (error_code == ERROR_ACCESS_DENIED) {
return false;
}
}
CloseHandle(directory);
return true;
}
#endif