1 #ifdef HAVE_CONFIG_H
2 # include <config.h>
3 #endif
4 
5 #include <errno.h>
6 #include <stdio.h>
7 #include <string.h>
8 #include <unistd.h>
9 #include <time.h>
10 #include <fcntl.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #ifdef _WIN32
14 # include <evil_private.h> /* mkdir */
15 #else
16 # include <pwd.h>
17 #endif
18 #include <libgen.h>
19 
20 #ifdef _WIN32
21 # include <ws2tcpip.h>
22 #endif
23 
24 #include <Ecore.h>
25 #include <ecore_private.h>
26 
27 #include "Ecore_Con.h"
28 #include "ecore_con_private.h"
29 
30 EAPI char *
ecore_con_local_path_new(Eina_Bool is_system,const char * name,int port)31 ecore_con_local_path_new(Eina_Bool is_system, const char *name, int port)
32 {
33 #if _WIN32
34    char buf[256 - sizeof(PIPE_NS)] = "";
35 
36    /* note: using '!' instead of '|' since at least on wine '|' causes
37     * ERROR_INVALID_NAME
38     */
39 
40    if (!is_system)
41      {
42         TCHAR user[sizeof(buf) - sizeof("ecore!u!n!1")] = "unknown";
43         DWORD userlen = sizeof(user);
44         if (!GetUserName(user, &userlen))
45           {
46              char *msg = _efl_net_windows_error_msg_get(GetLastError());
47              ERR("GetUserName(%p, %lu): %s", user, userlen, msg);
48              free(msg);
49           }
50         if (port < 0)
51           snprintf(buf, sizeof(buf), "ecore!%s!%s", user, name);
52         else
53           snprintf(buf, sizeof(buf), "ecore!%s!%s!%d", user, name, port);
54      }
55    else
56      {
57         if (port < 0)
58           snprintf(buf, sizeof(buf), "ecore_service!%s", name);
59         else
60           snprintf(buf, sizeof(buf), "ecore_service!%s!%d", name, port);
61      }
62    return strdup(buf);
63 #else
64    char buf[4096];
65    const char *homedir;
66 
67    EINA_SAFETY_ON_NULL_RETURN_VAL(name, NULL);
68 
69    if (!is_system)
70      {
71         if (port < 0)
72           eina_vpath_resolve_snprintf(buf, sizeof(buf), "(:usr.run:)/.ecore/%s", name);
73         else
74           eina_vpath_resolve_snprintf(buf, sizeof(buf), "(:usr.run:)/.ecore/%s/%i", name, port);
75 
76         return strdup(buf);
77      }
78    else
79      {
80         if (port < 0)
81           {
82              if (name[0] == '/')
83                return strdup(name);
84              else
85                {
86                   homedir = eina_environment_tmp_get();
87                   snprintf(buf, sizeof(buf), "%s/.ecore_service|%s",
88                            homedir, name);
89                   return strdup(buf);
90                }
91           }
92         else
93           {
94              if (name[0] == '/')
95                snprintf(buf, sizeof(buf), "%s|%i", name, port);
96              else
97                {
98                   homedir = eina_environment_tmp_get();
99                   snprintf(buf, sizeof(buf), "%s/.ecore_service|%s|%i",
100                            homedir, name, port);
101                }
102              return strdup(buf);
103           }
104      }
105 #endif
106 }
107 
108 void
_ecore_con_local_mkpath(const char * path,mode_t mode)109 _ecore_con_local_mkpath(const char *path, mode_t mode)
110 {
111    char *s, *d, *itr;
112 
113    if (!path) return;
114    EINA_SAFETY_ON_TRUE_RETURN(path[0] != '/');
115 
116    s = strdup(path);
117    EINA_SAFETY_ON_NULL_RETURN(s);
118    d = dirname(s);
119    EINA_SAFETY_ON_NULL_RETURN(d);
120 
121    for (itr = d + 1; *itr != '\0'; itr++)
122      {
123         if (*itr == '/')
124           {
125              *itr = '\0';
126              if (mkdir(d, mode) != 0)
127                {
128                   if (errno != EEXIST)
129                     {
130                        ERR("could not create parent directory '%s' of path '%s': %s", d, path, eina_error_msg_get(errno));
131                        goto end;
132                     }
133                }
134              *itr = '/';
135           }
136      }
137 
138    if (mkdir(d, mode) != 0)
139      {
140         if (errno != EEXIST)
141           ERR("could not create parent directory '%s' of path '%s': %s", d, path, eina_error_msg_get(errno));
142         else
143           {
144              struct stat st;
145              if ((stat(d, &st) != 0) || (!S_ISDIR(st.st_mode)))
146                ERR("could not create parent directory '%s' of path '%s': exists but is not a directory", d, path);
147           }
148      }
149 
150  end:
151    free(s);
152 }
153