Skip to content

Commit 98b28aa

Browse files
committed
Remove VFS index auto-redirect logic
1 parent d6d3b5c commit 98b28aa

1 file changed

Lines changed: 5 additions & 56 deletions

File tree

  • examples/C++/virtual_file_system

examples/C++/virtual_file_system/vfs.py

Lines changed: 5 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,8 @@
1212
import os
1313
import sys
1414

15-
def generate_vfs_header(directory, output_header, custom_index=None):
15+
def generate_vfs_header(directory, output_header):
1616
files = []
17-
index_files = {}
18-
19-
# Handle custom index file
20-
custom_index_path = None
21-
if custom_index:
22-
custom_index_rel = custom_index.lstrip('/').replace('\\', '/')
23-
custom_index_abs = os.path.join(directory, custom_index_rel)
24-
if os.path.isfile(custom_index_abs):
25-
custom_index_path = '/' + custom_index_rel
2617

2718
# Walk through the directory and collect files
2819
for root, _, filenames in os.walk(directory):
@@ -33,16 +24,6 @@ def generate_vfs_header(directory, output_header, custom_index=None):
3324
relative_path = '/' + relative_path.replace('\\', '/')
3425
files.append((relative_path, filepath))
3526

36-
# Check for index files
37-
if filename.startswith("index."):
38-
dir_path = os.path.dirname(relative_path)
39-
if dir_path not in index_files:
40-
index_files[dir_path] = relative_path
41-
42-
# If a custom index file is provided, override the root index
43-
if custom_index_path is not None:
44-
index_files["/"] = custom_index_path
45-
4627
# Generate the C header file
4728
with open(output_header, 'w') as header:
4829
header.write('#ifndef VIRTUAL_FILE_SYSTEM_H\n')
@@ -75,15 +56,6 @@ def generate_vfs_header(directory, output_header, custom_index=None):
7556

7657
header.write('static const int virtual_files_count = sizeof(virtual_files) / sizeof(virtual_files[0]);\n\n')
7758

78-
header.write('static const char* index_files[] = {\n')
79-
for dir_path, index_path in index_files.items():
80-
if dir_path == "/":
81-
header.write(f' "/", "{index_path}",\n')
82-
else:
83-
header.write(f' "{dir_path}/", "{index_path}",\n')
84-
header.write(' NULL\n')
85-
header.write('};\n\n')
86-
8759
header.write('bool virtual_file_system(const char* path, const unsigned char** file, int* length) {\n')
8860
header.write(' for (int i = 0; i < virtual_files_count; ++i) {\n')
8961
header.write(' if (strcmp(virtual_files[i].path, path) == 0) {\n')
@@ -110,41 +82,18 @@ def generate_vfs_header(directory, output_header, custom_index=None):
11082
header.write(' snprintf((char*) response, header_length + 1, http_header_template, content_type, file_length);\n')
11183
header.write(' memcpy(response + header_length, file_data, file_length);\n')
11284
header.write(' return response;\n')
113-
header.write(' } else {\n')
114-
header.write(' // Check for index file redirection\n')
115-
header.write(' char redirect_path[1024];\n')
116-
header.write(' snprintf(redirect_path, sizeof(redirect_path), "%s", path);\n')
117-
header.write(' size_t len = strlen(redirect_path);\n')
118-
header.write(' if (redirect_path[len - 1] != \'/\') {\n')
119-
header.write(' redirect_path[len] = \'/\';\n')
120-
header.write(' redirect_path[len + 1] = \'\\0\';\n')
121-
header.write(' }\n')
122-
header.write(' for (int i = 0; index_files[i] != NULL; i += 2) {\n')
123-
header.write(' if (strcmp(index_files[i], redirect_path) == 0) {\n')
124-
header.write(' const char* location_header = "HTTP/1.1 302 Found\\r\\n"\n')
125-
header.write(' "Location: %s\\r\\n"\n')
126-
header.write(' "Cache-Control: no-cache\\r\\n\\r\\n";\n')
127-
header.write(' int header_length = snprintf(NULL, 0, location_header, index_files[i + 1]);\n')
128-
header.write(' *length = header_length;\n')
129-
header.write(' unsigned char* response = (unsigned char*) webui_malloc(*length);\n')
130-
header.write(' snprintf((char*) response, header_length + 1, location_header, index_files[i + 1]);\n')
131-
header.write(' return response;\n')
132-
header.write(' }\n')
133-
header.write(' }\n')
134-
header.write(' return NULL;\n')
13585
header.write(' }\n')
86+
header.write(' return NULL;\n')
13687
header.write('}\n\n')
13788

13889
header.write('#endif // VIRTUAL_FILE_SYSTEM_H\n')
13990

14091
if __name__ == '__main__':
141-
if len(sys.argv) not in (3, 4):
142-
print(f'Usage: {sys.argv[0]} <directory> <output_header> [custom_index_filename]')
92+
if len(sys.argv) != 3:
93+
print(f'Usage: {sys.argv[0]} <directory> <output_header>')
14394
sys.exit(1)
14495

14596
directory = sys.argv[1]
14697
output_header = sys.argv[2]
147-
custom_index = sys.argv[3] if len(sys.argv) == 4 else None
148-
149-
generate_vfs_header(directory, output_header, custom_index)
98+
generate_vfs_header(directory, output_header)
15099
print(f'Generated {output_header} from {directory}')

0 commit comments

Comments
 (0)