summaryrefslogtreecommitdiff
path: root/source/XMPFiles_IO.cpp
blob: 6de86668f3c0e2d68667c282b6ce3e7b36e89308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2010 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE:  Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================

#include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.

#include "public/include/XMP_Const.h"
#include "public/include/XMP_IO.hpp"

#include "source/XMP_LibUtils.hpp"
#include "source/XMPFiles_IO.hpp"
#include "source/XIO.hpp"


#define EMPTY_FILE_PATH ""
#define XMP_FILESIO_STATIC_START try { int a;
#define XMP_FILESIO_STATIC_END1(errorCallbackPtr, filePath, severity)											\
		a = 1;																									\
	} catch ( XMP_Error & error ) {																				\
		if ( (errorCallbackPtr) != NULL ) (errorCallbackPtr)->NotifyClient ( (severity), error, (filePath) );	\
		else throw;																								\
	}
#define XMP_FILESIO_START try { int b;
#define XMP_FILESIO_END1(severity)																				\
		b = 1;																									\
	} catch ( XMP_Error & error ) {																				\
		if ( errorCallback != NULL ) errorCallback->NotifyClient ( (severity), error, filePath.c_str() );		\
		else throw;																								\
	}
#define XMP_FILESIO_END2(filePath, severity)																	\
		b = 1;																									\
	} catch ( XMP_Error & error ) {																				\
		if ( errorCallback != NULL ) errorCallback->NotifyClient ( (severity), error, (filePath) );				\
		else throw;																								\
	}
#define XMP_FILESIO_STATIC_NOTIFY_ERROR(errorCallbackPtr, filePath, severity, error)							\
	if ( (errorCallbackPtr) != NULL ) errorCallbackPtr->NotifyClient ( (severity), (error), (filePath) );
#define XMP_FILESIO_NOTIFY_ERROR(filePath, severity, error)														\
	XMP_FILESIO_STATIC_NOTIFY_ERROR(errorCallback, (filePath), (severity), (error))


// =================================================================================================
// XMPFiles_IO::New_XMPFiles_IO
// ============================

/* class static */
XMPFiles_IO * XMPFiles_IO::New_XMPFiles_IO (
	const char * filePath,
	bool readOnly,
	GenericErrorCallback * _errorCallback,
	XMP_ProgressTracker * _progressTracker )
{
	XMP_FILESIO_STATIC_START
	Host_IO::FileRef hostFile = Host_IO::noFileRef;
	
	switch ( Host_IO::GetFileMode ( filePath ) ) {
		case Host_IO::kFMode_IsFile:
			hostFile = Host_IO::Open ( filePath, readOnly );
			break;
		case Host_IO::kFMode_DoesNotExist:
			break;
		default:
			XMP_Throw ( "New_XMPFiles_IO, path must be a file or not exist", kXMPErr_FilePathNotAFile );
	}
	if ( hostFile == Host_IO::noFileRef ) {
		XMP_Error error (kXMPErr_NoFile, "New_XMPFiles_IO, file does not exist");
		XMP_FILESIO_STATIC_NOTIFY_ERROR ( _errorCallback, filePath, kXMPErrSev_Recoverable, error );
		return 0;
	}

	Host_IO::Rewind ( hostFile );	// Make sure offset really is 0.

	XMPFiles_IO * newFile = new XMPFiles_IO ( hostFile, filePath, readOnly, _errorCallback, _progressTracker );
	return newFile;
	XMP_FILESIO_STATIC_END1 ( _errorCallback, filePath, kXMPErrSev_FileFatal )
	return NULL;

}	// XMPFiles_IO::New_XMPFiles_IO

// =================================================================================================
// XMPFiles_IO::XMPFiles_IO
// ========================

XMPFiles_IO::XMPFiles_IO (
	Host_IO::FileRef hostFile,
	const char * _filePath,
	bool _readOnly,
	GenericErrorCallback * _errorCallback,
	XMP_ProgressTracker * _progressTracker )
	: readOnly(_readOnly)
	, filePath(_filePath)
	, fileRef(hostFile)
	, currOffset(0)
	, isTemp(false)
	, derivedTemp(0)
	, errorCallback(_errorCallback)
	, progressTracker(_progressTracker)
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );

	this->currLength = Host_IO::Length ( this->fileRef );
	XMP_FILESIO_END2 ( _filePath, kXMPErrSev_FileFatal )
}	// XMPFiles_IO::XMPFiles_IO

// =================================================================================================
// XMPFiles_IO::~XMPFiles_IO
// =========================

