summaryrefslogtreecommitdiff
path: root/src/lib/eina/eina_vpath.c
blob: 38255efcff8d2f2f90c7dd8aa77752b7933158f5 (plain)
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <sys/types.h>
#ifdef _WIN32
# include <evil_private.h> /* mkdir */
#else
# include <pwd.h>
#endif

#include <Eina.h>

#include "eina_internal.h"
#include "eina_private.h"

static Eina_Hash *vpath_data = NULL;

#ifdef CRI
#undef CRI
#endif
#define CRI(...) EINA_LOG_DOM_CRIT(_eina_vpath_log_dom, __VA_ARGS__)

#ifdef ERR
#undef ERR
#endif
#define ERR(...) EINA_LOG_DOM_ERR(_eina_vpath_log_dom, __VA_ARGS__)

#ifdef DBG
#undef DBG
#endif
#define DBG(...) EINA_LOG_DOM_DBG(_eina_vpath_log_dom, __VA_ARGS__)

static int _eina_vpath_log_dom = -1;

static inline void
_eina_vpath_data_add(const char *key, const char *value)
{
   eina_hash_add(vpath_data, key, eina_stringshare_add(value));
}

static inline Eina_Stringshare*
_eina_vpath_data_get(const char *key)
{
   return eina_hash_find(vpath_data, key);
}


static char *
_fallback_runtime_dir(const char *home)
{
   char buf[PATH_MAX];
   struct stat st;
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
   uid_t uid = getuid();

   if (setuid(geteuid()) != 0)
      {
         fprintf(stderr,
                 "FATAL: Cannot setuid - errno=%i\n",
                 errno);
         abort();
      }
#endif
   // fallback - make ~/.run
   snprintf(buf, sizeof(buf), "%s/.run", home);
   if (!!mkdir(buf,  S_IRUSR | S_IWUSR | S_IXUSR))
     {
        if (errno == EEXIST)
          {
             if (stat(buf, &st) == 0)
               {
                  // some sanity checks - but not for security
                  if (!(st.st_mode & S_IFDIR))
                    {
                       // fatal - exists but is not a dir
                       fprintf(stderr,
                               "FATAL: run dir '%s' exists but not a dir\n",
                               buf);
                       abort();
                    }
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
                  if (st.st_uid != geteuid())
                    {
                       // fatal - run dir doesn't belong to user
                       fprintf(stderr,
                               "FATAL: run dir '%s' not owned by uid %i\n",
                               buf, (int)geteuid());
                       abort();
                    }
#endif
               }
             else
               {
                  // fatal - we cant create our run dir in ~/
                  fprintf(stderr,
                          "FATAL: Cannot verify run dir '%s' errno=%i\n",
                          buf, errno);
                  abort();
               }
          }
        else
          {
             // fatal - we cant create our run dir in ~/
             fprintf(stderr,
                     "FATAL: Cannot create run dir '%s' - errno=%i\n",
                     buf, errno);
             abort();
          }
     }
#if defined(HAVE_GETUID) && defined(HAVE_GETEUID)
  if (setreuid(uid, geteuid()) != 0)
     {
        fprintf(stderr,
                "FATAL: Cannot setreuid - errno=%i\n",
                errno);
        abort();
     }
#endif

   return strdup(buf);
}

static char *
_fallback_home_dir()
{
   char buf[PATH_MAX];
   /* Windows does not have getuid(), but home can't be NULL */
#ifdef HAVE_GETEUID
   uid_t uid = geteuid();
   struct stat st;

   snprintf(buf, sizeof(buf), "/tmp/%i", (int)uid);
   if (mkdir(buf,  S_IRUSR | S_IWUSR | S_IXUSR) < 0)
     {
        if (errno != EEXIST)
          {
             if (stat("/tmp", &st) == 0)
               snprintf(buf, sizeof(buf), "/tmp");
             else
               snprintf(buf, sizeof(buf), "/");
          }
     }
   if (stat(buf, &st) != 0)
     {
        if (stat("/tmp", &st) == 0)
          snprintf(buf, sizeof(buf), "/tmp");
        else
          snprintf(buf, sizeof(buf), "/");
     }
#else
   snprintf(buf, sizeof(buf), "/");
#endif
   return strdup(buf);
}

