File: POSIXUtil.cpp

package info (click to toggle)
nsis 3.11-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 12,496 kB
  • sloc: cpp: 39,326; ansic: 27,284; python: 1,386; asm: 712; xml: 409; pascal: 231; makefile: 225; javascript: 67
file content (132 lines) | stat: -rwxr-xr-x 4,210 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
//---------------------------------------------------------------------------
// POSIXUtil.cpp
//---------------------------------------------------------------------------
//                           -=* VPatch *=-
//---------------------------------------------------------------------------
// Copyright (C) 2001-2005 Koen van de Sande / Van de Sande Productions
//---------------------------------------------------------------------------
// Website: http://www.tibed.net/vpatch
//
// This software is provided 'as-is', without any express or implied
// warranty.  In no event will the authors be held liable for any damages
// arising from the use of this software.
//
// Permission is granted to anyone to use this software for any purpose,
// including commercial applications, and to alter it and redistribute it
// freely, subject to the following restrictions:
//
// 1. The origin of this software must not be misrepresented; you must not
//    claim that you wrote the original software. If you use this software
//    in a product, an acknowledgment in the product documentation would be
//    appreciated but is not required.
// 2. Altered source versions must be plainly marked as such, and must not be
//    misrepresented as being the original software.
// 3. This notice may not be removed or altered from any source distribution.
//
// Unicode support by Jim Park -- 08/29/2007


#include "POSIXUtil.h"
#include <iostream>
#include <fstream>

#include <time.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>

/* My Borland/VC++ compiler do not have this file */
#if !defined(__BORLANDC__) && !defined(_MSC_VER)
  #include <unistd.h>
#endif

#ifdef __WIN32__     /* The Windows way of getting a temp file needs windows.h*/
  #include <windows.h>
#endif

using namespace std;

namespace POSIX {

  void TimeT_To_FILETIME(time_t t, ALT_FILETIME *pft ) {
      uint64_t ll = (((uint64_t)t) *10000000L) + (((uint64_t)116444736L) * 1000000000L);
      pft->dwLowDateTime = (uint32_t)(ll & 0xFFFFFFFF);
      pft->dwHighDateTime = (uint32_t)((ll>>32)&0xFFFFFFFF);
  }

#ifdef __WIN32__
  /* do it the old way on Win32, because POSIX does not get timezone stuff right */
  ALT_FILETIME getFileTime(const TCHAR* sFileName) {
    FILETIME temp;
    GetSystemTimeAsFileTime(&temp);
    HANDLE h = CreateFile(sFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                            OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);
    if (h == INVALID_HANDLE_VALUE) {
      terr << _T("Cannot read file time of ") << sFileName << _T("\n");
    } else {
      GetFileTime(h, NULL, NULL, &temp);
      CloseHandle(h);
    }
    ALT_FILETIME result;
    result.dwHighDateTime = temp.dwHighDateTime;
    result.dwLowDateTime = temp.dwLowDateTime;
    return result;
  }
#else
  ALT_FILETIME getFileTime(const char* sFileName) {
    struct stat buf;
    /* get current time first as a fall-back */
    time_t currentTime = time(NULL);

    if(stat(sFileName, &buf)) {
      terr << _T("Cannot read file time of ") << sFileName << _T("\n");
    } else {
      /* get the time from the file */
      currentTime = buf.st_mtime;
    }
    ALT_FILETIME result;
    TimeT_To_FILETIME(currentTime, &result);
    return result;
  }
#endif

  uint32_t getFileSize(const TCHAR* sFileName) {
    bifstream f;
    f.open(sFileName, std::ios_base::binary | std::ios_base::in);
    if (!f.good() || f.eof() || !f.is_open()) {
      throw _T("File could not be read (getFileSize)");
    }
    f.seekg(0, std::ios_base::beg);
    std::ifstream::pos_type begin_pos = f.tellg();
    f.seekg(0, std::ios_base::end);
    return static_cast<int>(f.tellg() - begin_pos);
  }

#ifdef __WIN32__
  tstring getTempFile() {
    TCHAR buffer[MAX_PATH];
    if(GetTempFileName(_T("."),_T("vpatch"),0,buffer) == 0) {
      terr << _T("Cannot create temporary filename");
    }
    return tstring(buffer);
  }
#else
  tstring getTempFile() {
    TCHAR t[] = _T("/tmp/genpatXXXXXX");

    mode_t old_umask = umask(0077);

    int fd = mkstemp(t);
    if (fd == -1) {
      terr << _T("Cannot create temporary filename");
      return _T("");
    }
    close(fd);

    umask(old_umask);

    return tstring(t);
  }
#endif

}