annotate common/portpath.c @ 1075:7e1fd6d96714

(issue109) Feedback improved. Shell translation updated.
author Bernhard Reiter <bernhard@intevation.de>
date Thu, 11 Sep 2014 10:18:46 +0200
parents f110a3f6e387
children fd7d04bb37cb
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
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
22 char *
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
23 port_dirname(char *path)
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
24 {
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
25 #ifndef _WIN32
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
26 return dirname(path);
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
27 #else
164
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
28 char drive[_MAX_DRIVE];
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
29 char dir[_MAX_DIR];
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
30 _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
31 size_t dlen = strlen(dir);
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
32 if ((dlen > 0) &&
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
33 ((dir[dlen-1] == '/') || (dir[dlen-1] == '\\')))
3343ddf43f42 Windows implementation of port_dirname: strip trailing '/' and '\'.
Sascha Wilde <wilde@intevation.de>
parents: 169
diff changeset
34 dir[dlen-1] = '\0';
164
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
35 /* We assume: drive + dir is shorter than
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
36 * drive + dir + fname + ext */
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
37 sprintf(path, "%s%s", drive, dir);
6d64d7e9fa32 Implemented port_dirname for windows.
Sascha Wilde <wilde@intevation.de>
parents: 146
diff changeset
38 return path;
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
39 #endif
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
40 }
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
41
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
42 bool
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
43 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
44 {
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
45 #ifndef _WIN32
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
46 if (propagate_acl)
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
47 {
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
48 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
49 }
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
50 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
51 #else
984
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
52 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
53 bool ret;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
54
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
55 if (!wchar_path)
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
56 {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
57 return false;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
58 }
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
59 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
60 xfree (wchar_path);
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
61 return ret;
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
62 #endif
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
63 }
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
64
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
65 char *
975
b3695a3399de (issue86) Install into default directories on Linux
Andre Heinecke <andre.heinecke@intevation.de>
parents: 404
diff changeset
66 port_realpath(const char *path)
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
67 {
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
68 #ifndef _WIN32
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
69 return realpath(path, NULL);
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
70 #else
169
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
71 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
72 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
73 return fp;
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
74 else
701b7036c5dc Windows implementation of port_realpath: NULL if file does not exist.
Sascha Wilde <wilde@intevation.de>
parents: 168
diff changeset
75 return NULL;
146
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
76 #endif
306e4db11761 Added portable path name handling functions.
Sascha Wilde <wilde@intevation.de>
parents:
diff changeset
77 }
168
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
78
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
79 bool
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
80 port_fileexits(char *path)
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
81 {
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
82 int ret;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
83 #ifndef _WIN32
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
84 struct stat sb;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
85 ret = stat(path, &sb);
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
86 #else
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
87 struct _stat sb;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
88 ret = _stat(path, &sb);
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
89 #endif
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
90
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
91 if (ret == 0)
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
92 return true;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
93 else
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
94 return false;
f100861dad8f Added simple portable function to test if an file exists.
Sascha Wilde <wilde@intevation.de>
parents: 166
diff changeset
95 }
176
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
96
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
97 bool
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
98 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
99 {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
100 char *parent_path,
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
101 *p;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
102 if (!path) {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
103 return false;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
104 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
105 if (port_isdir(path)) {
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
106 return true;
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
107 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
108 parent_path = xstrndup (path, strlen(path));
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
109 p = strrchr(parent_path, '/');
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
110 if (!p)
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 p = strrchr(parent_path, '\\');
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
113 }
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 return false;
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 *p = '\0';
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
119 if (!port_isdir(parent_path))
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
120 {
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
121 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
122 }
1070
f110a3f6e387 (issue114) Fine tune ACL propagation
Andre Heinecke <andre.heinecke@intevation.de>
parents: 986
diff changeset
123 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
124 }
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
125
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
126 bool
faf58e9f518b Add recursive mkdir and mkdir for windows
Andre Heinecke <andre.heinecke@intevation.de>
parents: 975
diff changeset
127 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
128 {
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
129 int ret;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
130 #ifndef _WIN32
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
131 struct stat sb;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
132 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
133 #else
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
134 struct _stat sb;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
135 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
136 #endif
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
137
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
138 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
139 return true;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
140 else
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
141 return false;
70d627e9e801 New portability function to test if a file is an directory.
Sascha Wilde <wilde@intevation.de>
parents: 170
diff changeset
142 }

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