Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/utility.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,14 @@

int fix_flags(int flags) { int result = 0;

// Fix the flags.
if (flags & 0x0001) { result |= O_RDONLY; }
if (flags & 0x0002) { result |= O_WRONLY | O_TRUNC; } // FIXME: Truncate is needed for some programs.
// Fix the flags: the access mode is the low two bits (1 read, 2 write,
// 3 both). Read and write is O_RDWR, not O_RDONLY | O_WRONLY (which is
// O_WRONLY on most hosts, so reads fail).
switch (flags & 0x0003) {
case 0x0001: result |= O_RDONLY; break;
case 0x0002: result |= O_WRONLY | O_TRUNC; break; // FIXME: Truncate is needed for some programs.
case 0x0003: result |= O_RDWR; break;
}
#ifndef _WIN32
if (flags & 0x0010) { result |= O_NONBLOCK; }
#endif
Expand Down
15 changes: 9 additions & 6 deletions tests/fake_ps2link.c
Original file line number Diff line number Diff line change
Expand Up @@ -129,12 +129,12 @@ static int fake_write(int fd, const unsigned char *data, int length)
pthread_mutex_unlock(&fake.lock);
return result;
}
static unsigned char *load_file(const char *path, size_t *length)
static unsigned char *load_file(const char *path, int flags, size_t *length)
{
unsigned char *data = NULL;
unsigned char chunk[128 * 1024];
size_t allocated = 0;
int fd = fake_open(path, 1), n;
int fd = fake_open(path, flags), n;
*length = 0;
if (fd < 0) return NULL;
while ((n = fake_read(fd, chunk, sizeof(chunk))) > 0) {
Expand Down Expand Up @@ -217,12 +217,15 @@ static void *run_script(void *context)
}
pthread_mutex_unlock(&fake.lock);
break;
} else if (!strcmp(line, "readback")) {
} else if (!strcmp(line, "readback") || !strcmp(line, "rdwrback")) {
/* rdwrback: the same, the file opened for reading and writing
* (PS2 flags 3) */
int rdwr = !strcmp(line, "rdwrback");
size_t length = 0;
unsigned char *data = load_file(arg, &length);
unsigned char *data = load_file(arg, rdwr ? 3 : 1, &length);
if (data) {
char report[128];
snprintf(report, sizeof(report), "readback %zu %08x",
snprintf(report, sizeof(report), "%s %zu %08x", line,
length, crc32_bytes(data, length));
line_out(report);
free(data);
Expand Down Expand Up @@ -261,7 +264,7 @@ static void command_packet(unsigned char *packet, size_t length)
args[255] = '\0';
first = args;
path = strncmp(first, "host:", 5) == 0 ? first + 5 : first;
script = load_file(path, &size);
script = load_file(path, 1, &size);
if (!script) { line_out("fake: could not load script"); return; }
offset = strnlen(first, sizeof(args)) + 1;
for (i = 1; i < count && offset < 256; i++) {
Expand Down
6 changes: 6 additions & 0 deletions tests/run-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ printf 'readback blob.bin\nprint @@END@@ status=0\n' > readback.elf
check "big host: read" 0 "${H[@]}" -t 10 -e @@END@@ execee host:readback.elf
expect "readback size and crc" grep -q "^readback 300000 $("$HERE/../bin/crc32" blob.bin)$" out.txt

# --- a host: file opened for reading and writing (PS2 flags 3) can be read,
# and is not truncated
printf 'rdwrback blob.bin\nprint @@END@@ status=0\n' > rdwr.elf
check "host: read of a read-write file" 0 "${H[@]}" -t 10 -e @@END@@ execee host:rdwr.elf
expect "read-write: size and crc" grep -q "^rdwrback 300000 $("$HERE/../bin/crc32" blob.bin)$" out.txt

# --- second client
"$CLIENT" "${H[@]}" -t 5 listen > /dev/null 2>&1 &
LISTENER=$!
Expand Down
Loading