XMPFiles_IO::~XMPFiles_IO()
{
	try {
		XMP_FILESIO_START
		if ( this->derivedTemp != 0 ) this->DeleteTemp();
		if ( this->fileRef != Host_IO::noFileRef ) Host_IO::Close ( this->fileRef );
		if ( this->isTemp && (! this->filePath.empty()) ) Host_IO::Delete ( this->filePath.c_str() );
		XMP_FILESIO_END1 ( kXMPErrSev_Recoverable )
	} catch ( ... ) {
		// All of the above is fail-safe cleanup, ignore problems.
	}
}	// XMPFiles_IO::~XMPFiles_IO

// =================================================================================================
// XMPFiles_IO::operator=
// ======================

void XMPFiles_IO::operator = ( const XMP_IO& in )
{
	XMP_FILESIO_START
	XMP_Throw ( "No assignment for XMPFiles_IO", kXMPErr_InternalFailure );
	XMP_FILESIO_END1 ( kXMPErrSev_OperationFatal )

};	// XMPFiles_IO::operator=

// =================================================================================================
// XMPFiles_IO::Read
// =================

XMP_Uns32 XMPFiles_IO::Read ( void * buffer, XMP_Uns32 count, bool readAll /* = false */ )
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );
	XMP_Assert ( this->currOffset == Host_IO::Offset ( this->fileRef ) );
	XMP_Assert ( this->currLength == Host_IO::Length ( this->fileRef ) );
	XMP_Assert ( this->currOffset <= this->currLength );

	if ( count > (this->currLength - this->currOffset) ) {
		if ( readAll ) XMP_Throw ( "XMPFiles_IO::Read, not enough data", kXMPErr_EnforceFailure );
		count = (XMP_Uns32) (this->currLength - this->currOffset);
	}

	XMP_Uns32 amountRead = Host_IO::Read ( this->fileRef, buffer, count );
	XMP_Enforce ( amountRead == count );

	this->currOffset += amountRead;
	return amountRead;
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal )
	return 0;

}	// XMPFiles_IO::Read

// =================================================================================================
// XMPFiles_IO::Write
// ==================

void XMPFiles_IO::Write ( const void * buffer, XMP_Uns32 count )
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );
	XMP_Assert ( this->currOffset == Host_IO::Offset ( this->fileRef ) );
	XMP_Assert ( this->currLength == Host_IO::Length ( this->fileRef ) );
	XMP_Assert ( this->currOffset <= this->currLength );

	try {
		if ( this->readOnly )
			XMP_Throw ( "New_XMPFiles_IO, write not permitted on read only file", kXMPErr_FilePermission );
		Host_IO::Write ( this->fileRef, buffer, count );
		if ( this->progressTracker != 0 ) this->progressTracker->AddWorkDone ( (float) count );
	} catch ( ... ) {
		try {
			// we should try to maintain the state as best as possible
			// but no exception should escape from this backup plan.
			// Make sure the internal state reflects partial writes.
			this->currOffset = Host_IO::Offset ( this->fileRef );
			this->currLength = Host_IO::Length ( this->fileRef );
		} catch ( ... ) {
			// don't do anything
		}
		throw;
	}

	this->currOffset += count;
	if ( this->currOffset > this->currLength ) this->currLength = this->currOffset;
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal )

}	// XMPFiles_IO::Write

// =================================================================================================
// XMPFiles_IO::Seek
// =================

XMP_Int64 XMPFiles_IO::Seek ( XMP_Int64 offset, SeekMode mode )
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );
	XMP_Assert ( this->currOffset == Host_IO::Offset ( this->fileRef ) );
	XMP_Assert ( this->currLength == Host_IO::Length ( this->fileRef ) );

	XMP_Int64 newOffset = offset;
	if ( mode == kXMP_SeekFromCurrent ) {
		newOffset += this->currOffset;
	} else if ( mode == kXMP_SeekFromEnd ) {
		newOffset += this->currLength;
	}
	XMP_Enforce ( newOffset >= 0 );

	if ( newOffset <= this->currLength ) {
		this->currOffset = Host_IO::Seek ( this->fileRef, offset, mode );
	} else if ( this->readOnly ) {
		XMP_Throw ( "XMPFiles_IO::Seek, read-only seek beyond EOF", kXMPErr_EnforceFailure );
	} else {
		Host_IO::SetEOF ( this->fileRef, newOffset );	// Extend a file open for writing.
		this->currLength = newOffset;
		this->currOffset = Host_IO::Seek ( this->fileRef, 0, kXMP_SeekFromEnd );
	}

	XMP_Assert ( this->currOffset == newOffset );
	return this->currOffset;
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal );
	return -1;

}	// XMPFiles_IO::Seek

// =================================================================================================
// XMPFiles_IO::Length
// ===================

XMP_Int64 XMPFiles_IO::Length()
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );
	XMP_Assert ( this->currOffset == Host_IO::Offset ( this->fileRef ) );
	XMP_Assert ( this->currLength == Host_IO::Length ( this->fileRef ) );
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal )
	return this->currLength;

}	// XMPFiles_IO::Length

