annotate common/portpath.c @ 1169:882df57bdaf6

Merged.
author Emanuel Schuetze <emanuel@intevation.de>
date Fri, 19 Sep 2014 11:54:16 +0200
parents fd7d04bb37cb
children 0a803c3fb5a6
rev   line source
404
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 176
diff changeset
1 /* Copyright (C) 2014 by Bundesamt für Sicherheit in der Informationstechnik
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 176
diff changeset
2 * Software engineering by Intevation GmbH
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 176
diff changeset
3 *
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 176
diff changeset
4 * This file is Free Software under the GNU GPL (v>=2)
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 176
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY!
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 176
diff changeset
6 * See LICENSE.txt for details.
17e1c8f37d72 Add License
Andre Heinecke <aheinecke@intevation.de>
parents: 176
diff changeset
7 */
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
8 #include "portpath.h"
984
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
9 #include "strhelp.h"
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
10 #include "util.h"
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
11 #include "logging.h"
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
12
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
13 #include <libgen.h>
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
14 #include <limits.h>
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
15 #include <stdio.h>
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
16 #include <stdlib.h>
168
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
17 #include <sys/stat.h>
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
18 #include <sys/types.h>
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
19 #include <unistd.h>
986
66f6361fba1b Fix linux build
Andre Heinecke <andre.heinecke@intevation.de>
parents: 984
diff changeset
20 #include <string.h>
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
21
1157
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
22 #ifdef WIN32
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
23 #include <share.h>
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
24 #endif
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
25
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
26 char *
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
27 port_dirname(char *path)
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
28 {
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
29 #ifndef _WIN32
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
30 return dirname(path);
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
31 #else
164
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
32 char drive[_MAX_DRIVE];
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
33 char dir[_MAX_DIR];
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
34 _splitpath(path, drive, dir, NULL, NULL);
170
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
35 size_t dlen = strlen(dir);
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
36 if ((dlen > 0) &&
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
37 ((dir[dlen-1] == '/') || (dir[dlen-1] == '\\')))
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
38 dir[dlen-1] = '\0';
164
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
39 /* We assume: drive + dir is shorter than
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
40 * drive + dir + fname + ext */
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
41 sprintf(path, "%s%s", drive, dir);
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
42 return path;
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
43 #endif
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
44 }
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
45
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
46 bool
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
47 port_mkdir(const char *path, bool propagate_acl)
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
48 {
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
49 #ifndef _WIN32
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
50 if (propagate_acl)
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
51 {
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
52 DEBUGPRINTF("WARNING: ACL propagation only has an effect on Windows.\n");
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
53 }
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
54 return mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0;
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
55 #else
984
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
56 wchar_t *wchar_path = utf8_to_wchar(path, strlen(path));
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
57 bool ret;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
58
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
59 if (!wchar_path)
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
60 {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
61 return false;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
62 }
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
63 ret = create_restricted_directory (wchar_path, propagate_acl);
984
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
64 xfree (wchar_path);
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
65 return ret;
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
66 #endif
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
67 }
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
68
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
69 char *
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
70 port_realpath(const char *path)
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
71 {
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
72 #ifndef _WIN32
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
73 return realpath(path, NULL);
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
74 #else
169
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
75 char *fp = _fullpath(NULL, path, 0);
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
76 if (port_fileexits(fp))
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
77 return fp;
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
78 else
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
79 return NULL;
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
80 #endif
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
81 }
168
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
82
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
83 bool
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
84 port_fileexits(char *path)
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
85 {
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
86 int ret;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
87 #ifndef _WIN32
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
88 struct stat sb;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
89 ret = stat(path, &sb);
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
90 #else
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
91 struct _stat sb;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
92 ret = _stat(path, &sb);
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
93 #endif
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
94
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
95 if (ret == 0)
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
96 return true;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
97 else
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
98 return false;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
99 }
176
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
100
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
101 bool
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
102 port_mkdir_p(const char *path, bool propagate_acl)
984
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
103 {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
104 char *parent_path,
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
105 *p;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
106 if (!path) {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
107 return false;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
108 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
109 if (port_isdir(path)) {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
110 return true;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
111 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
112 parent_path = xstrndup (path, strlen(path));
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
113 p = strrchr(parent_path, '/');
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
114 if (!p)
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
115 {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
116 p = strrchr(parent_path, '\\');
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
117 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
118 if (!p)
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
119 {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
120 return false;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
121 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
122 *p = '\0';
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
123 if (!port_isdir(parent_path))
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
124 {
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
125 port_mkdir_p(parent_path, false);
984
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
126 }
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
127 return port_mkdir(path, propagate_acl);
984
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
128 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
129
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
130 bool
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
131 port_isdir(const char *path)
176
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
132 {
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
133 int ret;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
134 #ifndef _WIN32
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
135 struct stat sb;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
136 ret = stat(path, &sb);
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
137 #else
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
138 struct _stat sb;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
139 ret = _stat(path, &sb);
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
140 #endif
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
141
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
142 if ((ret == 0) && S_ISDIR(sb.st_mode))
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
143 return true;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
144 else
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
145 return false;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
146 }
1157
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
147
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
148 FILE*
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
149 port_fopen_rb(const char *path, bool exclusive)
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
150 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
151 FILE *f = NULL;
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
152 if (!path)
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
153 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
154 return NULL;
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
155 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
156 #ifdef WIN32
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
157 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
158 wchar_t *wFilename = utf8_to_wchar(path, strlen(path));
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
159 if (!wFilename)
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
160 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
161 ERRORPRINTF ("Invalid encoding\n");
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
162 return NULL;
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
163 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
164 /* We open and write protect the file here so that
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
165 as long as the file is open we can be sure that
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
166 it was not modified and can use it in subsequent
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
167 calls based on the filename. */
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
168 OutputDebugStringW(wFilename);
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
169 if (exclusive)
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
170 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
171 f = _wfsopen(wFilename, L"rb", _SH_DENYWR);
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
172 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
173 else
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
174 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
175 f = _wfopen(wFilename, L"rb");
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
176 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
177 xfree(wFilename);
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
178 if (f == NULL)
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
179 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
180 /* Fall back to local8 bit encoding */
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
181 if (exclusive)
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
182 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
183 f = _fsopen(path, "rb", _SH_DENYWR);
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
184 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
185 else
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
186 {
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
187 f = fopen(path, "rb");
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
188 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
189 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
190 }
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
191 #else
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
192 (void)(exclusive);
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
193 f = fopen(path, "rb");
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
194 #endif
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
195 return f;
fd7d04bb37cb (issue36) Add encoding aware port_fopen function and use it
Andre Heinecke <andre.heinecke@intevation.de>
parents: 1070
diff changeset
196 }

http://wald.intevation.org/projects/trustbridge/