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()
59 char TmpName
[PATH_MAX
];
63 if (tmpnam(TmpName
) == NULL
)
64 return CAB_STATUS_CANNOT_CREATE
;
66 /* Append 'tmp' if the file name ends with a dot */
67 length
= strlen(TmpName
);
68 if (length
> 0 && TmpName
[length
- 1] == '.')
69 strcat(TmpName
, "tmp");
71 /* Skip a leading slash or backslash */
73 if (*pName
== '/' || *pName
== '\\')
76 strcpy(FullName
, pName
);
78 FileHandle
= fopen(FullName
, "w+b");
79 if (FileHandle
== NULL
)
80 return CAB_STATUS_CANNOT_CREATE
;
82 if ((FileHandle
= tmpfile()) == NULL
)
83 return CAB_STATUS_CANNOT_CREATE
;
85 return CAB_STATUS_SUCCESS
;
89 * @name CCFDATAStorage class
97 ULONG
CCFDATAStorage::Destroy()
99 ASSERT(FileHandle
!= NULL
);
109 return CAB_STATUS_SUCCESS
;
113 * @name CCFDATAStorage class
116 * Truncate the scratch file to zero bytes
119 * Status of operation
121 ULONG
CCFDATAStorage::Truncate()
124 FileHandle
= fopen(FullName
, "w+b");
125 if (FileHandle
== NULL
)
127 DPRINT(MID_TRACE
, ("ERROR '%i'.\n", errno
));
128 return CAB_STATUS_FAILURE
;
131 return CAB_STATUS_SUCCESS
;
135 * @name CCFDATAStorage class
138 * Returns current position in file
143 ULONG
CCFDATAStorage::Position()
145 return (ULONG
)ftell(FileHandle
);
150 * @name CCFDATAStorage class
153 * Seeks to an absolute position
156 * Absolute position to seek to
159 * Status of operation
161 ULONG
CCFDATAStorage::Seek(LONG Position
)
163 if (fseek(FileHandle
, (off_t
)Position
, SEEK_SET
) != 0)
164 return CAB_STATUS_FAILURE
;
166 return CAB_STATUS_SUCCESS
;
171 * @name CCFDATAStorage class
174 * Reads a CFDATA block from the file
177 * Pointer to CFDATA block for the buffer
180 * Pointer to buffer to store data read
183 * Pointer to buffer to write number of bytes read
186 * Status of operation
188 ULONG
CCFDATAStorage::ReadBlock(PCFDATA Data
, void* Buffer
, PULONG BytesRead
)
190 *BytesRead
= fread(Buffer
, 1, Data
->CompSize
, FileHandle
);
191 if (*BytesRead
!= Data
->CompSize
)
192 return CAB_STATUS_CANNOT_READ
;
194 return CAB_STATUS_SUCCESS
;
199 * @name CCFDATAStorage class
202 * Writes a CFDATA block to the file
205 * Pointer to CFDATA block for the buffer
208 * Pointer to buffer with data to write
210 * @param BytesWritten
211 * Pointer to buffer to write number of bytes written
214 * Status of operation
216 ULONG
CCFDATAStorage::WriteBlock(PCFDATA Data
, void* Buffer
, PULONG BytesWritten
)
218 *BytesWritten
= fwrite(Buffer
, 1, Data
->CompSize
, FileHandle
);
219 if (*BytesWritten
!= Data
->CompSize
)
220 return CAB_STATUS_CANNOT_WRITE
;
222 return CAB_STATUS_SUCCESS
;
226 #endif /* CAB_READ_ONLY */