Add fat32_test0 test

This commit is contained in:
mkostoevr 2021-12-12 13:37:44 +03:00 committed by Ivan Baravy
parent 334866c209
commit 08648324cb
6 changed files with 21972 additions and 2 deletions

View File

@ -1,9 +1,11 @@
RANDDIR=../tools/randdir
MKDIRRANGE=../tools/mkdirrange
MKFILEPATTERN=../tools/mkfilepattern
DIRTOTEST=python3 ../tools/dirtotest.py
MOUNT_OPT=-t xfs
TEMP_DIR:=$(shell mktemp -d)
all: s05k s4k unicode v5 kolibri.img coverage
all: s05k s4k unicode v5 kolibri.img fat32_test0.img coverage
rmdir $(TEMP_DIR)
s05k: xfs_v4_ftype0_s05k_b2k_n8k.img xfs_v4_ftype1_s05k_b2k_n8k.img xfs_v4_xattr.img xfs_v4_files_s05k_b4k_n8k.img xfs_v4_ftype0_s05k_b2k_n8k_xattr.img xfs_v4_btrees_l2.img xfs_short_dir_i8.img
@ -353,4 +355,13 @@ xfs_v5_files_s05k_b4k_n8k.img:
parted --script $@ mktable gpt
parted --script --align optimal $@ mkpart primary 1MiB 100%
fat32_test0.img:
fallocate -l 127MiB $@
mkfs.fat -n KOLIBRIOS -F 32 $@
sudo mount $@ $(TEMP_DIR)
sudo chown $$USER $(TEMP_DIR) -R
$(RANDDIR) $(TEMP_DIR) 4096 8 255 65536
$(DIRTOTEST) $(TEMP_DIR) fat32_test0.img hd0 > ../test/045_#f70_#fat32_test0.t
sudo umount $(TEMP_DIR)

File diff suppressed because it is too large Load Diff

4100
test/045_#f70_#fat32_test0.t Normal file

File diff suppressed because it is too large Load Diff

46
tools/dirtotest.py Normal file
View File

@ -0,0 +1,46 @@
import os
import sys
import shutil
def handle_file(path):
path_without_root_folder_name = path.replace(f"{folder}/", "")
virtual_path = f"{disk}/{path_without_root_folder_name}"
print(f"read70 {virtual_path} 0 16388096 -h")
def handle_dir(path):
if path == folder:
print(f"ls80 {disk}/")
else:
path_without_root_folder_name = path.replace(f"{folder}/", "")
virtual_path = f"{disk}/{path_without_root_folder_name}"
print(f"ls80 {virtual_path}/")
items = [f"{path}/{f}" for f in os.listdir(path)]
items.sort()
for item in items:
if os.path.isdir(item):
handle_dir(item)
else:
handle_file(item)
if __name__ == "__main__":
if len(sys.argv) < 2:
print(f"Usage: {sys.argv[0]} <folder_name> [<img_name> [<virtual_disk_name>]]")
exit()
if len(sys.argv) > 2:
img_name = sys.argv[2]
else:
img_name = "fat32_test0.img"
if len(sys.argv) > 3:
virtual_disk_name = sys.argv[3]
else:
virtual_disk_name = "hd0"
print("umka_init")
print(f"disk_add ../img/{img_name} {virtual_disk_name} -c 0")
disk = f"/{virtual_disk_name}/1"
folder = sys.argv[1]
handle_dir(folder)
print(f"disk_del {virtual_disk_name}")

View File

@ -7,7 +7,7 @@ CFLAGS=$(WARNINGS) $(NOWARNINGS) -std=c11 -O2 \
-DNDEBUG -D_POSIX_C_SOURCE=200809L -fno-pie
LDFLAGS=-no-pie
all: mkdirrange mkfilepattern lfbviewx
all: mkdirrange mkfilepattern lfbviewx randdir
mkdirrange: mkdirrange.c
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
@ -18,6 +18,9 @@ mkfilepattern: mkfilepattern.c
lfbviewx: lfbviewx.c
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@ -lX11 -lXext -D_GNU_SOURCE
randdir: randdir.c
$(CC) $(CFLAGS) $(LDFLAGS) $< -o $@
.PHONY: all clean
clean:

165
tools/randdir.c Normal file
View File

