mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:32:46 +00:00 
			
		
		
		
	Kernel+Userland: Convert /proc/df to JSON.
This commit is contained in:
		
							parent
							
								
									aaedc24f15
								
							
						
					
					
						commit
						54d7670fc3
					
				
					 2 changed files with 30 additions and 35 deletions
				
			
		|  | @ -450,18 +450,19 @@ ByteBuffer procfs$mounts(InodeIdentifier) | |||
| ByteBuffer procfs$df(InodeIdentifier) | ||||
| { | ||||
|     // FIXME: This is obviously racy against the VFS mounts changing.
 | ||||
|     StringBuilder builder; | ||||
|     VFS::the().for_each_mount([&builder](auto& mount) { | ||||
|     JsonArray json; | ||||
|     VFS::the().for_each_mount([&json](auto& mount) { | ||||
|         auto& fs = mount.guest_fs(); | ||||
|         builder.appendf("%s,", fs.class_name()); | ||||
|         builder.appendf("%u,", fs.total_block_count()); | ||||
|         builder.appendf("%u,", fs.free_block_count()); | ||||
|         builder.appendf("%u,", fs.total_inode_count()); | ||||
|         builder.appendf("%u,", fs.free_inode_count()); | ||||
|         builder.append(mount.absolute_path()); | ||||
|         builder.append('\n'); | ||||
|         JsonObject fs_object; | ||||
|         fs_object.set("class_name", fs.class_name()); | ||||
|         fs_object.set("total_block_count", fs.total_block_count()); | ||||
|         fs_object.set("free_block_count", fs.free_block_count()); | ||||
|         fs_object.set("total_inode_count", fs.total_inode_count()); | ||||
|         fs_object.set("free_inode_count", fs.free_inode_count()); | ||||
|         fs_object.set("mount_point", mount.absolute_path()); | ||||
|         json.append(fs_object); | ||||
|     }); | ||||
|     return builder.to_byte_buffer(); | ||||
|     return json.serialized().to_byte_buffer(); | ||||
| } | ||||
| 
 | ||||
| ByteBuffer procfs$cpuinfo(InodeIdentifier) | ||||
|  |  | |||
|  | @ -1,5 +1,8 @@ | |||
| #include <AK/AKString.h> | ||||
| #include <AK/JsonArray.h> | ||||
| #include <AK/JsonObject.h> | ||||
| #include <AK/Vector.h> | ||||
| #include <LibCore/CFile.h> | ||||
| #include <fcntl.h> | ||||
| #include <stdio.h> | ||||
| #include <stdlib.h> | ||||
|  | @ -16,31 +19,23 @@ struct FileSystem { | |||
| 
 | ||||
| int main(int, char**) | ||||
| { | ||||
|     FILE* fp = fopen("/proc/df", "r"); | ||||
|     if (!fp) { | ||||
|         perror("failed to open /proc/df"); | ||||
|     CFile file("/proc/df"); | ||||
|     if (!file.open(CIODevice::ReadOnly)) { | ||||
|         fprintf(stderr, "Failed to open /proc/df: %s\n", file.error_string()); | ||||
|         return 1; | ||||
|     } | ||||
|     printf("Filesystem    Blocks        Used    Available   Mount point\n"); | ||||
|     for (;;) { | ||||
|         char buf[4096]; | ||||
|         char* ptr = fgets(buf, sizeof(buf), fp); | ||||
|         if (!ptr) | ||||
|             break; | ||||
|         auto parts = String(buf, Chomp).split(','); | ||||
|         if (parts.size() < 6) | ||||
|             break; | ||||
|         bool ok; | ||||
|         String fs = parts[0]; | ||||
|         unsigned total_block_count = parts[1].to_uint(ok); | ||||
|         ASSERT(ok); | ||||
|         unsigned free_block_count = parts[2].to_uint(ok); | ||||
|         ASSERT(ok); | ||||
|         unsigned total_inode_count = parts[3].to_uint(ok); | ||||
|         ASSERT(ok); | ||||
|         unsigned free_inode_count = parts[4].to_uint(ok); | ||||
|         ASSERT(ok); | ||||
|         String mount_point = parts[5]; | ||||
| 
 | ||||
|     auto file_contents = file.read_all(); | ||||
|     auto json = JsonValue::from_string(file_contents).as_array(); | ||||
|     json.for_each([](auto& value) { | ||||
|         auto fs_object = value.as_object(); | ||||
|         auto fs = fs_object.get("class_name").to_string(); | ||||
|         auto total_block_count = fs_object.get("total_block_count").to_dword(); | ||||
|         auto free_block_count = fs_object.get("free_block_count").to_dword(); | ||||
|         auto total_inode_count = fs_object.get("total_inode_count").to_dword(); | ||||
|         auto free_inode_count = fs_object.get("free_inode_count").to_dword(); | ||||
|         auto mount_point = fs_object.get("mount_point").to_string(); | ||||
| 
 | ||||
|         (void)total_inode_count; | ||||
|         (void)free_inode_count; | ||||
|  | @ -51,8 +46,7 @@ int main(int, char**) | |||
|         printf("%10u   ", free_block_count); | ||||
|         printf("%s", mount_point.characters()); | ||||
|         printf("\n"); | ||||
|     } | ||||
|     int rc = fclose(fp); | ||||
|     ASSERT(rc == 0); | ||||
|     }); | ||||
| 
 | ||||
|     return 0; | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Andreas Kling
						Andreas Kling