Skip to content

Commit 5616e49

Browse files
Revert "Regularize the various VFS seek implementations (#18607)" (#18624)
This reverts commit bd15d7c.
1 parent 76af05a commit 5616e49

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

libretro-common/include/libretro.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2834,7 +2834,8 @@ typedef int64_t (RETRO_CALLCONV *retro_vfs_tell_t)(struct retro_vfs_file_handle
28342834
* @param stream The file to set the position of.
28352835
* @param offset The new position, in bytes.
28362836
* @param seek_position The position to seek from.
2837-
* @return 0 on success, -1 on failure.
2837+
* @return The new position,
2838+
* or -1 if there was an error.
28382839
* @since VFS API v1
28392840
* @see File Seek Positions
28402841
* @see filestream_seek

libretro-common/include/streams/file_stream.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ RFILE* filestream_open(const char *path, unsigned mode, unsigned hints);
123123
* @param offset The new stream position, in bytes.
124124
* @param seek_position The position to seek from.
125125
* Should be one of the values in \refitem RETRO_VFS_SEEK_POSITION.
126-
* @return 0 if the stream position was set successfully,
126+
* @return The new stream position in bytes relative to the beginning,
127127
* or -1 if there was an error.
128128
* @see RETRO_VFS_SEEK_POSITION
129129
* @see retro_vfs_seek_t

libretro-common/vfs/vfs_implementation.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ int64_t retro_vfs_file_seek_internal(
233233
libretro_vfs_implementation_file *stream,
234234
int64_t offset, int whence)
235235
{
236+
int64_t val;
237+
236238
if (!stream)
237239
return -1;
238240

@@ -252,7 +254,7 @@ int64_t retro_vfs_file_seek_internal(
252254
#elif defined(HAVE_64BIT_OFFSETS)
253255
return fseeko(stream->fp, (off_t)offset, whence);
254256
#else
255-
return fseek(stream->fp, (long)offset, whence) != 0 ? -1 : 0;
257+
return fseek(stream->fp, (long)offset, whence);
256258
#endif
257259
}
258260
#ifdef HAVE_MMAP
@@ -296,7 +298,10 @@ int64_t retro_vfs_file_seek_internal(
296298
}
297299
#endif
298300

299-
return lseek(stream->fd, (off_t)offset, whence) == -1 ? -1 : 0;
301+
if ((val = lseek(stream->fd, (off_t)offset, whence)) < 0)
302+
return -1;
303+
304+
return val;
300305
}
301306

302307
/**
@@ -745,6 +750,8 @@ int64_t retro_vfs_file_truncate_impl(libretro_vfs_implementation_file *stream, i
745750

746751
int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file *stream)
747752
{
753+
int64_t val;
754+
748755
if (!stream)
749756
return -1;
750757

@@ -774,7 +781,10 @@ int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file *stream)
774781
RETRO_VFS_FILE_ACCESS_HINT_FREQUENT_ACCESS)
775782
return stream->mappos;
776783
#endif
777-
return lseek(stream->fd, 0, SEEK_CUR);
784+
if ((val = lseek(stream->fd, 0, SEEK_CUR)) < 0)
785+
return -1;
786+
787+
return val;
778788
}
779789

780790
int64_t retro_vfs_file_seek_impl(libretro_vfs_implementation_file *stream,

libretro-common/vfs/vfs_implementation_smb.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static bool smb_build_path(char *dest, size_t dest_size, const char *relative_pa
249249
settings_t *settings = config_get_ptr();
250250
char temp_path[PATH_MAX_LENGTH];
251251
const char *p;
252-
252+
253253
if (!settings)
254254
{
255255
RARCH_ERR("[SMB] Cannot retrieve settings\n");
@@ -415,8 +415,10 @@ int64_t retro_vfs_file_write_smb(libretro_vfs_implementation_file *stream,
415415
int64_t retro_vfs_file_seek_smb(libretro_vfs_implementation_file *stream,
416416
int64_t offset, int whence)
417417
{
418+
uint64_t newpos = 0;
418419
struct smb2fh *fh;
419420
struct smb2_context *ctx;
421+
int64_t ret;
420422

421423
if (!smb_initialized || !stream || !stream->smb_ctx)
422424
return -1;
@@ -438,13 +440,15 @@ int64_t retro_vfs_file_seek_smb(libretro_vfs_implementation_file *stream,
438440
if (whence != SEEK_SET && whence != SEEK_CUR && whence != SEEK_END)
439441
return -1;
440442

441-
if (smb2_lseek(ctx, fh, offset, whence, NULL) == -EINVAL)
443+
/* libsmb2 returns status via ret, and the new offset via out param */
444+
ret = smb2_lseek(ctx, fh, offset, whence, &newpos);
445+
if (ret < 0)
442446
{
443447
RARCH_ERR("[SMB] Seek error: %s\n", smb2_get_error(ctx));
444448
return -1;
445449
}
446450

447-
return 0;
451+
return (int64_t)newpos;
448452
}
449453

450454
/* return the current byte offset in an open file */
@@ -453,6 +457,7 @@ int64_t retro_vfs_file_tell_smb(libretro_vfs_implementation_file *stream)
453457
uint64_t cur = 0;
454458
struct smb2fh *fh;
455459
struct smb2_context *ctx;
460+
int64_t ret;
456461

457462
if (!smb_initialized || !stream || !stream->smb_ctx)
458463
return -1;
@@ -468,7 +473,8 @@ int64_t retro_vfs_file_tell_smb(libretro_vfs_implementation_file *stream)
468473
if (!ctx)
469474
return -1;
470475

471-
if (smb2_lseek(ctx, fh, 0, SEEK_CUR, &cur) == -EINVAL)
476+
ret = smb2_lseek(ctx, fh, 0, SEEK_CUR, &cur);
477+
if (ret < 0)
472478
{
473479
RARCH_ERR("[SMB] Tell error: %s\n", smb2_get_error(ctx));
474480
return -1;

libretro-common/vfs/vfs_implementation_uwp.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,10 @@ int64_t retro_vfs_file_tell_impl(libretro_vfs_implementation_file* stream)
138138
#endif
139139
return _ftelli64(stream->fp);
140140
}
141-
return lseek(stream->fd, 0, SEEK_CUR);
141+
if (lseek(stream->fd, 0, SEEK_CUR) < 0)
142+
return -1;
143+
144+
return 0;
142145
}
143146

144147
int64_t retro_vfs_file_seek_internal(
@@ -156,7 +159,10 @@ int64_t retro_vfs_file_seek_internal(
156159
#endif
157160
return _fseeki64(stream->fp, offset, whence);
158161
}
159-
return lseek(stream->fd, (off_t)offset, whence) == -1 ? -1 : 0;
162+
if (lseek(stream->fd, (off_t)offset, whence) < 0)
163+
return -1;
164+
165+
return 0;
160166
}
161167

162168
int64_t retro_vfs_file_seek_impl(libretro_vfs_implementation_file* stream,

0 commit comments

Comments
 (0)