-
-/* Prints a list of files in the filesystem to the system
- console.
- Returns true if successful, false on failure,
- which occurs only if an internal memory allocation fails. */
-bool
-filesys_list (void)
-{
- struct dir *dir = dir_create (NUM_DIR_ENTRIES);
- if (dir == NULL)
- return false;
- dir_read (dir, root_dir_file);
- dir_list (dir);
- dir_destroy (dir);
-
- return true;
-}
-
-/* Dumps the filesystem state to the system console,
- including the free map, the list of files, and file contents.
- Returns true if successful, false on failure,
- which occurs only if an internal memory allocation fails. */
-bool
-filesys_dump (void)
-{
- struct bitmap *free_map;
- struct dir *dir;
-
- printf ("Free map:\n");
- free_map = bitmap_create (disk_size (filesys_disk));
- if (free_map == NULL)
- return false;
- bitmap_read (free_map, free_map_file);
- bitmap_dump (free_map);
- bitmap_destroy (free_map);
- printf ("\n");
-
- dir = dir_create (NUM_DIR_ENTRIES);
- if (dir == NULL)
- return false;
- dir_read (dir, root_dir_file);
- dir_dump (dir);
- dir_destroy (dir);
-
- return true;
-}
-
-static void must_succeed_function (int, bool) NO_INLINE;
-#define MUST_SUCCEED(EXPR) must_succeed_function (__LINE__, EXPR)
-
-/* Performs basic sanity checks on the filesystem.
- The filesystem should not contain a file named `foo' when
- called. */
-void
-filesys_self_test (void)
-{
- static const char s[] = "This is a test string.";
- static const char zeros[sizeof s] = {0};
- struct file *file;
- char s2[sizeof s];
- int i;
-
- filesys_remove ("foo");
- for (i = 0; i < 2; i++)
- {
- /* Create file and check that it contains zeros
- throughout the created length. */
- MUST_SUCCEED (filesys_create ("foo", sizeof s));
- MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
- MUST_SUCCEED (file_read (file, s2, sizeof s2) == sizeof s2);
- MUST_SUCCEED (memcmp (s2, zeros, sizeof s) == 0);
- MUST_SUCCEED (file_tell (file) == sizeof s);
- MUST_SUCCEED (file_length (file) == sizeof s);
- file_close (file);
-
- /* Reopen file and write to it. */
- MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
- MUST_SUCCEED (file_write (file, s, sizeof s) == sizeof s);
- MUST_SUCCEED (file_tell (file) == sizeof s);
- MUST_SUCCEED (file_length (file) == sizeof s);
- file_close (file);
-
- /* Reopen file and verify that it reads back correctly.
- Delete file while open to check proper semantics. */
- MUST_SUCCEED ((file = filesys_open ("foo")) != NULL);
- MUST_SUCCEED (filesys_remove ("foo"));
- MUST_SUCCEED (file_read (file, s2, sizeof s) == sizeof s);
- MUST_SUCCEED (memcmp (s, s2, sizeof s) == 0);
- MUST_SUCCEED (file_tell (file) == sizeof s);
- MUST_SUCCEED (file_length (file) == sizeof s);
- file_close (file);
-
- /* Make sure file is deleted. */
- MUST_SUCCEED ((file = filesys_open ("foo")) == NULL);
- }
-
- printf ("filesys: self test ok\n");
-}