1 | /****************************************************************************
|
---|
2 | ** Filename: zipglobal.cpp
|
---|
3 | ** Last updated [dd/mm/yyyy]: 06/02/2011
|
---|
4 | **
|
---|
5 | ** pkzip 2.0 file compression.
|
---|
6 | **
|
---|
7 | ** Some of the code has been inspired by other open source projects,
|
---|
8 | ** (mainly Info-Zip and Gilles Vollant's minizip).
|
---|
9 | ** Compression and decompression actually uses the zlib library.
|
---|
10 | **
|
---|
11 | ** Copyright (C) 2007-2012 Angius Fabrizio. All rights reserved.
|
---|
12 | **
|
---|
13 | ** This file is part of the OSDaB project (http://osdab.42cows.org/).
|
---|
14 | **
|
---|
15 | ** This file may be distributed and/or modified under the terms of the
|
---|
16 | ** GNU General Public License version 2 as published by the Free Software
|
---|
17 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
18 | ** packaging of this file.
|
---|
19 | **
|
---|
20 | ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
|
---|
21 | ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
|
---|
22 | **
|
---|
23 | ** See the file LICENSE.GPL that came with this software distribution or
|
---|
24 | ** visit http://www.gnu.org/copyleft/gpl.html for GPL licensing information.
|
---|
25 | **
|
---|
26 | **********************************************************************/
|
---|
27 |
|
---|
28 | #include "zipglobal.h"
|
---|
29 |
|
---|
30 | #if defined(Q_OS_WIN) || defined(Q_OS_WINCE) || defined(Q_OS_LINUX) || defined (Q_OS_MACX)
|
---|
31 | #define OSDAB_ZIP_HAS_UTC
|
---|
32 | #include <ctime>
|
---|
33 | #else
|
---|
34 | #undef OSDAB_ZIP_HAS_UTC
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #if defined(Q_OS_WIN)
|
---|
38 | #include <QtCore/qt_windows.h>
|
---|
39 | #elif defined(Q_OS_LINUX) || defined(Q_OS_MACX)
|
---|
40 | #include <utime.h>
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | OSDAB_BEGIN_NAMESPACE(Zip)
|
---|
44 |
|
---|
45 | /*! Returns the current UTC offset in seconds unless OSDAB_ZIP_NO_UTC is defined
|
---|
46 | and method is implemented for the current platform and 0 otherwise.
|
---|
47 | */
|
---|
48 | int OSDAB_ZIP_MANGLE(currentUtcOffset)()
|
---|
49 | {
|
---|
50 | #if !(!defined OSDAB_ZIP_NO_UTC && defined OSDAB_ZIP_HAS_UTC)
|
---|
51 | return 0;
|
---|
52 | #else
|
---|
53 | time_t curr_time_t;
|
---|
54 | time(&curr_time_t);
|
---|
55 |
|
---|
56 | #if defined Q_OS_WIN
|
---|
57 | struct tm _tm_struct;
|
---|
58 | struct tm* tm_struct = &_tm_struct;
|
---|
59 | #else
|
---|
60 | struct tm* tm_struct = 0;
|
---|
61 | #endif
|
---|
62 |
|
---|
63 | #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
|
---|
64 | // use the reentrant version of localtime() where available
|
---|
65 | tzset();
|
---|
66 | tm res;
|
---|
67 | tm_struct = gmtime_r(&curr_time_t, &res);
|
---|
68 | #elif defined Q_OS_WIN && !defined Q_CC_MINGW
|
---|
69 | if (gmtime_s(tm_struct, &curr_time_t))
|
---|
70 | return 0;
|
---|
71 | #else
|
---|
72 | tm_struct = gmtime(&curr_time_t);
|
---|
73 | #endif
|
---|
74 |
|
---|
75 | if (!tm_struct)
|
---|
76 | return 0;
|
---|
77 |
|
---|
78 | const time_t global_time_t = mktime(tm_struct);
|
---|
79 |
|
---|
80 | #if !defined(QT_NO_THREAD) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
|
---|
81 | // use the reentrant version of localtime() where available
|
---|
82 | tm_struct = localtime_r(&curr_time_t, &res);
|
---|
83 | #elif defined Q_OS_WIN && !defined Q_CC_MINGW
|
---|
84 | if (localtime_s(tm_struct, &curr_time_t))
|
---|
85 | return 0;
|
---|
86 | #else
|
---|
87 | tm_struct = localtime(&curr_time_t);
|
---|
88 | #endif
|
---|
89 |
|
---|
90 | if (!tm_struct)
|
---|
91 | return 0;
|
---|
92 |
|
---|
93 | const time_t local_time_t = mktime(tm_struct);
|
---|
94 |
|
---|
95 | const int utcOffset = - qRound(difftime(global_time_t, local_time_t));
|
---|
96 | return tm_struct->tm_isdst > 0 ? utcOffset + 3600 : utcOffset;
|
---|
97 | #endif // No UTC
|
---|
98 |
|
---|
99 | return 0;
|
---|
100 | }
|
---|
101 |
|
---|
102 | QDateTime OSDAB_ZIP_MANGLE(fromFileTimestamp)(const QDateTime& dateTime)
|
---|
103 | {
|
---|
104 | #if !defined OSDAB_ZIP_NO_UTC && defined OSDAB_ZIP_HAS_UTC
|
---|
105 | const int utc = OSDAB_ZIP_MANGLE(currentUtcOffset)();
|
---|
106 | return dateTime.toUTC().addSecs(utc);
|
---|
107 | #else
|
---|
108 | return dateTime;
|
---|
109 | #endif // OSDAB_ZIP_NO_UTC
|
---|
110 | }
|
---|
111 |
|
---|
112 | bool OSDAB_ZIP_MANGLE(setFileTimestamp)(const QString& fileName, const QDateTime& dateTime)
|
---|
113 | {
|
---|
114 | if (fileName.isEmpty())
|
---|
115 | return true;
|
---|
116 |
|
---|
117 | #ifdef Q_OS_WIN
|
---|
118 | HANDLE hFile = CreateFile(fileName.toStdWString().c_str(),
|
---|
119 | GENERIC_WRITE, FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
|
---|
120 | if (hFile == INVALID_HANDLE_VALUE) {
|
---|
121 | return false;
|
---|
122 | }
|
---|
123 |
|
---|
124 | SYSTEMTIME st;
|
---|
125 | FILETIME ft, ftLastMod;
|
---|
126 | const QDate date = dateTime.date();
|
---|
127 | const QTime time = dateTime.time();
|
---|
128 | st.wYear = date.year();
|
---|
129 | st.wMonth = date.month();
|
---|
130 | st.wDay = date.day();
|
---|
131 | st.wHour = time.hour();
|
---|
132 | st.wMinute = time.minute();
|
---|
133 | st.wSecond = time.second();
|
---|
134 | st.wMilliseconds = time.msec();
|
---|
135 |
|
---|
136 | SystemTimeToFileTime(&st, &ft);
|
---|
137 | LocalFileTimeToFileTime(&ft, &ftLastMod);
|
---|
138 |
|
---|
139 | const bool success = SetFileTime(hFile, NULL, NULL, &ftLastMod);
|
---|
140 | CloseHandle(hFile);
|
---|
141 | return success;
|
---|
142 |
|
---|
143 | #elif defined(Q_OS_LINUX) || defined(Q_OS_MACX)
|
---|
144 |
|
---|
145 | struct utimbuf t_buffer;
|
---|
146 | t_buffer.actime = t_buffer.modtime = dateTime.toTime_t();
|
---|
147 | return utime(fileName.toLocal8Bit().constData(), &t_buffer) == 0;
|
---|
148 | #endif
|
---|
149 |
|
---|
150 | return true;
|
---|
151 | }
|
---|
152 | OSDAB_END_NAMESPACE
|
---|