Skip to content

Commit cab4e4f

Browse files
committed
VSI: Use CPLParseMemorySize for CPL_VSIL_CURL_CACHE_SIZE and CPL_VSIL_CURL_CHUNK_SIZE
1 parent 73f3f38 commit cab4e4f

File tree

3 files changed

+52
-13
lines changed

3 files changed

+52
-13
lines changed

doc/source/user/configoptions.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,8 @@ Networking options
616616
:since: 2.3
617617

618618
Size of global least-recently-used (LRU) cache shared among all downloaded
619-
content.
619+
content. Value is assumed to represent bytes unless memory units are
620+
specified (since GDAL 3.11).
620621

621622
- .. config:: CPL_VSIL_CURL_USE_HEAD
622623
:choices: YES, NO
@@ -664,6 +665,9 @@ Networking options
664665
:choices: <bytes>
665666
:since: 2.3
666667

668+
Value is assumed to represent bytes unless memory units are
669+
specified (since GDAL 3.11).
670+
667671
- .. config:: GDAL_INGESTED_BYTES_AT_OPEN
668672
:since: 2.3
669673

port/cpl_string.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1848,7 +1848,10 @@ CPLErr CPLParseMemorySize(const char *pszValue, GIntBig *pnValue,
18481848
}
18491849

18501850
*pnValue = static_cast<GIntBig>(value);
1851-
*pbUnitSpecified = (unit != nullptr);
1851+
if (pbUnitSpecified)
1852+
{
1853+
*pbUnitSpecified = (unit != nullptr);
1854+
}
18521855
return CE_None;
18531856
}
18541857

port/cpl_vsil_curl.cpp

Lines changed: 43 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -137,31 +137,63 @@ static void VSICURLReadGlobalEnvVariables()
137137
Initializer()
138138
{
139139
constexpr int DOWNLOAD_CHUNK_SIZE_DEFAULT = 16384;
140+
const char *pszChunkSize =
141+
CPLGetConfigOption("CPL_VSIL_CURL_CHUNK_SIZE", nullptr);
142+
GIntBig nChunkSize = DOWNLOAD_CHUNK_SIZE_DEFAULT;
143+
144+
if (pszChunkSize)
145+
{
146+
if (CPLParseMemorySize(pszChunkSize, &nChunkSize, nullptr) !=
147+
CE_None)
148+
{
149+
CPLError(
150+
CE_Warning, CPLE_AppDefined,
151+
"Could not parse value for CPL_VSIL_CURL_CHUNK_SIZE. "
152+
"Using default value of %d instead.",
153+
DOWNLOAD_CHUNK_SIZE_DEFAULT);
154+
}
155+
}
140156

141-
DOWNLOAD_CHUNK_SIZE_DO_NOT_USE_DIRECTLY = atoi(CPLGetConfigOption(
142-
"CPL_VSIL_CURL_CHUNK_SIZE",
143-
CPLSPrintf("%d", DOWNLOAD_CHUNK_SIZE_DEFAULT)));
144157
constexpr int MIN_CHUNK_SIZE = 1024;
145158
constexpr int MAX_CHUNK_SIZE = 10 * 1024 * 1024;
146-
if (DOWNLOAD_CHUNK_SIZE_DO_NOT_USE_DIRECTLY < MIN_CHUNK_SIZE ||
147-
DOWNLOAD_CHUNK_SIZE_DO_NOT_USE_DIRECTLY > MAX_CHUNK_SIZE)
159+
if (nChunkSize < MIN_CHUNK_SIZE || nChunkSize > MAX_CHUNK_SIZE)
148160
{
149-
DOWNLOAD_CHUNK_SIZE_DO_NOT_USE_DIRECTLY =
150-
DOWNLOAD_CHUNK_SIZE_DEFAULT;
161+
nChunkSize = DOWNLOAD_CHUNK_SIZE_DEFAULT;
151162
CPLError(CE_Warning, CPLE_AppDefined,
152163
"Invalid value for CPL_VSIL_CURL_CHUNK_SIZE. "
153164
"Allowed range is [%d, %d]. "
154165
"Using CPL_VSIL_CURL_CHUNK_SIZE=%d instead",
155166
MIN_CHUNK_SIZE, MAX_CHUNK_SIZE,
156-
DOWNLOAD_CHUNK_SIZE_DO_NOT_USE_DIRECTLY);
167+
DOWNLOAD_CHUNK_SIZE_DEFAULT);
157168
}
169+
DOWNLOAD_CHUNK_SIZE_DO_NOT_USE_DIRECTLY =
170+
static_cast<int>(nChunkSize);
158171

159172
constexpr int N_MAX_REGIONS_DEFAULT = 1000;
160173
constexpr int CACHE_SIZE_DEFAULT =
161174
N_MAX_REGIONS_DEFAULT * DOWNLOAD_CHUNK_SIZE_DEFAULT;
162-
GIntBig nCacheSize = CPLAtoGIntBig(
163-
CPLGetConfigOption("CPL_VSIL_CURL_CACHE_SIZE",
164-
CPLSPrintf("%d", CACHE_SIZE_DEFAULT)));
175+
176+
const char *pszCacheSize =
177+
CPLGetConfigOption("CPL_VSIL_CURL_CACHE_SIZE", nullptr);
178+
GIntBig nCacheSize = CACHE_SIZE_DEFAULT;
179+
180+
if (pszCacheSize)
181+
{
182+
if (CPLParseMemorySize(pszCacheSize, &nCacheSize, nullptr) !=
183+
CE_None)
184+
{
185+
CPLError(
186+
CE_Warning, CPLE_AppDefined,
187+
"Could not parse value for CPL_VSIL_CURL_CACHE_SIZE. "
188+
"Using default value of " CPL_FRMT_GIB " instead.",
189+
nCacheSize);
190+
}
191+
}
192+
else
193+
{
194+
nCacheSize = CACHE_SIZE_DEFAULT;
195+
}
196+
165197
const auto nMaxRAM = CPLGetUsablePhysicalRAM();
166198
const auto nMinVal = DOWNLOAD_CHUNK_SIZE_DO_NOT_USE_DIRECTLY;
167199
auto nMaxVal = static_cast<GIntBig>(INT_MAX) *

0 commit comments

Comments
 (0)