// =================================================================================================
// XMPFiles_IO::Truncate
// =====================

void XMPFiles_IO::Truncate ( XMP_Int64 length )
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );
	XMP_Assert ( this->currOffset == Host_IO::Offset ( this->fileRef ) );
	XMP_Assert ( this->currLength == Host_IO::Length ( this->fileRef ) );

	if ( this->readOnly )
		XMP_Throw ( "New_XMPFiles_IO, truncate not permitted on read only file", kXMPErr_FilePermission );

	XMP_Enforce ( length <= this->currLength );
	Host_IO::SetEOF ( this->fileRef, length );

	this->currLength = length;
	if ( this->currOffset > this->currLength ) this->currOffset = this->currLength;

	// ! Seek to the expected offset, some versions of Host_IO::SetEOF implicitly seek to EOF.
	Host_IO::Seek ( this->fileRef, this->currOffset, kXMP_SeekFromStart );
	XMP_Assert ( this->currOffset == Host_IO::Offset ( this->fileRef ) );
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal )

}	// XMPFiles_IO::Truncate

// =================================================================================================
// XMPFiles_IO::DeriveTemp
// =======================

XMP_IO* XMPFiles_IO::DeriveTemp()
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );

	if ( this->derivedTemp != 0 ) return this->derivedTemp;

	if ( this->readOnly ) {
		XMP_Throw ( "XMPFiles_IO::DeriveTemp, can't derive from read-only", kXMPErr_InternalFailure );
	}
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal )

	std::string tempPath;

	XMP_FILESIO_START
	tempPath = Host_IO::CreateTemp ( this->filePath.c_str() );

	XMPFiles_IO* newTemp = XMPFiles_IO::New_XMPFiles_IO ( tempPath.c_str(), Host_IO::openReadWrite );
	if ( newTemp == 0 ) {
		Host_IO::Delete ( tempPath.c_str() );
		XMP_Throw ( "XMPFiles_IO::DeriveTemp, can't open temp file", kXMPErr_InternalFailure );
	}

	newTemp->isTemp = true;
	this->derivedTemp = newTemp;
	newTemp->progressTracker = this->progressTracker;	// Automatically track writes to the temp file.
	XMP_FILESIO_END2 ( tempPath.c_str(), kXMPErrSev_FileFatal )
	return this->derivedTemp;

}	// XMPFiles_IO::DeriveTemp

// =================================================================================================
// XMPFiles_IO::AbsorbTemp
// =======================

void XMPFiles_IO::AbsorbTemp()
{
	XMP_FILESIO_START
	XMP_Assert ( this->fileRef != Host_IO::noFileRef );

	XMPFiles_IO * temp = this->derivedTemp;
	if ( temp == 0 ) {
		XMP_Throw ( "XMPFiles_IO::AbsorbTemp, no temp to absorb", kXMPErr_InternalFailure );
	}
	XMP_Assert ( temp->isTemp );

	this->Close();
	temp->Close();

	Host_IO::SwapData ( this->filePath.c_str(), temp->filePath.c_str() );
	this->DeleteTemp();

	this->fileRef = Host_IO::Open ( this->filePath.c_str(), Host_IO::openReadWrite );
	this->currLength = Host_IO::Length ( this->fileRef );
	this->currOffset = 0;
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal )

}	// XMPFiles_IO::AbsorbTemp

// =================================================================================================
// XMPFiles_IO::DeleteTemp
// =======================

void XMPFiles_IO::DeleteTemp()
{
	XMP_FILESIO_START
	XMPFiles_IO * temp = this->derivedTemp;

	if ( temp != 0 ) {

		if ( temp->fileRef != Host_IO::noFileRef ) {
			Host_IO::Close ( temp->fileRef );
			temp->fileRef = Host_IO::noFileRef;
		}

		if ( ! temp->filePath.empty() ) {
			Host_IO::Delete ( temp->filePath.c_str() );
			temp->filePath.erase();
		}

		delete temp;
		this->derivedTemp = 0;

	}
	XMP_FILESIO_END2 ( this->derivedTemp->filePath.c_str(), kXMPErrSev_FileFatal )

}	// XMPFiles_IO::DeleteTemp

// =================================================================================================
// XMPFiles_IO::Close
// ==================

void XMPFiles_IO::Close()
{
	XMP_FILESIO_START
	if ( this->fileRef != Host_IO::noFileRef ) {
		Host_IO::Close ( this->fileRef );
		this->fileRef = Host_IO::noFileRef;
	}
	XMP_FILESIO_END1 ( kXMPErrSev_FileFatal )

}	// XMPFiles_IO::Close

// =================================================================================================