Skip to content

Commit 8192de3

Browse files
authored
Merge pull request #54 from AKuHAK/8Mb2
Add 'df' command to display free space
2 parents a025f02 + 249e2f0 commit 8192de3

File tree

2 files changed

+83
-9
lines changed

2 files changed

+83
-9
lines changed

src/iomanX_port.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ extern int _init_hdlfs(int argc, char *argv[]);
8787
#define FIO_S_IXOTH 0x0001 // execute
8888

8989
// File mode checking macros
90-
#define FIO_S_ISLNK(m) (((m)&FIO_S_IFMT) == FIO_S_IFLNK)
91-
#define FIO_S_ISREG(m) (((m)&FIO_S_IFMT) == FIO_S_IFREG)
92-
#define FIO_S_ISDIR(m) (((m)&FIO_S_IFMT) == FIO_S_IFDIR)
90+
#define FIO_S_ISLNK(m) (((m) & FIO_S_IFMT) == FIO_S_IFLNK)
91+
#define FIO_S_ISREG(m) (((m) & FIO_S_IFMT) == FIO_S_IFREG)
92+
#define FIO_S_ISDIR(m) (((m) & FIO_S_IFMT) == FIO_S_IFDIR)
9393

9494
/* File attributes that are retrieved using the getstat and dread calls, and
9595
set using chstat. */
@@ -102,6 +102,10 @@ extern int _init_hdlfs(int argc, char *argv[]);
102102
#define HIOCNSUB 0x6803
103103
#define HIOCFLUSH 0x6804
104104

105+
// DEVCTL commands
106+
#define HDIOC_TOTALSECTOR 0x4802
107+
#define HDIOC_FREESECTOR 0x480A
108+
105109
// Arbitrarily-named commands
106110
#define HIOCTRANSFER 0x6832 // Used by PFS.IRX to read/write data
107111
#define HIOCGETSIZE 0x6833 // For main(0)/subs(1+)
@@ -162,11 +166,13 @@ typedef struct
162166
/*1c*/ unsigned char mtime[8];
163167
/*24*/ unsigned int hisize;
164168
/*28*/ unsigned int private_0; // Number of subs (main) / subpart number (sub)
165-
/*2c*/ unsigned int private_1;
166-
/*30*/ unsigned int private_2;
169+
/*2c*/ unsigned int private_1; // partition size low part
170+
// cppcheck-suppress unusedStructMember
171+
/*30*/ unsigned int private_2; // partition size high part
167172
/*34*/ unsigned int private_3;
168173
/*38*/ unsigned int private_4;
169-
/*3c*/ unsigned int private_5; /* Sector start. */
174+
// cppcheck-suppress unusedStructMember
175+
/*3c*/ unsigned int private_5; // Sector start
170176
} iox_stat_t;
171177

172178
typedef struct

src/shell.c

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "iomanX_port.h"
1010
#include "hl.h"
1111

12+
unsigned int total_sectors = 0;
13+
1214
typedef 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 */
161223
extern 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

Comments
 (0)