Debian Codegraph @

Contrib/VPatch/Source/GenPat/POSIXUtil.cpp

raw
1 //---------------------------------------------------------------------------
2 // POSIXUtil.cpp
3 //---------------------------------------------------------------------------
4 // -=* VPatch *=-
5 //---------------------------------------------------------------------------
6 // Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions
7 //---------------------------------------------------------------------------
8 // Website: http://www.tibed.net/vpatch
9 //
10 // This software is provided 'as-is', without any express or implied
11 // warranty. In no event will the authors be held liable for any damages
12 // arising from the use of this software.
13 //
14 // Permission is granted to anyone to use this software for any purpose,
15 // including commercial applications, and to alter it and redistribute it
16 // freely, subject to the following restrictions:
17 //
18 // 1. The origin of this software must not be misrepresented; you must not
19 // claim that you wrote the original software. If you use this software
20 // in a product, an acknowledgment in the product documentation would be
21 // appreciated but is not required.
22 // 2. Altered source versions must be plainly marked as such, and must not be
23 // misrepresented as being the original software.
24 // 3. This notice may not be removed or altered from any source distribution.
25 //
26 // Unicode support by Jim Park -- 08/29/2007
27
28
29 #include "POSIXUtil.h"
30 #include <iostream>
31 #include <fstream>
32
33 #include <time.h>
34 #include <stdlib.h>
35 #include <sys/types.h>
36 #include <sys/stat.h>
37
38 /* My Borland/VC++ compiler do not have this file */
39 #if !defined(__BORLANDC__) && !defined(_MSC_VER)
40 #include <unistd.h>
41 #endif
42
43 #ifdef __WIN32__ /* The Windows way of getting a temp file needs windows.h*/
44 #include <windows.h>
45 #endif
46
47 using namespace std;
48
49 namespace POSIX {
50
51 void TimeT_To_FILETIME(time_t t, ALT_FILETIME *pft ) {
52 uint64_t ll = (((uint64_t)t) *10000000L) + (((uint64_t)116444736L) * 1000000000L);
53 pft->dwLowDateTime = (uint32_t)(ll & 0xFFFFFFFF);
54 pft->dwHighDateTime = (uint32_t)((ll>>32)&0xFFFFFFFF);
55 }
56
57 #ifdef __WIN32__
58 /* do it the old way on Win32, because POSIX does not get timezone stuff right */
59 ALT_FILETIME getFileTime(const TCHAR* sFileName) {
60 FILETIME temp;
61 GetSystemTimeAsFileTime(&temp);
62 HANDLE h = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
63 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
64 if (h == INVALID_HANDLE_VALUE) {
65 terr << _T("Cannot read file time of ") << sFileName << _T("\n");
66 } else {
67 GetFileTime(h, NULL, NULL, &temp);
68 CloseHandle(h);
69 }
70 ALT_FILETIME result;
71 result.dwHighDateTime = temp.dwHighDateTime;
72 result.dwLowDateTime = temp.dwLowDateTime;
73 return result;
74 }
75 #else
76 ALT_FILETIME getFileTime(const char* sFileName) {
77 struct stat buf;
78 /* get current time first as a fall-back */
79 time_t currentTime = time(NULL);
80
81 if(stat(sFileName, &buf)) {
82 terr << _T("Cannot read file time of ") << sFileName << _T("\n");
83 } else {
84 /* get the time from the file */
85 currentTime = buf.st_mtime;
86 }
87 ALT_FILETIME result;
88 TimeT_To_FILETIME(currentTime, &result);
89 return result;
90 }
91 #endif
92
93 uint32_t getFileSize(const TCHAR* sFileName) {
94 bifstream f;
95 f.open(sFileName, std::ios_base::binary | std::ios_base::in);
96 if (!f.good() || f.eof() || !f.is_open()) {
97 throw _T("File could not be read (getFileSize)");
98 }
99 f.seekg(0, std::ios_base::beg);
100 std::ifstream::pos_type begin_pos = f.tellg();
101 f.seekg(0, std::ios_base::end);
102 return static_cast<int>(f.tellg() - begin_pos);
103 }
104
105 #ifdef __WIN32__
106 tstring getTempFile() {
107 TCHAR buffer[MAX_PATH];
108 if(GetTempFileName(_T("."),_T("vpatch"),0,buffer) == 0) {
109 terr << _T("Cannot create temporary filename");
110 }
111 return tstring(buffer);
112 }
113 #else
114 tstring getTempFile() {
115 TCHAR t[] = _T("/tmp/genpatXXXXXX");
116
117 mode_t old_umask = umask(0077);
118
119 int fd = mkstemp(t);
120 if (fd == -1) {
121 terr << _T("Cannot create temporary filename");
122 return _T("");
123 }
124 close(fd);
125
126 umask(old_umask);
127
128 return tstring(t);
129 }
130 #endif
131
132 }