Skip to content

Commit eba0630

Browse files
committed
Fixed the directory in the enumeration callback for Steam storage
This is guaranteed to have a path separator at the end
1 parent 1978506 commit eba0630

File tree

1 file changed

+37
-17
lines changed

1 file changed

+37
-17
lines changed

src/storage/steam/SDL_steamstorage.c

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,32 @@ static bool STEAM_StorageReady(void *userdata)
8080
return true;
8181
}
8282

83+
static char *GetNormalizedStoragePath(const char *path, bool add_separator)
84+
{
85+
if (SDL_strcmp(path, ".") == 0) {
86+
path = "";
87+
} else {
88+
while (*path == '/') {
89+
++path;
90+
}
91+
}
92+
93+
size_t pathlen = SDL_strlen(path);
94+
while (pathlen > 0 && path[pathlen - 1] == '/') {
95+
--pathlen;
96+
}
97+
98+
char *normalized = (char *)SDL_malloc(pathlen + add_separator + 1);
99+
if (normalized) {
100+
SDL_memcpy(normalized, path, pathlen);
101+
if (add_separator) {
102+
normalized[pathlen++] = '/';
103+
}
104+
normalized[pathlen] = '\0';
105+
}
106+
return normalized;
107+
}
108+
83109
static bool STEAM_EnumerateStorageDirectory(void *userdata, const char *path, SDL_EnumerateDirectoryCallback callback, void *callback_userdata)
84110
{
85111
bool result = true;
@@ -89,19 +115,11 @@ static bool STEAM_EnumerateStorageDirectory(void *userdata, const char *path, SD
89115
return SDL_SetError("SteamRemoteStorage unavailable");
90116
}
91117

92-
const char *prefix;
93-
if (SDL_strcmp(path, ".") == 0) {
94-
prefix = "";
95-
} else {
96-
prefix = path;
97-
while (*prefix == '/') {
98-
++prefix;
99-
}
100-
}
101-
size_t prefixlen = SDL_strlen(prefix);
102-
while (prefixlen > 0 && prefix[prefixlen - 1] == '/') {
103-
--prefixlen;
118+
char *dirname = GetNormalizedStoragePath(path, true);
119+
if (!dirname) {
120+
return false;
104121
}
122+
size_t dirlen = SDL_strlen(dirname);
105123

106124
bool done = false;
107125
Sint32 count = steam->SteamAPI_ISteamRemoteStorage_GetFileCount(steamremotestorage);
@@ -112,12 +130,12 @@ static bool STEAM_EnumerateStorageDirectory(void *userdata, const char *path, SD
112130
}
113131

114132
const char *fname;
115-
if (prefixlen > 0) {
116-
// Make sure the prefix matches
117-
if (SDL_strncmp(prefix, file, prefixlen) != 0 || *(file + prefixlen) != '/') {
133+
if (dirlen > 1) {
134+
// Make sure the directory matches
135+
if (SDL_strncmp(dirname, file, dirlen) != 0) {
118136
continue;
119137
}
120-
fname = file + prefixlen + 1;
138+
fname = file + dirlen;
121139
} else {
122140
// Make sure this is a top-level file
123141
if (SDL_strchr(file, '/') != NULL) {
@@ -126,7 +144,7 @@ static bool STEAM_EnumerateStorageDirectory(void *userdata, const char *path, SD
126144
fname = file;
127145
}
128146

129-
switch (callback(callback_userdata, path, fname)) {
147+
switch (callback(callback_userdata, dirname, fname)) {
130148
case SDL_ENUM_SUCCESS:
131149
done = true;
132150
break;
@@ -138,6 +156,8 @@ static bool STEAM_EnumerateStorageDirectory(void *userdata, const char *path, SD
138156
break;
139157
}
140158
}
159+
SDL_free(dirname);
160+
141161
return result;
142162
}
143163

0 commit comments

Comments
 (0)