~ps10gel/ubuntu/xenial/trafficserver/6.2.0

« back to all changes in this revision

Viewing changes to iocore/utils/Layout.cc

  • Committer: Bazaar Package Importer
  • Author(s): Arno Toell
  • Date: 2011-01-13 11:49:18 UTC
  • Revision ID: james.westby@ubuntu.com-20110113114918-vu422h8dknrgkj15
Tags: upstream-2.1.5-unstable
ImportĀ upstreamĀ versionĀ 2.1.5-unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** @file
 
2
 
 
3
  A brief file description
 
4
 
 
5
  @section license License
 
6
 
 
7
  Licensed to the Apache Software Foundation (ASF) under one
 
8
  or more contributor license agreements.  See the NOTICE file
 
9
  distributed with this work for additional information
 
10
  regarding copyright ownership.  The ASF licenses this file
 
11
  to you under the Apache License, Version 2.0 (the
 
12
  "License"); you may not use this file except in compliance
 
13
  with the License.  You may obtain a copy of the License at
 
14
 
 
15
      http://www.apache.org/licenses/LICENSE-2.0
 
16
 
 
17
  Unless required by applicable law or agreed to in writing, software
 
18
  distributed under the License is distributed on an "AS IS" BASIS,
 
19
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
20
  See the License for the specific language governing permissions and
 
21
  limitations under the License.
 
22
 */
 
23
 
 
24
#include "libts.h"
 
25
#include "I_Layout.h"
 
26
 
 
27
static Layout *layout = NULL;
 
28
 
 
29
Layout *
 
30
Layout::get()
 
31
{
 
32
  if (layout == NULL) {
 
33
    ink_assert("need to call create_default_layout before accessing" "default_layout()");
 
34
  }
 
35
  return layout;
 
36
}
 
37
 
 
38
void
 
39
Layout::create(const char *prefix)
 
40
{
 
41
  if (layout == NULL) {
 
42
    layout = NEW(new Layout(prefix));
 
43
  }
 
44
}
 
45
 
 
46
static char *
 
47
layout_relative(const char *root, const char *file)
 
48
{
 
49
  char path[PATH_MAX];
 
50
 
 
51
  if (ink_filepath_merge(path, PATH_MAX, root, file,
 
52
                         INK_FILEPATH_TRUENAME)) {
 
53
    int err = errno;
 
54
    // Log error
 
55
    if (err == EACCES) {
 
56
      ink_error("Cannot merge path '%s' above the root '%s'\n", file, root);
 
57
    } else if (err == E2BIG) {
 
58
      ink_error("Excedding file name length limit of %d characters\n", PATH_MAX);
 
59
    }
 
60
    else {
 
61
      // TODO: Make some pretty errors.
 
62
      ink_error("Cannot merge '%s' with '%s' error=%d\n", file, root, err);
 
63
    }
 
64
    return NULL;
 
65
  }
 
66
  return xstrdup(path);
 
67
}
 
68
 
 
69
char *
 
70
Layout::relative(const char *file)
 
71
{
 
72
  return layout_relative(prefix, file);
 
73
}
 
74
 
 
75
void
 
76
Layout::relative(char *buf, size_t bufsz, const char *file)
 
77
{
 
78
  char path[PATH_MAX];
 
79
 
 
80
  if (ink_filepath_merge(path, PATH_MAX, prefix, file,
 
81
      INK_FILEPATH_TRUENAME)) {
 
82
    int err = errno;
 
83
    // Log error
 
84
    if (err == EACCES) {
 
85
      ink_error("Cannot merge path '%s' above the root '%s'\n", file, prefix);
 
86
    } else if (err == E2BIG) {
 
87
      ink_error("Excedding file name length limit of %d characters\n", PATH_MAX);
 
88
    }
 
89
    else {
 
90
      // TODO: Make some pretty errors.
 
91
      ink_error("Cannot merge '%s' with '%s' error=%d\n", file, prefix, err);
 
92
    }
 
93
    return;
 
94
  }
 
95
  size_t path_len = strlen(path) + 1;
 
96
  if (path_len > bufsz) {
 
97
    ink_error("Provided buffer is too small: %d, required %d\n",
 
98
              bufsz, path_len);
 
99
  }
 
100
  else {
 
101
    strcpy(buf, path);
 
102
  }
 
103
}
 
104
 
 
105
char *
 
106
Layout::relative_to(const char *dir, const char *file)
 
107
{
 
108
  return layout_relative(dir, file);
 
109
}
 
110
 
 
111
void
 
112
Layout::relative_to(char *buf, size_t bufsz, const char *dir, const char *file)
 
