wfut  0.2.4
A client side C++ implementation of WFUT (WorldForge Update Tool).
platform.cpp
1 // This file may be redistributed and modified only under the terms of
2 // the GNU Lesser General Public License (See COPYING for details).
3 // Copyright (C) 2007 Simon Goodall
4 
5 
6 #include <cassert>
7 #include <stdlib.h>
8 #include <errno.h>
9 #include <stdio.h>
10 
11 //get MSVC header here: http://www.softagalleria.net/download/dirent/dirent-1.11.zip
12 #include <dirent.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 
16 #include <map>
17 #include <string>
18 #include <list>
19 #include <algorithm>
20 
21 #include "libwfut/platform.h"
22 
23 
24 #ifdef _WIN32
25 #include <direct.h>
26 #define mkdir(path,mode) _mkdir(path)
27 #define stat _stat
28 #endif
29 
30 namespace WFUT {
31 
32 FILE *os_create_tmpfile() {
33 #ifdef _MSC_VER
34  char path[MAX_PATH];
35  int ret = GetTempPathA(MAX_PATH, path);
36  if(ret > MAX_PATH || (ret == 0)){
37  strcpy(path, ".\\");
38  }
39  char filename[MAX_PATH];
40  //only 3 prefix characters allowed
41  ret = GetTempFileNameA(path,"wfu",0,filename);
42  if(ret == 0){
43  sprintf(filename,"%swfut%d.tmp",path,rand());
44  }
45  //There is a special 'D' sign, which means temporary and delete on close
46  //http://msdn.microsoft.com/en-us/library/yeby3zcb%28v=vs.71%29.aspx
47  return fopen(filename, "w+bD");
48 #else
49  return tmpfile();
50 #endif
51 }
52 
53 void os_free_tmpfile(FILE *fp) {
54  assert(fp != 0);
55  // tmpfile cleans up after itself
56  fclose(fp);
57 }
58 
59 int os_mkdir(const std::string &dir) {
60  return mkdir(dir.c_str(), 0700);
61 }
62 
63 bool os_exists(const std::string &file) {
64  struct stat info;
65  if (::stat(file.c_str(), &info) == 0) {
66  return true;
67  } else {
68  return false;
69  }
70 }
71 
72 int os_set_executable(const std::string &file) {
73 #ifdef _WIN32
74  // nothing to do for windows
75  return 0;
76 #else
77  struct stat info;
78  if (::stat(file.c_str(), &info) == 0) {
79 
80  mode_t mode = info.st_mode;
81  mode |= S_IXGRP | S_IXOTH | S_IXUSR;
82 
83  // TODO: Should we restrict these permissions some more?
84  // E.g., only user?
85  return chmod(file.c_str(), mode);
86  }
87  return 0;
88 #endif
89 }
90 
91 int os_dir_walk(const std::string &path, const std::list<std::string> &excludes, std::list<std::string> &files) {
92  DIR *d = opendir(path.c_str());
93  if (d != 0) {
94  struct dirent *dent = readdir(d);
95  while (dent != 0) {
96  const std::string d_name(dent->d_name);
97  if (d_name != "." && d_name != ".." && std::find(excludes.begin(), excludes.end(), d_name) == excludes.end()) {
98  //NOTE: dent->d_type member is not existing in mingw32, so we need to use stat.
99  struct stat filestat;
100  stat(d_name.c_str(), &filestat);
101  if (S_ISDIR(filestat.st_mode)) {
102  const std::string &pathname = path + "/" + d_name;
103  os_dir_walk(pathname, excludes, files);
104  } else if (S_ISREG(filestat.st_mode)) {
105  const std::string &filename = path + "/" + d_name;
106  files.push_back(filename);
107  }}
108  dent = readdir(d);
109  }
110  }
111 
112  return 0;
113 }
114 
115 
116 } /* namespace WFUT */