mtd-utils: fs-tests: integck: clean-ups
cover mtd-utils: fs-tests: integck: clean-ups
Here are some integck clean-ups I made so far while trying to improve it.
Artem.
PATCH 1/6 fs-tests: integck: introduce zalloc
From: Artem Bityutskiy <[email protected]>
The integck test often allocates memory and fills it with zeroes. Introduce a helper function for this frequent operation.
This is just a clean-up patch which makes the code 23 lines shorter.
--- tests/fs-tests/integrity/integck.c | 67 ++++++++++++------------------------ 1 files changed, 22 insertions(+), 45 deletions(-)
tests/fs-tests/integrity/integck.c +22 -45
@@ -135,6 +135,18 @@ static long mem_page_size; /* Page size for mmap */static uint64_t check_run_no; +/* +* Allocate a buffer of 'size' bytes and fill it with zeroes. +*/ +static void *zalloc (size_t size) +{ +void *buf =malloc (size); + +CHECK (buf !=NULL ); +memset (buf,0 , size); +return buf; +} +static char *copy_string (const char *s) {char *str;
@@ -218,12 +230,8 @@ static char *dir_path(struct dir_info *parent, const char *name)static void open_file_add (struct fd_info *fdi) {struct open_file_info *ofi; -size_t sz; - sz =sizeof (struct open_file_info ); - ofi = (struct open_file_info *)malloc (sz); -CHECK (ofi !=NULL ); -memset (ofi,0 , sz); + ofi =zalloc (sizeof (struct open_file_info )); ofi->next = open_files; ofi->fdi = fdi; open_files = ofi;
@@ -251,12 +259,8 @@ static void open_file_remove(struct fd_info *fdi)static struct fd_info *add_fd (struct file_info *file,int fd) {struct fd_info *fdi; -size_t sz; - sz =sizeof (struct fd_info ); - fdi = (struct fd_info *)malloc (sz); -CHECK (fdi !=NULL ); -memset (fdi,0 , sz); + fdi =zalloc (sizeof (struct fd_info )); fdi->next = file->fds; fdi->file = file; fdi->fd = fd;
@@ -269,13 +273,8 @@ static void add_dir_entry(struct dir_info *parent, char type, const char *name, void *target) {struct dir_entry_info *entry; -size_t sz; - - sz =sizeof (struct dir_entry_info ); - entry = (struct dir_entry_info *)malloc (sz); -CHECK (entry !=NULL ); -memset (entry,0 , sz); + entry =zalloc (sizeof (struct dir_entry_info )); entry->type = type; entry->name =copy_string (name); entry->parent = parent;
@@ -341,7 +340,6 @@ static void remove_dir_entry(struct dir_entry_info *entry)static struct dir_info *dir_new (struct dir_info *parent,const char *name) {struct dir_info *dir; -size_t sz;char *path; path =dir_path (parent, name);
@@ -353,10 +351,7 @@ static struct dir_info *dir_new(struct dir_info *parent, const char *name) }free (path); - sz =sizeof (struct dir_info ); - dir = (struct dir_info *)malloc (sz); -CHECK (dir !=NULL ); -memset (dir,0 , sz); + dir =zalloc (sizeof (struct dir_info )); dir->name =copy_string (name); dir->parent = parent;if (parent)
@@ -400,7 +395,6 @@ static struct file_info *file_new(struct dir_info *parent, const char *name)char *path;mode_t mode;int fd; -size_t sz;CHECK (parent !=NULL );
@@ -415,10 +409,7 @@ static struct file_info *file_new(struct dir_info *parent, const char *name) }free (path); - sz =sizeof (struct file_info ); - file = (struct file_info *)malloc (sz); -CHECK (file !=NULL ); -memset (file,0 , sz); + file =zalloc (sizeof (struct file_info )); file->name =copy_string (name);add_dir_entry (parent,'f' , name, file);
@@ -662,21 +653,15 @@ static void file_write_info(struct file_info *file, {struct write_info *new_write, *w, **prev, *tmp;int inserted; -size_t sz;off_t end, chg;/* Create struct write_info */ - sz =sizeof (struct write_info ); - new_write = (struct write_info *)malloc (sz); -CHECK (new_write !=NULL ); -memset (new_write,0 , sz); + new_write =zalloc (sizeof (struct write_info )); new_write->offset = offset; new_write->size = size; new_write->random_seed = seed; - w = (struct write_info *)malloc (sz); -CHECK (w !=NULL ); -memset (w,0 , sz); + w =zalloc (sizeof (struct write_info )); w->next = file->raw_writes; w->offset = offset; w->size = size;
@@ -707,7 +692,7 @@ static void file_write_info(struct file_info *file,else {/* w ends after new_write ends */ /* Split w */ - tmp = (struct write_info *)malloc (sz); + tmp =malloc (sizeof (struct write_info ));CHECK (tmp !=NULL ); *tmp = *w; chg = end - tmp->offset;
@@ -921,7 +906,6 @@ static void file_write_file(struct file_info *file)static void file_truncate_info (struct file_info *file,size_t new_length) {struct write_info *w, **prev, *tmp; -size_t sz;/* Remove / truncate file->writes */ w = file->writes;
@@ -941,10 +925,7 @@ static void file_truncate_info(struct file_info *file, size_t new_length) w = w->next; }/* Add an entry in raw_writes for the truncation */ - sz =sizeof (struct write_info ); - w = (struct write_info *)malloc (sz); -CHECK (w !=NULL ); -memset (w,0 , sz); + w =zalloc (sizeof (struct write_info )); w->next = file->raw_writes; w->offset = file->length; w->random_offset = new_length;/* Abuse random_offset */
@@ -1659,7 +1640,6 @@ static void symlink_new(struct dir_info *dir, const char *name_) {struct symlink_info *s;char *path, *target, *name =copy_string (name_); -size_t sz; path =dir_path (dir, name); target =pick_symlink_target (path);
@@ -1674,10 +1654,7 @@ static void symlink_new(struct dir_info *dir, const char *name_) }free (path); - sz =sizeof (struct symlink_info ); - s =malloc (sz); -CHECK (s !=NULL ); -memset (s,0 , sz); + s =zalloc (sizeof (struct symlink_info ));add_dir_entry (dir,'s' , name, s); s->target_pathname = target;free (name);
Signature · 1 line
1.7.2.3
PATCH 2/6 fs-tests: integck: do not cast void pointers
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
The malloc function returns 'void *', so it is not necessary to cast it when assigning. This is just a small clean-up patch.
--- tests/fs-tests/integrity/integck.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-)
tests/fs-tests/integrity/integck.c +4 -4
@@ -153,7 +153,7 @@ static char *copy_string(const char *s)if (!s)return NULL ; - str = (char *)malloc (strlen (s) +1 ); + str =malloc (strlen (s) +1 );CHECK (str !=NULL );strcpy (str, s);return str;
@@ -171,7 +171,7 @@ static char *cat_strings(const char *a, const char *b)if (!a && !b)return NULL ; sz =strlen (a) +strlen (b) +1 ; - str = (char *)malloc (sz); + str =malloc (sz);CHECK (str !=NULL );strcpy (str, a);strcat (str, b);
@@ -206,7 +206,7 @@ static char *cat_paths(const char *a, const char *b)return cat_strings (a, b +1 ); sz = na + nb +2 ; - str = (char *)malloc (sz); + str =malloc (sz);CHECK (str !=NULL );strcpy (str, a);strcat (str,"/" );
@@ -1286,7 +1286,7 @@ static void dir_check(struct dir_info *dir)/* Create an array of entries */ sz =sizeof (struct dir_entry_info *); n = dir->number_of_entries; - entry_array = (struct dir_entry_info **)malloc (sz * n); + entry_array =malloc (sz * n);CHECK (entry_array !=NULL ); entry = dir->first;
Signature · 1 line
1.7.2.3
PATCH 4/6 fs-tests: integck: remove unnecessary check
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
The 'link_new()' function has only one user, and the user checks that the third 'file' argument is non NULL, so remove this check from 'link_new()'. Let's be consistent and assume the 3rd argument is never NULL, just like the first and the second ones.
This is just a minor improvement.
--- tests/fs-tests/integrity/integck.c | 2 -- 1 files changed, 0 insertions(+), 2 deletions(-)
tests/fs-tests/integrity/integck.c +0 -2
@@ -425,8 +425,6 @@ static void link_new(struct dir_info *parent, const char *name,char *path, *target;int ret; -if (!file) -return ; entry = file->links;if (!entry)return ;
Signature · 1 line
1.7.2.3
PATCH 5/6 fs-tests: integck: include the common mtd-utils header
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
Include the commong mtd-utils "common.h" header in integck test to make it possible to use shared macros.
--- tests/fs-tests/integrity/Makefile | 4 +++- tests/fs-tests/integrity/integck.c | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)
tests/fs-tests/integrity/Makefile +3 -1
@@ -3,7 +3,9 @@ ifeq ($(origin CC),default) CC = gcc endif -CFLAGS := $(CFLAGS) -Wall -g -O2 -I../lib +COMMON_HEADERS_DIR := ../../../include + +CFLAGS := $(CFLAGS) -Wall -g -O2 -I../lib -I$(COMMON_HEADERS_DIR) LDFLAGS := $(LDFLAGS)
tests/fs-tests/integrity/integck.c +3 -1
@@ -30,9 +30,11 @@#include <limits.h> #include <dirent.h> #include <sys/mman.h> -#include "tests.h" +#define PROGRAM_NAME "integck" +#include "common.h" +/* Structures to store data written to the test file system, so that we can check whether the file system is correct. */
Signature · 1 line
1.7.2.3
PATCH 3/6 fs-tests: integck: simplify dir_entry_info fields
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
Instead of using named union 'entry' in 'struct dir_entry_info' and having to type long 'entry->entry.file', 'entry->entry.symlink', etc, use anonymous union which makes the syntax simpler: 'entry->file', 'entry->symlink', etc.
--- tests/fs-tests/integrity/integck.c | 59 +++++++++++++++++------------------ 1 files changed, 29 insertions(+), 30 deletions(-)
tests/fs-tests/integrity/integck.c +29 -30
@@ -89,13 +89,12 @@ struct dir_entry_info /* Each entry in a directory has one of these */struct dir_info *parent;/* Parent directory */ char type;/* f => file, d => dir, s => symlink */ int checked;/* Temporary flag used when checking */ -union entry_ - { +union {struct file_info *file;struct dir_info *dir;struct symlink_info *symlink;void *target; - } entry; + }; };struct fd_info /* We keep a number of files open */
@@ -288,7 +287,7 @@ static void add_dir_entry(struct dir_info *parent, char type, const char *name,if (entry->type =='f' ) {struct file_info *file = target; - entry->entry.file = file; + entry->file = file; entry->next_link = file->links;if (file->links) file->links->prev_link = entry;
@@ -297,14 +296,14 @@ static void add_dir_entry(struct dir_info *parent, char type, const char *name, }else if (entry->type =='d' ) {struct dir_info *dir = target; - entry->entry.dir = dir; + entry->dir = dir; dir->entry = entry; dir->name =copy_string (name); dir->parent = parent; }else if (entry->type =='s' ) {struct symlink_info *symlink = target; - entry->entry.symlink = symlink; + entry->symlink = symlink; symlink->entry = entry; } }
@@ -320,7 +319,7 @@ static void remove_dir_entry(struct dir_entry_info *entry) entry->next->prev = entry->prev;if (entry->type =='f' ) { -struct file_info *file = entry->entry.file; +struct file_info *file = entry->file;if (entry->prev_link) entry->prev_link->next_link = entry->next_link;
@@ -373,11 +372,11 @@ static void dir_remove(struct dir_info *dir) entry = dir->first;if (entry->type =='d' ) -dir_remove (entry->entry.dir); +dir_remove (entry->dir);else if (entry->type =='f' )file_unlink (entry);else if (entry->type =='s' ) -symlink_remove (entry->entry.symlink); +symlink_remove (entry->symlink);else CHECK (0 );/* Invalid struct dir_entry_info */ }
@@ -463,7 +462,7 @@ static void file_close_all(struct file_info *file)static void file_unlink (struct dir_entry_info *entry) { -struct file_info *file = entry->entry.file; +struct file_info *file = entry->file;char *path; path =dir_path (entry->parent, entry->name);
@@ -1325,12 +1324,12 @@ static void dir_check(struct dir_info *dir) entry = dir->first;while (entry) {if (entry->type =='d' ) { -dir_check (entry->entry.dir); +dir_check (entry->dir); link_count +=1 ;/* <subdir>/.. */ }else if (entry->type =='f' ) -file_check (entry->entry.file,-1 ); +file_check (entry->file,-1 );else if (entry->type =='s' ) -symlink_check (entry->entry.symlink); +symlink_check (entry->symlink);else CHECK (0 ); entry = entry->next;
@@ -1405,13 +1404,13 @@ static struct file_info *pick_file(void)if (!entry)return NULL ;if (entry->type =='f' ) -return entry->entry.file; +return entry->file;if (entry->type =='d' ) -if (entry->entry.dir->number_of_entries !=0 ) +if (entry->dir->number_of_entries !=0 )break ; entry = entry->next; } - dir = entry->entry.dir; + dir = entry->dir; } }
@@ -1450,7 +1449,7 @@ static struct dir_info *pick_dir(void) }if (!entry)return dir; - dir = entry->entry.dir; + dir = entry->dir;if (tests_random_no (40 ) >=30 )return dir; }
@@ -1478,7 +1477,7 @@ static char *pick_rename_name(struct dir_info **parent,if (!entry) entry = dir->first;if (!entry || - (entry->type =='d' && entry->entry.dir->number_of_entries !=0 )) + (entry->type =='d' && entry->dir->number_of_entries !=0 ))return copy_string (make_name (dir)); if ((isdir && entry->type != 'd') ||
@@ -1512,9 +1511,9 @@ static void rename_entry(struct dir_entry_info *entry)struct dir_info *p;for (p = parent; p; p = p->parent) -if (p == entry->entry.dir) +if (p == entry->dir)break ; -if (p == entry->entry.dir) { +if (p == entry->dir) {free (path);free (name);free (to);
@@ -1543,12 +1542,12 @@ static void rename_entry(struct dir_entry_info *entry)free (to);if (rename_entry && rename_entry->type == entry->type && - rename_entry->entry.target == entry->entry.target) { + rename_entry->target == entry->target) {free (name);return ; } -add_dir_entry (parent, entry->type, name, entry->entry.target); +add_dir_entry (parent, entry->type, name, entry->target);if (rename_entry)remove_dir_entry (rename_entry);remove_dir_entry (entry);
@@ -1684,33 +1683,33 @@ static void operate_on_entry(struct dir_entry_info *entry)return ; }if (entry->type =='s' ) { -symlink_check (entry->entry.symlink); +symlink_check (entry->symlink);/* If shrinking, 1 time in 50, remove a symlink */ if (shrink &&tests_random_no (50 ) ==0 ) -symlink_remove (entry->entry.symlink); +symlink_remove (entry->symlink);return ; }if (entry->type =='d' ) {/* If shrinking, 1 time in 50, remove a directory */ if (shrink &&tests_random_no (50 ) ==0 ) { -dir_remove (entry->entry.dir); +dir_remove (entry->dir);return ; } -operate_on_dir (entry->entry.dir); +operate_on_dir (entry->dir); }if (entry->type =='f' ) {/* If shrinking, 1 time in 10, remove a file */ if (shrink &&tests_random_no (10 ) ==0 ) { -file_delete (entry->entry.file); +file_delete (entry->file);return ; }/* If not growing, 1 time in 10, unlink a file with links > 1 */ -if (!grow && entry->entry.file->link_count >1 && +if (!grow && entry->file->link_count >1 &&tests_random_no (10 ) ==0 ) { -file_unlink_file (entry->entry.file); +file_unlink_file (entry->file);return ; } -operate_on_file (entry->entry.file); +operate_on_file (entry->file); } }
Signature · 1 line
1.7.2.3
PATCH 6/6 fs-tests: integck: use common printing macros
In reply to Artem BityutskiyFrom: Artem Bityutskiy <[email protected]>
Instead of using printf() directly, use the shared mtd-utils printing macros.
--- tests/fs-tests/integrity/integck.c | 89 ++++++++++++++++-------------------- 1 files changed, 39 insertions(+), 50 deletions(-)
tests/fs-tests/integrity/integck.c +39 -50
@@ -533,58 +533,51 @@ static void file_info_display(struct file_info *file)struct write_info *w;unsigned wcnt; -fprintf (stderr,"File Info:\n" ); -fprintf (stderr," Original name: %s\n" , file->name); -fprintf (stderr," Link count: %d\n" , file->link_count); -fprintf (stderr," Links:\n" ); +normsg ("File Info:" ); +normsg (" Original name: %s" , file->name); +normsg (" Link count: %d" , file->link_count); +normsg (" Links:" ); entry = file->links;while (entry) { -fprintf (stderr," Name: %s\n" , entry->name); -fprintf (stderr," Directory: %s\n" , entry->parent->name); +normsg (" Name: %s" , entry->name); +normsg (" Directory: %s" , entry->parent->name); entry = entry->next_link; } -fprintf (stderr," Length: %u\n" , (unsigned ) file->length); -fprintf (stderr," File was open: %s\n" , - (file->fds ==NULL ) ?"false" :"true" ); -fprintf (stderr," File was deleted: %s\n" , - (file->deleted ==0 ) ?"false" :"true" ); -fprintf (stderr," File was out of space: %s\n" , - (file->no_space_error ==0 ) ?"false" :"true" ); -fprintf (stderr," File Data:\n" ); +normsg (" Length: %u" , (unsigned ) file->length); +normsg (" File was open: %s" , + (file->fds ==NULL ) ?"false" :"true" ); +normsg (" File was deleted: %s" , + (file->deleted ==0 ) ?"false" :"true" ); +normsg (" File was out of space: %s" , + (file->no_space_error ==0 ) ?"false" :"true" ); +normsg (" File Data:" ); wcnt =0 ; w = file->writes;while (w) { -fprintf (stderr," Offset: %u Size: %u Seed: %u" -" R.Off: %u\n" , - (unsigned ) w->offset, - (unsigned ) w->size, - (unsigned ) w->random_seed, - (unsigned ) w->random_offset); +normsg (" Offset: %u Size: %u Seed: %u R.Off: %u" , + (unsigned ) w->offset, (unsigned ) w->size, + (unsigned ) w->random_seed, (unsigned ) w->random_offset); wcnt +=1 ; w = w->next; } -fprintf (stderr," %u writes\n" , wcnt); -fprintf (stderr," ============================================\n" ); -fprintf (stderr," Write Info:\n" ); +normsg (" %u writes" , wcnt); +normsg (" ============================================" ); +normsg (" Write Info:" ); wcnt =0 ; w = file->raw_writes;while (w) {if (w->trunc) -fprintf (stderr," Trunc from %u to %u\n" , - (unsigned ) w->offset, - (unsigned ) w->random_offset); +normsg (" Trunc from %u to %u" , + (unsigned ) w->offset, (unsigned ) w->random_offset);else -fprintf (stderr," Offset: %u Size: %u Seed: %u" -" R.Off: %u\n" , - (unsigned ) w->offset, - (unsigned ) w->size, - (unsigned ) w->random_seed, - (unsigned ) w->random_offset); +normsg (" Offset: %u Size: %u Seed: %u R.Off: %u" , + (unsigned ) w->offset, (unsigned ) w->size, + (unsigned ) w->random_seed, (unsigned ) w->random_offset); wcnt +=1 ; w = w->next; } -fprintf (stderr," %u writes or truncations\n" , wcnt); -fprintf (stderr," ============================================\n" ); +normsg (" %u writes or truncations" , wcnt); +normsg (" ============================================" ); }static struct fd_info *file_open (struct file_info *file)
@@ -1035,7 +1028,7 @@ static void save_file(int fd, struct file_info *file)strcpy (name,"/tmp/" );strcat (name, file->name);strcat (name,".integ.sav.read" ); -fprintf (stderr,"Saving %s\n" , name); +normsg ("Saving %sn" , name); w_fd =open (name,O_CREAT |O_WRONLY ,0777 );CHECK (w_fd !=-1 );
@@ -1055,7 +1048,7 @@ static void save_file(int fd, struct file_info *file)strcpy (name,"/tmp/" );strcat (name, file->name);strcat (name,".integ.sav.written" ); -fprintf (stderr,"Saving %s\n" , name); +normsg ("Saving %s" , name); w_fd =open (name,O_CREAT |O_WRONLY ,0777 );CHECK (w_fd !=-1 );
@@ -1082,11 +1075,10 @@ static void file_check_hole( struct file_info *file,CHECK (read (fd, buf, block) == block);for (i =0 ; i < block; ++i) {if (buf[i] !=0 ) { -fprintf (stderr,"file_check_hole failed at %u " -"checking hole at %u size %u\n" , - (unsigned ) (size - remains + i), - (unsigned ) offset, - (unsigned ) size); +errmsg ("file_check_hole failed at %u checking " +"hole at %u size %u" , + (unsigned ) (size - remains + i), + (unsigned ) offset, (unsigned ) size);file_info_display (file);save_file (fd, file); }
@@ -1118,11 +1110,10 @@ static void file_check_data( struct file_info *file,for (i =0 ; i < block; ++i) {char c = (char )rand ();if (buf[i] != c) { -fprintf (stderr,"file_check_data failed at %u " -"checking data at %u size %u\n" , +errmsg ("file_check_data failed at %u checking " +"data at %u size %u" , (unsigned ) (w->size - remains + i), - (unsigned ) w->offset, - (unsigned ) w->size); + (unsigned ) w->offset, (unsigned ) w->size);file_info_display (file);save_file (fd, file); }
@@ -1159,10 +1150,8 @@ static void file_check(struct file_info *file, int fd)/* Check length */ pos =lseek (fd,0 ,SEEK_END );if (pos != file->length) { -fprintf (stderr,"file_check failed checking length " -"expected %u actual %u\n" , - (unsigned ) file->length, - (unsigned ) pos); +errmsg ("file_check failed checking length expected %u actual %u\n" , + (unsigned ) file->length, (unsigned ) pos);file_info_display (file);save_file (fd, file); }
@@ -1934,7 +1923,7 @@ void integck(void)CHECK (mem_page_size >0 );/* Make our top directory */ pid =getpid (); -printf ("pid is %u\n" , (unsigned ) pid); +normsg ("pid is %u" , (unsigned ) pid);tests_cat_pid (dir_name,"integck_test_dir_" , pid);if (chdir (dir_name) !=-1 ) {/* Remove it if it is already there */
Signature · 1 line
1.7.2.3