View | Details | Raw Unified | Return to issue 105082
Collapse All | Expand All

(-)sal/osl/unx/file.cxx (-53 / +44 lines)
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
}
(-)sal/osl/w32/file.cxx (-13 / +20 lines)
Lines 103-108 Link Here
103
	oslFileError  setPos (sal_uInt64 uPos);
103
	oslFileError  setPos (sal_uInt64 uPos);
104
104
105
	sal_uInt64    getSize() const;
105
	sal_uInt64    getSize() const;
106
	oslFileError  setSize (sal_uInt64 uPos);
106
107
107
	oslFileError readAt (
108
	oslFileError readAt (
108
		LONGLONG     nOffset,
109
		LONGLONG     nOffset,
Lines 253-258 Link Here
253
	return std::max(m_size, sal::static_int_cast< sal_uInt64 >(bufend));
254
	return std::max(m_size, sal::static_int_cast< sal_uInt64 >(bufend));
254
}
255
}
255
256
257
oslFileError FileHandle_Impl::setSize (sal_uInt64 uSize)
258
{
259
	LARGE_INTEGER nDstPos; nDstPos.QuadPart = sal::static_int_cast< LONGLONG >(uSize);
260
	if (!::SetFilePointerEx(m_hFile, nDstPos, 0, FILE_BEGIN))
261
		return oslTranslateFileError( GetLastError() );
262
263
	if (!::SetEndOfFile(m_hFile))
264
		return oslTranslateFileError( GetLastError() );
265
	m_size = uSize;
266
267
	nDstPos.QuadPart = m_offset;
268
	if (!::SetFilePointerEx(m_hFile, nDstPos, 0, FILE_BEGIN))
269
		return oslTranslateFileError( GetLastError() );
270
271
	return osl_File_E_None;
272
}
273
256
oslFileError FileHandle_Impl::readAt (
274
oslFileError FileHandle_Impl::readAt (
257
	LONGLONG     nOffset,
275
	LONGLONG     nOffset,
258
	void *       pBuffer,
276
	void *       pBuffer,
Lines 1049-1068 Link Here
1049
	oslFileError result = pImpl->syncFile();
1067
	oslFileError result = pImpl->syncFile();
1050
	if (result != osl_File_E_None)
1068
	if (result != osl_File_E_None)
1051
		return (result);
1069
		return (result);
1070
    pImpl->m_bufptr = -1, pImpl->m_buflen = 0;
1052
1071
1053
	LARGE_INTEGER nDstPos; nDstPos.QuadPart = sal::static_int_cast< LONGLONG >(uSize);
1072
    return pImpl->setSize (uSize);
1054
	if (!::SetFilePointerEx(pImpl->m_hFile, nDstPos, 0, FILE_BEGIN))
1055
		return oslTranslateFileError( GetLastError() );
1056
1057
	if (!::SetEndOfFile(pImpl->m_hFile))
1058
		return oslTranslateFileError( GetLastError() );
1059
	pImpl->m_size = uSize;
1060
1061
	nDstPos.QuadPart = pImpl->m_offset;
1062
	if (!::SetFilePointerEx(pImpl->m_hFile, nDstPos, 0, FILE_BEGIN))
1063
		return oslTranslateFileError( GetLastError() );
1064
1065
	return osl_File_E_None;
1066
}
1073
}
1067
1074
1068
//##################################################################
1075
//##################################################################

Return to issue 105082