static void
_eina_vpath_interface_sys_init(void)
{
   const char *home, *tmp;

   // $HOME / ~/ etc.
   home = eina_environment_home_get();
   if (!home)
     {
        char *home2 = _fallback_home_dir();
        _eina_vpath_data_add("home", home2);
        free(home2);
     }
   else
     _eina_vpath_data_add("home", home);

   // tmp dir - system wide
   tmp = eina_environment_tmp_get();
   _eina_vpath_data_add("tmp", tmp);
}

Eina_Bool
eina_vpath_init(void)
{
   vpath_data = eina_hash_string_superfast_new((Eina_Free_Cb)eina_stringshare_del);

   _eina_vpath_interface_sys_init();
   eina_xdg_env_init();

   _eina_vpath_log_dom = eina_log_domain_register("vpath", "cyan");
   return EINA_TRUE;
}

Eina_Bool
eina_vpath_shutdown(void)
{
   eina_hash_free(vpath_data);
   vpath_data = NULL;
   eina_log_domain_unregister(_eina_vpath_log_dom);
   _eina_vpath_log_dom = -1;
   return EINA_TRUE;
}

#ifdef HAVE_GETPWENT
static Eina_Bool
_fetch_user_homedir(char **str, const char *name, const char *error)
{
  *str = NULL;
  struct passwd *pwent;

  pwent = getpwnam(name);
  if (!pwent)
    {
       ERR("User %s not found\nThe string was: %s", name, error);
       return EINA_FALSE;
    }
  *str = pwent->pw_dir;

  return EINA_TRUE;
}
#endif

static int
_eina_vpath_resolve(const char *path, char *str, size_t size)
{
   if (path[0] == '~')
     {
        char *home = NULL;
        // ~/ <- home directory
        if (path[1] == '/')
          {
             home = eina_hash_find(vpath_data, "home");
             path ++;
          }
        // ~username/ <- homedir of user "username"
        else
          {
#ifndef HAVE_GETPWENT
             ERR("User fetching is disabled on this system\nThe string was: %s", path);
             return 0;
#else
             const char *p;
             char *name;

             for (p = path + 1; *p; p++)
               {
                  if (*p =='/') break;
               }
             name = alloca(p - path);
             strncpy(name, path + 1, p - path - 1);
             name[p - path - 1] = 0;

             if (!_fetch_user_homedir(&home, name, path))
               return 0;
             path = p;
#endif
           }
         if (home)
           {
              return snprintf(str, size, "%s%s", home, path);
           }
     }
   // (:xxx:)/* ... <- meta hash table
   else if (((path[0] == '(') && (path[1] == ':')) ||
            ((path[0] == '$') && (path[1] == '{')))
     {
        const char *p, *end, *meta;
        const char *msg_start, *msg_end;
        char *name;
        int offset;
        Eina_Bool found = EINA_FALSE;

        if (path[0] == '(')
          {
             end = p = strstr(path + 2, ":)");
             offset = 2;
             msg_start = "(:";
             msg_end = ":)";
          }
        else
          {
             end = p = strchr(path + 2, '}');
             offset = 1;
             msg_start = "${";
             msg_end = "}";
          }
        if (p) found = EINA_TRUE;
        p += offset;

        if (!found)
          {
             ERR("'%s' Needs to have a matching '%s'\nThe string was: %s", msg_start, msg_end, path);
             return 0;
          }

        if (*p != '/')
          {
             ERR("A / is expected after '%s'\nThe string was: %s", msg_end, path);
             return 0;
          }

        if (found)
          {
             name = alloca(end - path);
             strncpy(name, path + 2, end - path - offset);
             name[end - path - 2] = 0;
             meta = _eina_vpath_data_get(name);
             if (meta)
               {
                  return snprintf(str, size, "%s%s", meta, end + offset);
               }
             else
               {
                  ERR("Meta key '%s' was not registered!\nThe string was: %s", name, path);
                  return 0;
               }
          }
     }
   //just return the path, since we assume that this is a normal path
   else
     {
        return snprintf(str, size, "%s", path);
     }
   str[0] = '\0';
   return 0;
}

