99#include "iomanX_port.h"
1010#include "hl.h"
1111
12+ unsigned int total_sectors = 0 ;
13+
1214typedef struct
1315{
1416 int setup ;
@@ -156,6 +158,66 @@ static int do_lcd(context_t *ctx, int argc, char *argv[])
156158 /* chdir would set errno on error */
157159 return (chdir (argv [1 ]));
158160}
161+ static int do_df (context_t * ctx , int argc , char * argv [])
162+ {
163+ char tmp [256 ];
164+ if (!ctx -> mount ) { /* not mounted */
165+ strcpy (tmp , "hdd0:" );
166+ unsigned int used_sectors = 0 ;
167+ int result ;
168+ iox_dirent_t dirent ;
169+ int dh = iomanX_dopen (tmp );
170+ if (dh < 0 ) {
171+ fprintf (stderr , "(!) Unable to open %s: %s.\n" , tmp , strerror (- dh ));
172+ return (dh );
173+ }
174+ while ((result = iomanX_dread (dh , & dirent )) && result != -1 ) {
175+ if (dirent .stat .mode != 0x0000 && dirent .stat .attr == 0 ) {
176+ used_sectors += dirent .stat .private_1 ;
177+ }
178+ }
179+ iomanX_close (dh );
180+ unsigned int free_sectors = total_sectors + 1 - used_sectors ;
181+
182+ printf ("Device Size Used Avail Capacity\n" );
183+ printf ("%s %4uGiB %4uGiB %4uGiB %3u%%\n" ,
184+ tmp ,
185+ (total_sectors + 1 ) / (2048 * 1024 ),
186+ used_sectors / (2048 * 1024 ),
187+ free_sectors / (2048 * 1024 ),
188+ (used_sectors * 100 ) / (total_sectors + 1 ));
189+ } else { /* mounted ctx->mount*/
190+ iox_stat_t stat ;
191+ int dh = iomanX_getstat (ctx -> mount_point , & stat );
192+ if (dh < 0 ) {
193+ fprintf (stderr , "(!) Unable to get stats for %s: %s.\n" , ctx -> mount_point , strerror (- dh ));
194+ return (dh );
195+ }
196+ unsigned int total_pfs_sectors = stat .private_1 - stat .private_0 * 4 - 4 * 1024 * 2 ; /* 4 MiB for main header, 2 KiB for each subpartition */
197+
198+ strcpy (tmp , "pfs0:" );
199+ unsigned int frsize = iomanX_devctl (tmp , PDIOC_ZONESZ , NULL , 0 , NULL , 0 );
200+ unsigned int bavail = iomanX_devctl (tmp , PDIOC_ZONEFREE , NULL , 0 , NULL , 0 );
201+ unsigned int free_sectors = frsize * bavail / 512 ; /* 512 bytes per sector */
202+ printf ("Device Size Used Avail Capacity\n" );
203+ if (total_pfs_sectors / 2048 < 10000 ) {
204+ printf ("%32s %4uMiB %4uMiB %4uMiB %3u%%\n" ,
205+ ctx -> mount_point + 5 ,
206+ total_pfs_sectors / 2048 ,
207+ (total_pfs_sectors - free_sectors ) / 2048 ,
208+ free_sectors / 2048 ,
209+ ((total_pfs_sectors - free_sectors ) * 100 ) / total_pfs_sectors );
210+ } else {
211+ printf ("%32s %4uGiB %4uGiB %4uGiB %3u%%\n" ,
212+ ctx -> mount_point + 5 ,
213+ total_pfs_sectors / (2048 * 1024 ),
214+ (total_pfs_sectors - free_sectors ) / (2048 * 1024 ),
215+ free_sectors / (2048 * 1024 ),
216+ ((total_pfs_sectors - free_sectors ) * 100 ) / total_pfs_sectors );
217+ }
218+ }
219+ return (0 );
220+ }
159221
160222/* where (image of) PS2 HDD is; in fake_sdk/atad.c */
161223extern void set_atad_device_path (const char * path );
@@ -201,6 +263,8 @@ static int do_device(context_t *ctx, int argc, char *argv[])
201263 exit (1 );
202264 }
203265 ctx -> setup = 1 ;
266+ total_sectors = iomanX_devctl ("hdd0:" , HDIOC_TOTALSECTOR , NULL , 0 , NULL , 0 );
267+ do_df (ctx , 0 , NULL ); /* display free space on partition */
204268 return (0 );
205269}
206270
@@ -219,6 +283,8 @@ static int do_initialize(context_t *ctx, int argc, char *argv[])
219283 }
220284 if (result < 0 )
221285 fprintf (stderr , "(!) format: %s.\n" , strerror (- result ));
286+ total_sectors = iomanX_devctl ("hdd0:" , HDIOC_TOTALSECTOR , NULL , 0 , NULL , 0 );
287+ do_df (ctx , 0 , NULL ); /* display free space on partition */
222288 return (result );
223289 }
224290}
@@ -634,16 +700,17 @@ static int do_help(context_t *ctx, int argc, char *argv[])
634700 "ls [-l] - no mount: list partitions; mount: list files/dirs; -l: verbose list;\n"
635701 "rename <curr_name> <new_name> - no mount: rename partition; mount: rename a file/dir.\n"
636702 "mkdir <dir_name> - create a new directory;\n"
637- "rmdir <dir_name> - delete an existing directory;\n"
703+ "rmdir <dir_name> - delete an existing empty directory;\n"
638704 "pwd - print current PS2 HDD directory;\n"
639705 "cd <dir_name> - change directory;\n"
640706 "get <file_name> - copy file from PS2 HDD to current dir;\n"
641707 "put <file_name> - copy file from current dir to PS2 HDD;\n"
642708 "\tfile name must not contain a path;\n"
643709 "rm <file_name> - delete a file;\n"
644- "rename <curr_name> <new_name> - rename a file/dir.\n"
710+ "rename <curr_name> <new_name> - rename a file/dir/partition .\n"
645711 "rmpart <part_name> - remove partition (destructive).\n"
646- "exit - exits the program. (Do this before you unplug your HDD)\n" ,
712+ "df - no mount: display free space on the whole HDD; mount: display free space on partition.\n"
713+ "exit/quit/bye - exits the program. (Do this before you unplug your HDD)\n" ,
647714 stderr );
648715 return (0 );
649716}
@@ -680,6 +747,7 @@ static int exec(void *data, int argc, char *argv[])
680747 {"put" , 1 , need_device + need_mount , & do_put },
681748 {"rm" , 1 , need_device + need_mount , & do_rm },
682749 {"rmpart" , 1 , need_device , & do_rmpart },
750+ {"df" , 0 , need_device , & do_df },
683751 {"rename" , 2 , need_device , & do_rename },
684752 {"help" , 0 , no_req , & do_help },
685753 };
0 commit comments