aboutsummaryrefslogtreecommitdiffstats
path: root/src/launcherutil.c
blob: 8df4a7151f82b93d4a2944d90f17b143f3cd1682 (plain) (blame)
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
#include "macros.h"
#include "l2su.h"

#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>

/* handcoded string functions
 *
 * NOTE: I am aware that this is inefficient but since these are used only a handful of times
 * during initialization, I don't think performance is really a huge deal here.
 *
 * remember that this is a Minecraft launcher, so the PC has to meet the minimum specs of that,
 * which are well above the minimum specs of this launcher */

char *l2_launcher_strapp(char *buf, const char *src)
{
  size_t buflen = strlen(buf);
  size_t srclen = strlen(src);

  char *ret = realloc(buf, buflen + srclen + 1);
  if (!ret) return NULL;

  memcpy(ret + buflen, src, srclen);
  ret[buflen + srclen] = '\0'; /* realloc does not initialize like calloc does */

  return ret;
}

char *l2_launcher_find_config_path(void)
{
  /* check for $L2SU_CONFIG */
  char *config = getenv(PROJECT_NAME_UPPER "_CONFIG");
  if (config) {
    return strdup(config);
  }
  
  /* check for $XDG_CONFIG_HOME */
  config = getenv("XDG_CONFIG_HOME");
  if (config) {
    /* want to append '"/" PROJECT_NAME' to our string */
    char *ret = strdup(config);
    if (!ret) return NULL;

    return l2_launcher_strapp(ret, "/" PROJECT_NAME);
  }

  /* check for $HOME/.config */
  config = getenv("HOME");
  if (config) {
    char *ret = strdup(config);
    if (!ret) return NULL;

    return l2_launcher_strapp(ret, "/.config/" PROJECT_NAME);
  }

  /* fail (do NOT attempt to find home directory from passwd */
  return NULL;
}

char *l2_launcher_find_data_path(void)
{
  /* check for $L2SU_DATA */
  char *config = getenv(PROJECT_NAME_UPPER "_DATA");
  if (config) {
    return strdup(config);
  }

  /* check for $XDG_DATA_HOME */
  config = getenv("XDG_DATA_HOME");
  if (config) {
    char *ret = strdup(config);
    if (!ret) return NULL;

    return l2_launcher_strapp(ret, "/" PROJECT_NAME);
  }

  /* check for $HOME/.local/share */
  config = getenv("HOME");
  if (config) {
    char *ret = strdup(config);
    if (!ret) return NULL;

    return l2_launcher_strapp(ret, "/.local/share/" PROJECT_NAME);
  }

  return NULL;
}

int l2_launcher_open_config(const char *path, int flags, mode_t mode)
{
  int conffd = open(l2_state.paths.config, O_RDONLY | O_DIRECTORY);
  if (conffd < 0) return INSTANCE_ERRNO;

  int instfd = openat(conffd, path, flags, mode);
  int en = errno; /* back up errno because close can fail */

  close(conffd);

  errno = en;
  return instfd;
}

/* NOTE: There's no portable (or otherwise - see open(2) BUGS) way to do this without race conditions. */
int l2_launcher_mkdir_parents(const char *path)
{
  if (*path != '/') return -1;

  int reserrno = 0;

  char *pathbuf = strdup(path);
  char *pcurelem = pathbuf;
  if (!pathbuf) return -1;

  struct stat stbuf = { 0 };

  do { /* strtok is off-limits because it smells bad */
    *pcurelem = '/';
    pcurelem = strchr(pcurelem + 1, '/');

    if (pcurelem) {
      *pcurelem = '\0';
    }

    /* now pathbuf contains our truncated path name which may or may not exist */
    if (mkdir(pathbuf, 0755) < 0) {
      if (errno == EEXIST) {
        /* racy: stat the file and continue if it is a directory */
        if (stat(pathbuf, &stbuf) < 0) {
          reserrno = errno;
          goto mdcleanup;
        }

        if (!S_ISDIR(stbuf.st_mode)) {
          reserrno = ENOTDIR;
          goto mdcleanup;
        }
      } else {
        reserrno = errno;
        goto mdcleanup;
      }
    }
  } while (pcurelem);

mdcleanup:

  free(pathbuf);
  if (reserrno != 0) {
    errno = reserrno;
    return -1;
  }

  return 0;
}