summaryrefslogtreecommitdiffstats
path: root/llvm/lib/System/linux/Path.cpp
blob: a0eed86fe20f5af8427610f900d7244c8dd375d2 (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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
//===- Path.cpp - Path Operating System Concept -----------------*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// Copyright (C) 2004 eXtensible Systems, Inc. All Rights Reserved.
//
// This program is open source software; you can redistribute it and/or modify
// it under the terms of the University of Illinois Open Source License. See
// LICENSE.TXT (distributed with this software) for details.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
// or FITNESS FOR A PARTICULAR PURPOSE.
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
/// @file lib/System/linux/Path.cpp
/// @author Reid Spencer <raspencer@users.sourceforge.net> (original author)
/// @version \verbatim $Id$ \endverbatim
/// @date 2003/04/17
/// @since 1.4
/// @brief Implements the linux specific portion of class llvm::sys::Path
////////////////////////////////////////////////////////////////////////////////

#include "LinuxCommon.h"
#include <sys/stat.h>
#include <fcntl.h>

namespace llvm {
namespace sys {

Path::Path(ConstructSpecial which) throw() {
  switch (which) {
    case CONSTRUCT_TEMP_FILE:
      this->make_temp_file();
      break;
    case CONSTRUCT_TEMP_DIR:
      this->make_temp_directory();
      break;
  }
}

bool 
Path::is_file() const throw() {
  if ( !empty() && ((*this)[length()-1] !=  '/' ))
    return true;
  return false;
}

bool 
Path::is_directory() const throw() {
  if ((!empty()) && ((*this)[length()-1] ==  '/' ))
      return true;
  return false;
}

ErrorCode 
Path::make_directory() throw() {
  char end[2];
  end[0] = '/';
  end[1] = 0;
  if ( empty() )
    this->assign( end );
  else if ( (*this)[length()-1] != '/' )
    this->append( end );
  return NOT_AN_ERROR;
}

ErrorCode
Path::make_temp_directory() throw() {
  char temp_name[64];
  ::strcpy(temp_name,"/tmp/llvm_XXXXXX");
  char* res = ::mkdtemp(temp_name);
  if ( res == 0 )
    RETURN_ERRNO;
  *this = temp_name;
  make_directory();
  return NOT_AN_ERROR;
}

ErrorCode
Path::make_file() throw() {
  if ( (*this)[length()-1] == '/' )
    this->erase( this->length()-1, 1 );
  return NOT_AN_ERROR;
}

ErrorCode
Path::make_temp_file() throw() {
  char temp_name[64];
  ::strcpy(temp_name,"/tmp/llvm_XXXXXX");
  int fd = ::mkstemp(temp_name);
  if ( fd == -1 )
    RETURN_ERRNO;
  ::close(fd);
  *this = temp_name;
  return NOT_AN_ERROR;
}

ErrorCode
Path::exists() throw() {
  char pathname[MAXPATHLEN];
  this->fill(pathname,MAXPATHLEN);
  int lastchar = this->length() - 1 ; 
  if (pathname[lastchar] == '/') 
    pathname[lastchar] = 0;
  RETURN_ON_ERROR( access,( pathname, F_OK | R_OK ) );
  return NOT_AN_ERROR;
}

ErrorCode
Path::create_directory() throw() {
  char pathname[MAXPATHLEN];
  this->fill(pathname,MAXPATHLEN);
  int lastchar = this->length() - 1 ; 
  if (pathname[lastchar] == '/') 
    pathname[lastchar] = 0;
  RETURN_ON_ERROR( mkdir, ( pathname, S_IRWXU | S_IRWXG ) );
  return NOT_AN_ERROR;
}

ErrorCode
Path::create_directories() throw() {
  char pathname[MAXPATHLEN];
  this->fill(pathname,MAXPATHLEN);
  int lastchar = this->length() - 1 ; 
  if (pathname[lastchar] == '/') 
    pathname[lastchar] = 0;
  char * next = index(pathname,'/');
  if ( pathname[0] == '/' ) 
    next = index(&pathname[1],'/');
  while ( next != 0 )
  {
    *next = 0;
    if ( 0 != access( pathname, F_OK | R_OK ) )
      RETURN_ON_ERROR( mkdir, (pathname, S_IRWXU | S_IRWXG ) );
    char* save = next;
    next = index(pathname,'/');
    *save = '/';
  }
  return NOT_AN_ERROR;
}

ErrorCode
Path::remove_directory( ) throw() {
  char pathname[MAXPATHLEN];
  this->fill(pathname,MAXPATHLEN);
  int lastchar = this->length() - 1 ; 
  if (pathname[lastchar] == '/') 
    pathname[lastchar] = 0;
  RETURN_ON_ERROR( rmdir, (pathname) );
  return NOT_AN_ERROR;
}

ErrorCode
Path::create_file( ) throw() {
  char pathname[MAXPATHLEN];
  this->fill(pathname,MAXPATHLEN);
  int lastchar = this->length() - 1 ; 
  if (pathname[lastchar] == '/') 
    pathname[lastchar] = 0;
  RETURN_ON_ERROR( creat, ( pathname, S_IRUSR | S_IWUSR ) );
  return NOT_AN_ERROR;
}

ErrorCode
Path::remove_file( ) throw() {
  char pathname[MAXPATHLEN];
  this->fill(pathname,MAXPATHLEN);
  int lastchar = this->length() - 1 ; 
  if (pathname[lastchar] == '/') 
    pathname[lastchar] = 0;
  RETURN_ON_ERROR( unlink, (pathname) );
  return NOT_AN_ERROR;
}

ErrorCode
Path::find_lib( const char * file ) throw()
{
    ASSERT_ARG( file != 0 );

#if 0
  ACE_TCHAR tempcopy[MAXPATHLEN + 1];
  ACE_TCHAR searchpathname[MAXPATHLEN + 1];
  ACE_TCHAR searchfilename[MAXPATHLEN + 1];

  // Create a copy of filename to work with.
  if (ACE_OS::strlen (filename) + 1
      > (sizeof tempcopy / sizeof (ACE_TCHAR)))
    {
      errno = ENOMEM;
      return -1;
    }
  else
    ACE_OS::strcpy (tempcopy, filename);

  // Insert canonical directory separators.
  ACE_TCHAR *separator_ptr;

#if (ACE_DIRECTORY_SEPARATOR_CHAR != '/')
  // Make all the directory separators "canonical" to simplify
  // subsequent code.
  ACE_Lib_Find::strrepl (tempcopy, ACE_DIRECTORY_SEPARATOR_CHAR, '/');
#endif /* ACE_DIRECTORY_SEPARATOR_CHAR */

  // Separate filename from pathname.
  separator_ptr = ACE_OS::strrchr (tempcopy, '/');

  // This is a relative path.
  if (separator_ptr == 0)
    {
      searchpathname[0] = '\0';
      ACE_OS::strcpy (searchfilename, tempcopy);
    }
  else // This is an absolute path.
    {
      ACE_OS::strcpy (searchfilename, separator_ptr + 1);
      separator_ptr[1] = '\0';
      ACE_OS::strcpy (searchpathname, tempcopy);
    }

  int got_suffix = 0;

  // Check to see if this has an appropriate DLL suffix for the OS
  // platform.
  ACE_TCHAR *s = ACE_OS::strrchr (searchfilename, '.');

  const ACE_TCHAR *dll_suffix = ACE_DLL_SUFFIX;

  if (s != 0)
    {
      // If we have a dot, we have a suffix
      got_suffix = 1;

      // Check whether this matches the appropriate platform-specific
      // suffix.
      if (ACE_OS::strcmp (s, dll_suffix) != 0)
        {
          ACE_ERROR ((LM_WARNING,
                      ACE_LIB_TEXT ("Warning: improper suffix for a ")
                      ACE_LIB_TEXT ("shared library on this platform: %s\n"),
                      s));
        }
    }

  // Make sure we've got enough space in searchfilename.
  if (ACE_OS::strlen (searchfilename)
      + ACE_OS::strlen (ACE_DLL_PREFIX)
      + got_suffix ? 0 : ACE_OS::strlen (dll_suffix) >= (sizeof searchfilename /
                                                         sizeof (ACE_TCHAR)))
    {
      errno = ENOMEM;
      return -1;
    }

      // Use absolute pathname if there is one.
      if (ACE_OS::strlen (searchpathname) > 0)
        {
          if (ACE_OS::strlen (searchfilename)
              + ACE_OS::strlen (searchpathname) >= maxpathnamelen)
            {
              errno = ENOMEM;
              return -1;
            }
          else
            {
#if (ACE_DIRECTORY_SEPARATOR_CHAR != '/')
              // Revert to native path name separators.
              ACE_Lib_Find::strrepl (searchpathname,
		      '/',
                                     ACE_DIRECTORY_SEPARATOR_CHAR);
#endif /* ACE_DIRECTORY_SEPARATOR_CHAR */
              // First, try matching the filename *without* adding a
              // prefix.
#if defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS)
              ACE_OS::sprintf (pathname,
                               ACE_LIB_TEXT ("%s%s%s"),
                               searchpathname,
                               searchfilename,
                               got_suffix ? ACE_static_cast (ACE_TCHAR *,
                                                             ACE_LIB_TEXT (""))
                               : ACE_static_cast (ACE_TCHAR *,
                                                  dll_suffix));
#else /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
              ACE_OS::sprintf (pathname,
                               ACE_LIB_TEXT ("%s%s%s"),
                               searchpathname,
                               searchfilename,
                               got_suffix ? ACE_LIB_TEXT ("") : dll_suffix);
#endif /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
              if (ACE_OS::access (pathname, F_OK) == 0)
                return 0;

              // Second, try matching the filename *with* adding a prefix.
#if defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS)
              ACE_OS::sprintf (pathname,
                               ACE_LIB_TEXT ("%s%s%s%s"),
                               searchpathname,
                               ACE_DLL_PREFIX,
                               searchfilename,
                               got_suffix ? ACE_static_cast (ACE_TCHAR *,
                                                             ACE_LIB_TEXT (""))
                               : ACE_static_cast (ACE_TCHAR *,
                                                  dll_suffix));
#else /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
              ACE_OS::sprintf (pathname,
                               ACE_LIB_TEXT ("%s%s%s%s"),
                               searchpathname,
                               ACE_DLL_PREFIX,
                               searchfilename,
                               got_suffix ? ACE_LIB_TEXT ("") : dll_suffix);
#endif /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
              if (ACE_OS::access (pathname, F_OK) == 0)
                return 0;
            }
        }

      // Use relative filenames via LD_LIBRARY_PATH or PATH (depending on
      // OS platform).
      else
        {
          ACE_TCHAR *ld_path =
#if defined ACE_DEFAULT_LD_SEARCH_PATH
            ACE_DEFAULT_LD_SEARCH_PATH;
#else
            ACE_OS::getenv (ACE_LD_SEARCH_PATH);
#endif /* ACE_DEFAULT_LD_SEARCH_PATH */

          if (ld_path != 0
              && (ld_path = ACE_OS::strdup (ld_path)) != 0)
            {
              // strtok has the strange behavior of not separating the
              // string ":/foo:/bar" into THREE tokens.  One would expect
              // that the first iteration the token would be an empty
              // string, the second iteration would be "/foo", and the
              // third iteration would be "/bar".  However, this is not
              // the case; one only gets two iterations: "/foo" followed
              // by "/bar".

              // This is especially a problem in parsing Unix paths
              // because it is permissible to specify 'the current
              // directory' as an empty entry.  So, we introduce the
              // following special code to cope with this:

              // Look at each dynamic lib directory in the search path.

              ACE_TCHAR *nextholder = 0;
              const ACE_TCHAR *path_entry =
                ACE_Lib_Find::strsplit_r (ld_path,
                                          ACE_LD_SEARCH_PATH_SEPARATOR_STR,
                                          nextholder);
              int result = 0;

              for (;;)
                {
                  // Check if at end of search path.
                  if (path_entry == 0)
                    {
                      errno = ENOENT;
                      result = -1;
                      break;
                    }
                  else if (ACE_OS::strlen (path_entry)
                           + 1
                           + ACE_OS::strlen (searchfilename)
                           >= maxpathnamelen)
                    {
                      errno = ENOMEM;
                      result = -1;
                      break;
                    }
                  // This works around the issue where a path might have
                  // an empty component indicating 'current directory'.
                  // We need to do it here rather than anywhere else so
                  // that the loop condition will still work.
                  else if (path_entry[0] == '\0')
                    path_entry = ACE_LIB_TEXT (".");

                  // First, try matching the filename *without* adding a
                  // prefix.
#if defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS)
                  ACE_OS::sprintf (pathname,
                                   ACE_LIB_TEXT ("%s%c%s%s"),
                                   path_entry,
                                   ACE_DIRECTORY_SEPARATOR_CHAR,
                                   searchfilename,
                                   got_suffix ? ACE_static_cast (ACE_TCHAR *,
                                                                 ACE_LIB_TEXT (""))
                                   : ACE_static_cast (ACE_TCHAR *,
                                                      dll_suffix));
#else /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
                  ACE_OS::sprintf (pathname,
                                   ACE_LIB_TEXT ("%s%c%s%s"),
                                   path_entry,
                                   ACE_DIRECTORY_SEPARATOR_CHAR,
                                   searchfilename,
                                   got_suffix ? ACE_LIB_TEXT ("") : dll_suffix);
#endif /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
                  if (ACE_OS::access (pathname, F_OK) == 0)
                    break;

                  // Second, try matching the filename *with* adding a
                  // prefix.
#if defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS)
                  ACE_OS::sprintf (pathname,
                                   ACE_LIB_TEXT ("%s%c%s%s%s"),
                                   path_entry,
                                   ACE_DIRECTORY_SEPARATOR_CHAR,
                                   ACE_DLL_PREFIX,
                                   searchfilename,
                                   got_suffix ? ACE_static_cast (ACE_TCHAR *,
                                                                 ACE_LIB_TEXT (""))
                                   : ACE_static_cast (ACE_TCHAR *,
                                                      dll_suffix));
#else /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
                  ACE_OS::sprintf (pathname,
                                   ACE_LIB_TEXT ("%s%c%s%s%s"),
                                   path_entry,
                                   ACE_DIRECTORY_SEPARATOR_CHAR,
                                   ACE_DLL_PREFIX,
                                   searchfilename,
                                   got_suffix ? ACE_LIB_TEXT ("") : dll_suffix);
#endif /* ! defined (ACE_HAS_BROKEN_CONDITIONAL_STRING_CASTS) */
                  if (ACE_OS::access (pathname, F_OK) == 0)
                    break;

                  // Fetch the next item in the path
                  path_entry = ACE_Lib_Find::strsplit_r (0,
                                                         ACE_LD_SEARCH_PATH_SEPARATOR_STR,
                                                         nextholder);
                }

              ACE_OS::free ((void *) ld_path);
                return result;
            }
        }
  errno = ENOENT;
  return -1;
#endif

    /// @todo FIXME: Convert ACE code
    *this = Path( file ); 
    
    return NOT_AN_ERROR;
}

}
}

// vim: sw=2 smartindent smarttab tw=80 autoindent expandtab
OpenPOWER on IntegriCloud