113
{
 
114
  char path[PATH_MAX];
 
115
 
 
116
  if (ink_filepath_merge(path, PATH_MAX, dir, file,
 
117
      INK_FILEPATH_TRUENAME)) {
 
118
    int err = errno;
 
119
    // Log error
 
120
    if (err == EACCES) {
 
121
      ink_error("Cannot merge path '%s' above the root '%s'\n", file, dir);
 
122
    } else if (err == E2BIG) {
 
123
      ink_error("Excedding file name length limit of %d characters\n", PATH_MAX);
 
124
    }
 
125
    else {
 
126
      // TODO: Make some pretty errors.
 
127
      ink_error("Cannot merge '%s' with '%s' error=%d\n", file, dir, err);
 
128
    }
 
129
    return;
 
130
  }
 
131
  size_t path_len = strlen(path) + 1;
 
132
  if (path_len > bufsz) {
 
133
    ink_error("Provided buffer is too small: %d, required %d\n",
 
134
              bufsz, path_len);
 
135
  }
 
136
  else {
 
137
    strcpy(buf, path);
 
138
  }
 
139
}
 
140
 
 
141
Layout::Layout(const char *_prefix)
 
142
{
 
143
  if (_prefix) {
 
144
    prefix = xstrdup(_prefix);
 
145
  }
 
146
  else {
 
147
    char *env_path;
 
148
    char path[PATH_MAX];
 
149
    int  len;
 
150
 
 
151
    if ((env_path = getenv("TS_ROOT"))) {
 
152
      len = strlen(env_path);
 
153
      if ((len + 1) > PATH_MAX) {
 
154
        ink_error("TS_ROOT environment variable is too big: %d, max %d\n",
 
155
                  len, PATH_MAX -1);
 
156
        return;
 
157
      }
 
158
      strcpy(path, env_path);
 
159
      while (len > 1 && path[len - 1] == '/') {
 
160
        path[len - 1] = '\0';
 
161
        --len;
 
162
      }
 
163
    } else {
 
164
        // Use compile time --prefix
 
165
        ink_strncpy(path, PREFIX, sizeof(path));
 
166
    }
 
167
 
 
168
    if (access(path, R_OK) == -1) {
 
169
      ink_error("unable to access() TS_ROOT '%s': %d, %s\n",
 
170
                path, errno, strerror(errno));
 
171
      return;
 
172
    }
 
173
    prefix = xstrdup(path);
 
174
  }
 
175
  exec_prefix = layout_relative(prefix, EXEC_PREFIX);
 
176
  bindir = layout_relative(prefix, BINDIR);
 
177
  sbindir = layout_relative(prefix, SBINDIR);
 
178
  sysconfdir = layout_relative(prefix, SYSCONFDIR);
 
179
  datadir = layout_relative(prefix, DATADIR);
 
180
  includedir = layout_relative(prefix, INCLUDEDIR);
 
181
  libdir = layout_relative(prefix, LIBDIR);
 
182
  libexecdir = layout_relative(prefix, LIBEXECDIR);
 
183
  localstatedir = layout_relative(prefix, LOCALSTATEDIR);
 
184
  sharedstatedir = layout_relative(prefix, SHAREDSTATEDIR);
 
185
  runtimedir = layout_relative(prefix, RUNTIMEDIR);
 
186
  logdir = layout_relative(prefix, LOGDIR);
 
187
  mandir = layout_relative(prefix, MANDIR);
 
188
  infodir = layout_relative(prefix, INFODIR);
 
189
  cachedir = layout_relative(prefix, CACHEDIR);
 
190
 
 
191
#ifdef DEBUG
 
192
// TODO: Use a propper Debug logging
 
193
//
 
194
#define PrintSTR(var) \
 
195
  fprintf(stdout, "%18s = '%s'\n", "--" #var, (var == NULL? "NULL" : var));
 
196
 
 
197
  fprintf(stdout, "Layout configuration\n");
 
198
  PrintSTR(prefix);
 
199
  PrintSTR(exec_prefix);
 
200
  PrintSTR(bindir);
 
201
  PrintSTR(sbindir);
 
202
  PrintSTR(sysconfdir);
 
203
  PrintSTR(datadir);
 
204
  PrintSTR(includedir);
 
205
  PrintSTR(libdir);
 
206
  PrintSTR(libexecdir);
 
207
  PrintSTR(localstatedir);
 
208
  PrintSTR(sharedstatedir);
 
209
  PrintSTR(runtimedir);
 
210
  PrintSTR(logdir);
 
211
  PrintSTR(mandir);
 
212
  PrintSTR(infodir);
 
213
  PrintSTR(cachedir);
 
214
#endif
 
215
 
 
216
}
 
217
 
 
218
Layout::~Layout()
 
219
{
 
220
#define SafeFree(x) \
 
221
  if (x) xfree(x);
 
222
 
 
223
  SafeFree(prefix);
 
224
  SafeFree(exec_prefix);
 
225
  SafeFree(bindir);
 
226
  SafeFree(sbindir);
 
227
  SafeFree(sysconfdir);
 
228
  SafeFree(datadir);
 
229
  SafeFree(includedir);
 
230
  SafeFree(libdir);
 
231
  SafeFree(libexecdir);
 
232
  SafeFree(localstatedir);
 
233
  SafeFree(sharedstatedir);
 
234
  SafeFree(runtimedir);
 
235
  SafeFree(logdir);
 
236
  SafeFree(mandir);
 
237
  SafeFree(infodir);
 
238
  SafeFree(cachedir);
 
239
}
 
240