EINA_API char *
eina_vpath_resolve(const char* path)
{
   char buf[PATH_MAX];
   EINA_SAFETY_ON_NULL_RETURN_VAL(path, NULL);

   if (_eina_vpath_resolve(path, buf, sizeof(buf)) > 0)
     return strdup(buf);
   return NULL;
}

EINA_API int
eina_vpath_resolve_snprintf(char *str, size_t size, const char *format, ...)
{
   va_list args;
   char *path;
   int r;

   // XXX: implement parse of path then look up in hash if not just create
   // object where path and result are the same and return that with
   // path set and result set to resolved path - return obj handler calls
   // "do" on object to get the result inside fetched or failed callback.
   // if it's a url then we need a new classs that overrides the do and
   // begins a fetch and on finish calls the event cb or when wait is called
   /* FIXME: not working for WIndows */
   // /* <- full path

   path = alloca(size + 1);

   va_start(args, format);
   vsnprintf(path, size, format, args);
   va_end(args);

   r = _eina_vpath_resolve(path, str, size);
   if (r > 0) return r;

   ERR("The path has to start with either '~/' or '(:NAME:)/' or be a normal path \nThe string was: %s", path);

   return 0;
}

EINA_API void
eina_vpath_interface_app_set(const char *app_domain, Eina_Prefix *app_pfx)
{
   char buf[PATH_MAX];

   EINA_SAFETY_ON_NULL_RETURN(app_domain);
   EINA_SAFETY_ON_NULL_RETURN(app_pfx);

   _eina_vpath_data_add("app.dir", eina_prefix_get(app_pfx));
   _eina_vpath_data_add("app.bin", eina_prefix_bin_get(app_pfx));
   _eina_vpath_data_add("app.lib", eina_prefix_lib_get(app_pfx));
   _eina_vpath_data_add("app.data", eina_prefix_data_get(app_pfx));
   _eina_vpath_data_add("app.locale", eina_prefix_locale_get(app_pfx));
   snprintf(buf, sizeof(buf), "%s/%s",
            _eina_vpath_data_get("usr.config"), app_domain);
   _eina_vpath_data_add("app.config", buf);
   snprintf(buf, sizeof(buf), "%s/%s",
            _eina_vpath_data_get("usr.cache"), app_domain);
   _eina_vpath_data_add("app.cache", buf);
   snprintf(buf, sizeof(buf), "%s/%s",
            _eina_vpath_data_get("usr.data"), app_domain);
   _eina_vpath_data_add("app.local", buf);
   snprintf(buf, sizeof(buf), "%s/%s",
            _eina_vpath_data_get("usr.tmp"), app_domain);
   _eina_vpath_data_add("app.tmp", buf);
}

EINA_API void
eina_vpath_interface_user_set(Eina_Vpath_Interface_User *user)
{
   Eina_Bool free_run = EINA_FALSE;

   EINA_SAFETY_ON_NULL_RETURN(user);

   if (!user->run)
     {
        user->run = _fallback_runtime_dir(_eina_vpath_data_get("home"));
        free_run = EINA_TRUE;
     }

#define ADD(a) _eina_vpath_data_add("usr." #a , user->a)
   ADD(desktop);
   ADD(documents);
   ADD(downloads);
   ADD(music);
   ADD(pictures);
   ADD(pub);
   ADD(templates);
   ADD(videos);
   ADD(data);
   ADD(config);
   ADD(cache);
   ADD(run);
   ADD(tmp);
#undef ADD

   if (free_run)
     free((char *)user->run);
}