Skip to content

Commit 7f24354

Browse files
authored
Merge pull request #6 from fjtrujy/getStat
Add getStat Support
2 parents 5cdd3d3 + 1d26e95 commit 7f24354

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

src/ps2link.c

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,58 @@
580580
return ps2link_response_rmdir(result);
581581
}
582582

583+
int ps2link_request_getstat(void *packet) {
584+
struct { unsigned int number; unsigned short length; char name[256]; } PACKED *request = packet;
585+
struct stat stats; struct tm *loctime;
586+
int ret;
587+
unsigned int mode; unsigned char ctime[8]; unsigned char atime[8]; unsigned char mtime[8];
588+
589+
// Fix the arguments.
590+
fix_pathname(request->name);
591+
592+
// Fetch the entry's statistics.
593+
ret = stat(request->name, &stats);
594+
595+
if (ret == 0) {
596+
// Convert the mode.
597+
mode = (stats.st_mode & 0x07);
598+
if (S_ISDIR(stats.st_mode)) { mode |= 0x20; }
599+
#ifndef _WIN32
600+
if (S_ISLNK(stats.st_mode)) { mode |= 0x08; }
601+
#endif
602+
if (S_ISREG(stats.st_mode)) { mode |= 0x10; }
603+
604+
// Convert the creation time.
605+
loctime = localtime(&(stats.st_ctime));
606+
ctime[6] = (unsigned char)loctime->tm_year;
607+
ctime[5] = (unsigned char)loctime->tm_mon + 1;
608+
ctime[4] = (unsigned char)loctime->tm_mday;
609+
ctime[3] = (unsigned char)loctime->tm_hour;
610+
ctime[2] = (unsigned char)loctime->tm_min;
611+
ctime[1] = (unsigned char)loctime->tm_sec;
612+
613+
// Convert the access time.
614+
loctime = localtime(&(stats.st_atime));
615+
atime[6] = (unsigned char)loctime->tm_year;
616+
atime[5] = (unsigned char)loctime->tm_mon + 1;
617+
atime[4] = (unsigned char)loctime->tm_mday;
618+
atime[3] = (unsigned char)loctime->tm_hour;
619+
atime[2] = (unsigned char)loctime->tm_min;
620+
atime[1] = (unsigned char)loctime->tm_sec;
621+
622+
// Convert the last modified time.
623+
loctime = localtime(&(stats.st_mtime));
624+
mtime[6] = (unsigned char)loctime->tm_year;
625+
mtime[5] = (unsigned char)loctime->tm_mon + 1;
626+
mtime[4] = (unsigned char)loctime->tm_mday;
627+
mtime[3] = (unsigned char)loctime->tm_hour;
628+
mtime[2] = (unsigned char)loctime->tm_min;
629+
mtime[1] = (unsigned char)loctime->tm_sec;
630+
}
631+
632+
return ps2link_response_getstat(ret, mode, 0, stats.st_size, ctime, atime, mtime, 0);
633+
}
634+
583635
////////////////////////////////
584636
// PS2LINK RESPONSE FUNCTIONS //
585637
////////////////////////////////
@@ -737,6 +789,25 @@
737789
return network_send(request_socket, &response, sizeof(response));
738790
}
739791

792+
int ps2link_response_getstat(int result, unsigned int mode, unsigned int attr, unsigned int size, unsigned char *ctime, unsigned char *atime, unsigned char *mtime, unsigned int hisize) {
793+
struct { unsigned int number; unsigned short length; int result; unsigned int mode; unsigned int attr; unsigned int size; unsigned char ctime[8]; unsigned char atime[8]; unsigned char mtime[8]; unsigned int hisize; } PACKED response;
794+
795+
// Build the response packet.
796+
response.number = htonl(PS2LINK_RESPONSE_GETSTAT);
797+
response.length = htons(sizeof(response));
798+
response.result = htonl(result);
799+
response.mode = htonl(mode);
800+
response.attr = htonl(attr);
801+
response.size = htonl(size);
802+
if (ctime) { memcpy(response.ctime, ctime, 8); }
803+
if (atime) { memcpy(response.atime, atime, 8); }
804+
if (mtime) { memcpy(response.mtime, mtime, 8); }
805+
response.hisize = htonl(hisize);
806+
807+
// Send the response packet.
808+
return network_send(request_socket, &response, sizeof(response));
809+
}
810+
740811
//////////////////////////////
741812
// PS2LINK THREAD FUNCTIONS //
742813
//////////////////////////////
@@ -801,7 +872,8 @@
801872
if (ntohl(packet.number) == PS2LINK_REQUEST_READDIR) { ps2link_request_readdir(&packet); } else
802873
if (ntohl(packet.number) == PS2LINK_REQUEST_REMOVE) { ps2link_request_remove(&packet); } else
803874
if (ntohl(packet.number) == PS2LINK_REQUEST_MKDIR) { ps2link_request_mkdir(&packet); } else
804-
if (ntohl(packet.number) == PS2LINK_REQUEST_RMDIR) { ps2link_request_rmdir(&packet); }
875+
if (ntohl(packet.number) == PS2LINK_REQUEST_RMDIR) { ps2link_request_rmdir(&packet); } else
876+
if (ntohl(packet.number) == PS2LINK_REQUEST_GETSTAT) { ps2link_request_getstat(&packet); }
805877

806878
// Reset the timeout counter.
807879
ps2link_counter = 0;

src/ps2link.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
#define PS2LINK_REQUEST_REMOVE 0xBABE0191
7171
#define PS2LINK_REQUEST_MKDIR 0xBABE01A1
7272
#define PS2LINK_REQUEST_RMDIR 0xBABE01B1
73+
#define PS2LINK_REQUEST_GETSTAT 0xBABE01C1
7374

7475
int ps2link_request_open(void *packet);
7576

@@ -108,6 +109,7 @@
108109
#define PS2LINK_RESPONSE_REMOVE 0xBABE0192
109110
#define PS2LINK_RESPONSE_MKDIR 0xBABE01A2
110111
#define PS2LINK_RESPONSE_RMDIR 0xBABE01B2
112+
#define PS2LINK_RESPONSE_GETSTAT 0xBABE01C2
111113

112114
int ps2link_response_open(int result);
113115

0 commit comments

Comments
 (0)