Lines 110-115
Link Here
|
110 |
oslFileError setPos (sal_uInt64 uPos); |
110 |
oslFileError setPos (sal_uInt64 uPos); |
111 |
|
111 |
|
112 |
sal_uInt64 getSize() const; |
112 |
sal_uInt64 getSize() const; |
|
|
113 |
oslFileError setSize (sal_uInt64 uSize); |
113 |
|
114 |
|
114 |
oslFileError readAt ( |
115 |
oslFileError readAt ( |
115 |
off_t nOffset, |
116 |
off_t nOffset, |
Lines 271-276
Link Here
|
271 |
return std::max(m_size, sal::static_int_cast< sal_uInt64 >(bufend)); |
272 |
return std::max(m_size, sal::static_int_cast< sal_uInt64 >(bufend)); |
272 |
} |
273 |
} |
273 |
|
274 |
|
|
|
275 |
oslFileError FileHandle_Impl::setSize (sal_uInt64 uSize) |
276 |
{ |
277 |
off_t const nSize = sal::static_int_cast< off_t >(uSize); |
278 |
if (-1 == ftruncate (m_fd, nSize)) |
279 |
{ |
280 |
/* Failure. Save original result. Try fallback algorithm */ |
281 |
oslFileError result = oslTranslateFileError (OSL_FET_ERROR, errno); |
282 |
|
283 |
/* Check against current size. Fail upon 'shrink' */ |
284 |
if (uSize <= getSize()) |
285 |
{ |
286 |
/* Failure upon 'shrink'. Return original result */ |
287 |
return (result); |
288 |
} |
289 |
|
290 |
/* Save current position */ |
291 |
off_t const nCurPos = (off_t)lseek (m_fd, (off_t)0, SEEK_CUR); |
292 |
if (nCurPos == (off_t)(-1)) |
293 |
return (result); |
294 |
|
295 |
/* Try 'expand' via 'lseek()' and 'write()' */ |
296 |
if (-1 == lseek (m_fd, (off_t)(nSize - 1), SEEK_SET)) |
297 |
return (result); |
298 |
|
299 |
if (-1 == write (m_fd, (char*)"", (size_t)1)) |
300 |
{ |
301 |
/* Failure. Restore saved position */ |
302 |
(void) lseek (m_fd, (off_t)(nCurPos), SEEK_SET); |
303 |
return (result); |
304 |
} |
305 |
|
306 |
/* Success. Restore saved position */ |
307 |
if (-1 == lseek (m_fd, (off_t)nCurPos, SEEK_SET)) |
308 |
return (result); |
309 |
} |
310 |
|
311 |
OSL_FILE_TRACE("osl_setFileSize(%d, %lld) => %ld", m_fd, getSize(), nSize); |
312 |
m_size = sal::static_int_cast< sal_uInt64 >(nSize); |
313 |
return osl_File_E_None; |
314 |
} |
315 |
|
274 |
oslFileError FileHandle_Impl::readAt ( |
316 |
oslFileError FileHandle_Impl::readAt ( |
275 |
off_t nOffset, |
317 |
off_t nOffset, |
276 |
void * pBuffer, |
318 |
void * pBuffer, |
Lines 1282-1343
Link Here
|
1282 |
static sal_uInt64 const g_limit_off_t = std::numeric_limits< off_t >::max(); |
1324 |
static sal_uInt64 const g_limit_off_t = std::numeric_limits< off_t >::max(); |
1283 |
if (g_limit_off_t < uSize) |
1325 |
if (g_limit_off_t < uSize) |
1284 |
return osl_File_E_OVERFLOW; |
1326 |
return osl_File_E_OVERFLOW; |
1285 |
off_t const nSize = sal::static_int_cast< off_t >(uSize); |
|
|
1286 |
|
1327 |
|
1287 |
oslFileError result = pImpl->syncFile(); |
1328 |
oslFileError result = pImpl->syncFile(); |
1288 |
if (result != osl_File_E_None) |
1329 |
if (result != osl_File_E_None) |
1289 |
return (result); |
1330 |
return (result); |
|
|
1331 |
pImpl->m_bufptr = -1, pImpl->m_buflen = 0; |
1290 |
|
1332 |
|
1291 |
if (-1 == ftruncate (pImpl->m_fd, nSize)) |
1333 |
return pImpl->setSize (uSize); |
1292 |
{ |
|
|
1293 |
/* Failure. Try fallback algorithm */ |
1294 |
off_t nCurPos; |
1295 |
|
1296 |
/* Save original result */ |
1297 |
result = oslTranslateFileError (OSL_FET_ERROR, errno); |
1298 |
PERROR("ftruncate", "Try osl_setFileSize [fallback]\n"); |
1299 |
|
1300 |
/* Check against current size. Fail upon 'shrink' */ |
1301 |
if (uSize <= pImpl->getSize()) |
1302 |
{ |
1303 |
/* Failure upon 'shrink'. Return original result */ |
1304 |
return (result); |
1305 |
} |
1306 |
|
1307 |
/* Save current position *//* @@@ pImpl->m_offset @@@ */ |
1308 |
nCurPos = (off_t)lseek (pImpl->m_fd, (off_t)0, SEEK_CUR); |
1309 |
if (nCurPos == (off_t)(-1)) |
1310 |
{ |
1311 |
PERROR("ftruncate: lseek", "Out osl_setFileSize [error]\n"); |
1312 |
return (result); |
1313 |
} |
1314 |
|
1315 |
/* Try 'expand' via 'lseek()' and 'write()' */ |
1316 |
if (lseek (pImpl->m_fd, (off_t)(nSize - 1), SEEK_SET) < 0) |
1317 |
{ |
1318 |
PERROR("ftruncate: lseek", "Out osl_setFileSize [error]\n"); |
1319 |
return (result); |
1320 |
} |
1321 |
if (write (pImpl->m_fd, (char*)"", (size_t)1) < 0) |
1322 |
{ |
1323 |
/* Failure. Restore saved position */ |
1324 |
PERROR("ftruncate: write", "Out osl_setFileSize [error]\n"); |
1325 |
if (lseek (pImpl->m_fd, (off_t)nCurPos, SEEK_SET) < 0) |
1326 |
{ |
1327 |
PERROR("ftruncate: lseek", "ignoring"); |
1328 |
} |
1329 |
return (result); |
1330 |
} |
1331 |
|
1332 |
/* Success. Restore saved position */ |
1333 |
if (lseek (pImpl->m_fd, (off_t)nCurPos, SEEK_SET) < 0) |
1334 |
{ |
1335 |
PERROR("ftruncate: lseek", "Out osl_setFileSize [error]"); |
1336 |
return (result); |
1337 |
} |
1338 |
} |
1339 |
|
1340 |
OSL_FILE_TRACE("osl_setFileSize(%d, %lld) => %ld", pImpl->m_fd, pImpl->getSize(), nSize); |
1341 |
pImpl->m_size = sal::static_int_cast< sal_uInt64 >(nSize); |
1342 |
return osl_File_E_None; |
1343 |
} |
1334 |
} |