2 * PROJECT: ReactOS cabinet manager
3 * LICENSE: GPL-2.0+ (https://spdx.org/licenses/GPL-2.0+)
4 * PURPOSE: CCFDATAStorage class implementation for Linux/Unix
5 * COPYRIGHT: Copyright 2017 Casper S. Hornstrup (chorns@users.sourceforge.net)
6 * Copyright 2017 Colin Finck <mail@colinfinck.de>
7 * Copyright 2018 Dmitry Bagdanov <dimbo_job@mail.ru>
13 #include <sys/types.h>
23 #if !defined(CAB_READ_ONLY)
26 * @name CCFDATAStorage class
31 CCFDATAStorage::CCFDATAStorage()
37 * @name CCFDATAStorage class
42 CCFDATAStorage::~CCFDATAStorage()
44 ASSERT(FileHandle
== NULL
);
48 * @name CCFDATAStorage class
56 ULONG
CCFDATAStorage::Create()
58 if ((FileHandle
= tmpfile()) == NULL
)
60 /* Workaround for breakage on some Windows system */
61 FileHandle
= fopen(tmpnam(NULL
) + 1, "wb");
62 if (FileHandle
== NULL
)
63 return CAB_STATUS_CANNOT_CREATE
;
66 return CAB_STATUS_SUCCESS
;
70 * @name CCFDATAStorage class
78 ULONG
CCFDATAStorage::Destroy()
80 ASSERT(FileHandle
!= NULL
);
86 return CAB_STATUS_SUCCESS
;
90 * @name CCFDATAStorage class
93 * Truncate the scratch file to zero bytes
98 ULONG
CCFDATAStorage::Truncate()
101 FileHandle
= tmpfile();
102 if (FileHandle
== NULL
)
104 DPRINT(MID_TRACE
, ("ERROR '%i'.\n", errno
));
105 return CAB_STATUS_FAILURE
;
108 return CAB_STATUS_SUCCESS
;
112 * @name CCFDATAStorage class
115 * Returns current position in file
120 ULONG
CCFDATAStorage::Position()
122 return (ULONG
)ftell(FileHandle
);
127 * @name CCFDATAStorage class
130 * Seeks to an absolute position
133 * Absolute position to seek to
136 * Status of operation
138 ULONG
CCFDATAStorage::Seek(LONG Position
)
140 if (fseek(FileHandle
, (off_t
)Position
, SEEK_SET
) != 0)
141 return CAB_STATUS_FAILURE
;
143 return CAB_STATUS_SUCCESS
;
148 * @name CCFDATAStorage class
151 * Reads a CFDATA block from the file
154 * Pointer to CFDATA block for the buffer
157 * Pointer to buffer to store data read
160 * Pointer to buffer to write number of bytes read
163 * Status of operation
165 ULONG
CCFDATAStorage::ReadBlock(PCFDATA Data
, void* Buffer
, PULONG BytesRead
)
167 *BytesRead
= fread(Buffer
, 1, Data
->CompSize
, FileHandle
);
168 if (*BytesRead
!= Data
->CompSize
)
169 return CAB_STATUS_CANNOT_READ
;
171 return CAB_STATUS_SUCCESS
;
176 * @name CCFDATAStorage class
179 * Writes a CFDATA block to the file
182 * Pointer to CFDATA block for the buffer
185 * Pointer to buffer with data to write
187 * @param BytesWritten
188 * Pointer to buffer to write number of bytes written
191 * Status of operation
193 ULONG
CCFDATAStorage::WriteBlock(PCFDATA Data
, void* Buffer
, PULONG BytesWritten
)
195 *BytesWritten
= fwrite(Buffer
, 1, Data
->CompSize
, FileHandle
);
196 if (*BytesWritten
!= Data
->CompSize
)
197 return CAB_STATUS_CANNOT_WRITE
;
199 return CAB_STATUS_SUCCESS
;
203 #endif /* CAB_READ_ONLY */