@ -0,0 +1,165 @@
#include <stdio.h>
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#define PRINTABLES "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
char *printables = PRINTABLES;
size_t printables_cnt = sizeof(PRINTABLES);
unsigned uint_hash(unsigned x) {
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = ((x >> 16) ^ x) * 0x45d9f3b;
x = (x >> 16) ^ x;
return x;
}
void create_directory(int root_dirfd, char *path) {
if(mkdirat(root_dirfd, path, 0755)) {
fprintf(stderr, "Can't mkdir %s: %s\n", path, strerror(errno));
exit(1);
}
}
void create_file_impl(int rootdirfd, char *path, size_t size, char *contents) {
int fd = openat(rootdirfd, path, O_CREAT | O_WRONLY, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd == -1) {
fprintf(stderr, "Can't open %s: %s\n", path, strerror(errno));
exit(1);
}
write(fd, contents, size);
close(fd);
}
void create_file(int rootdirfd, char *path, size_t max_size, unsigned seed) {
size_t path_len = strlen(path);
char *header = "<FILE>";
char *footer = "</FILE>";
size_t header_size = strlen(header);
size_t footer_size = strlen(footer);
size_t data_size = seed % max_size;
if (data_size + header_size + footer_size > max_size) {
data_size = max_size - header_size - footer_size;
}
size_t file_size = data_size + header_size + footer_size;
assert(file_size <= max_size);
char *contents = calloc(1, file_size);
if (contents == NULL) {
fprintf(stderr, "Can't allocate memory for the file of size %lu\n", file_size);
exit(1);
}
// First is header
memcpy(contents, header, header_size);
// Then is data
for (unsigned i = header_size; i < header_size + data_size; i++) {
contents[i] = (char)uint_hash(i) ^ path[i % path_len] ^ (char)seed;
}
// The last is footer
memcpy(contents + header_size + data_size, footer, footer_size);
create_file_impl(rootdirfd, path, file_size, contents);
}
void path_go_back(char *path) {
char *last_separator = strrchr(path, '/');
if (last_separator) {
*last_separator = '\0';
}
}
void path_append_impl(char *path, unsigned path_max, unsigned name_max, unsigned seed) {
size_t path_len = strlen(path);
unsigned name_len = seed % name_max;
if (name_len == 0) {
name_len = 1;
}
if (path_len + 1 + name_len >= path_max) {
return;
}
strcat(path, "/");
char name_buf[name_len + 1];
for (unsigned i = 0; i < name_len; i++) {
size_t rand_idx = seed ^ (139 * i);
name_buf[i] = printables[rand_idx % printables_cnt];
}
name_buf[name_len] = '\0';
strcat(path, name_buf);
}
int file_exists(int rootdirfd, char *path) {
return !faccessat(rootdirfd, path, W_OK, 0);
}
void path_append(int rootdirfd, char *path, unsigned path_max, unsigned name_max, unsigned seed) {
do {
path_append_impl(path, path_max, name_max, seed);
if (file_exists(rootdirfd, path)) {
// Generate new feed in case if the current one generated existing filename
seed = uint_hash(seed);
path_go_back(path);
continue;
}
break;
} while (1);
}
int in_range(unsigned v, unsigned begin, unsigned end) {
return v >= begin && v <= end;
}
int main(int argc, char *argv[])
{
unsigned random_things_count, name_max, path_max, file_size_max;
char *path;
if (argc == 6) {
path = argv[1];
sscanf(argv[2], "%u", &random_things_count);
sscanf(argv[3], "%u", &name_max);
sscanf(argv[4], "%u", &path_max);
sscanf(argv[5], "%u", &file_size_max);
} else {
fprintf(stderr, "randdir <directory> <random_things_count> <name_max> <path_max> <file_size_max>\n"
" directory - the folder to create random stuff in\n"
" random_things_count - count of things (folders and files) to generate\n"
" name_max - max length of a file or folder name\n"
" path_max - max length of a path relative to the root folder\n"
" file_size_max - max size of a generated file");
exit(1);
}
int rootdirfd = open(path, O_DIRECTORY);
if (rootdirfd == -1) {
fprintf(stderr, "Can't open root folder (%s): %s\n", path, strerror(errno));
exit(1);
}
char path_buf[path_max + 1];
strcpy(path_buf, ".");
for (unsigned i = 0; i < random_things_count; i++) {
unsigned h = uint_hash(i);
// Create a folder and get into it in 15% of cases
if (in_range(h % 100, 85, 99)) {
path_append(rootdirfd, path_buf, path_max, name_max, h);
create_directory(rootdirfd, path_buf);
continue;
}
// Get out of the folder in 15% of cases
if (in_range(h % 100, 0, 14)) {
path_go_back(path_buf);
}
// Create a file in 70% of cases
path_append(rootdirfd, path_buf, path_max, name_max, h);
create_file(rootdirfd, path_buf, file_size_max, h);
path_go_back(path_buf);
}
return 0;
}