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 char TmpName
[MAX_PATH
];
62 if (tmpnam(TmpName
) == NULL
)
63 return CAB_STATUS_CANNOT_CREATE
;
65 /* Append 'tmp' if the file name ends with a dot */
66 length
= strlen(TmpName
);
67 if (length
> 0 && TmpName
[length
- 1] == '.')
68 strcat(TmpName
, "tmp");
70 /* Skip a leading slash or backslash */
72 if (*pName
== '/' || *pName
== '\\')
75 strcpy(FullName
, pName
);
77 FileHandle
= fopen(FullName
, "w+b");
78 if (FileHandle
== NULL
)
79 return CAB_STATUS_CANNOT_CREATE
;
81 return CAB_STATUS_SUCCESS
;
85 * @name CCFDATAStorage class
93 ULONG
CCFDATAStorage::Destroy()
95 ASSERT(FileHandle
!= NULL
);
103 return CAB_STATUS_SUCCESS
;
107 * @name CCFDATAStorage class
110 * Truncate the scratch file to zero bytes
113 * Status of operation
115 ULONG
CCFDATAStorage::Truncate()
118 FileHandle
= fopen(FullName
, "w+b");
119 if (FileHandle
== NULL
)
121 DPRINT(MID_TRACE
, ("ERROR '%i'.\n", errno
));
122 return CAB_STATUS_FAILURE
;
125 return CAB_STATUS_SUCCESS
;
129 * @name CCFDATAStorage class
132 * Returns current position in file
137 ULONG
CCFDATAStorage::Position()
139 return (ULONG
)ftell(FileHandle
);
144 * @name CCFDATAStorage class
147 * Seeks to an absolute position
150 * Absolute position to seek to
153 * Status of operation
155 ULONG
CCFDATAStorage::Seek(LONG Position
)
157 if (fseek(FileHandle
, (off_t
)Position
, SEEK_SET
) != 0)
158 return CAB_STATUS_FAILURE
;
160 return CAB_STATUS_SUCCESS
;
165 * @name CCFDATAStorage class
168 * Reads a CFDATA block from the file
171 * Pointer to CFDATA block for the buffer
174 * Pointer to buffer to store data read
177 * Pointer to buffer to write number of bytes read
180 * Status of operation
182 ULONG
CCFDATAStorage::ReadBlock(PCFDATA Data
, void* Buffer
, PULONG BytesRead
)
184 *BytesRead
= fread(Buffer
, 1, Data
->CompSize
, FileHandle
);
185 if (*BytesRead
!= Data
->CompSize
)
186 return CAB_STATUS_CANNOT_READ
;
188 return CAB_STATUS_SUCCESS
;
193 * @name CCFDATAStorage class
196 * Writes a CFDATA block to the file
199 * Pointer to CFDATA block for the buffer
202 * Pointer to buffer with data to write
204 * @param BytesWritten
205 * Pointer to buffer to write number of bytes written
208 * Status of operation
210 ULONG
CCFDATAStorage::WriteBlock(PCFDATA Data
, void* Buffer
, PULONG BytesWritten
)
212 *BytesWritten
= fwrite(Buffer
, 1, Data
->CompSize
, FileHandle
);
213 if (*BytesWritten
!= Data
->CompSize
)
214 return CAB_STATUS_CANNOT_WRITE
;
216 return CAB_STATUS_SUCCESS
;
220 #endif /* CAB_READ_ONLY */