Reid Spencer | b89a223 | 2004-08-25 06:20:07 +0000 | [diff] [blame^] | 1 | //===- llvm/System/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Reid Spencer and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Unix specific portion of the Path class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | //=== WARNING: Implementation here must contain only generic UNIX code that |
| 16 | //=== is guaranteed to work on all UNIX variants. |
| 17 | //===----------------------------------------------------------------------===// |
| 18 | |
| 19 | #include "Unix.h" |
| 20 | #include <sys/stat.h> |
| 21 | #include <fcntl.h> |
| 22 | |
| 23 | bool |
| 24 | Path::is_file() const { |
| 25 | if (!empty() && ((*this)[length()-1] != '/')) |
| 26 | return true; |
| 27 | return false; |
| 28 | } |
| 29 | |
| 30 | bool |
| 31 | Path::is_directory() const { |
| 32 | if ((!empty()) && ((*this)[length()-1] == '/')) |
| 33 | return true; |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | void |
| 38 | Path::create( bool create_parents) { |
| 39 | if ( is_directory() ) { |
| 40 | if ( create_parents ) |
| 41 | this->create_directories( ); |
| 42 | this->create_directory( ); |
| 43 | } else if ( is_file() ) { |
| 44 | if ( create_parents ) |
| 45 | this->create_directories( ); |
| 46 | this->create_file( ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | Path::remove() { |
| 52 | if ( is_directory() ) { |
| 53 | if ( exists() ) |
| 54 | this->remove_directory( ); |
| 55 | } else if ( is_file() ) |
| 56 | if ( exists() ) |
| 57 | this->remove_file( ); |
| 58 | } |
| 59 | |
| 60 | bool |
| 61 | Path::exists() { |
| 62 | char pathname[MAXPATHLEN]; |
| 63 | this->fill(pathname,MAXPATHLEN); |
| 64 | int lastchar = this->length() - 1 ; |
| 65 | if (pathname[lastchar] == '/') |
| 66 | pathname[lastchar] = 0; |
| 67 | return 0 == access(pathname, F_OK ); |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | Path::create_directory( ) { |
| 72 | char pathname[MAXPATHLEN]; |
| 73 | this->fill(pathname,MAXPATHLEN); |
| 74 | int lastchar = this->length() - 1 ; |
| 75 | if (pathname[lastchar] == '/') |
| 76 | pathname[lastchar] = 0; |
| 77 | if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) |
| 78 | ThrowErrno(pathname); |
| 79 | } |
| 80 | |
| 81 | void |
| 82 | Path::create_directories() { |
| 83 | char pathname[MAXPATHLEN]; |
| 84 | this->fill(pathname,MAXPATHLEN); |
| 85 | int lastchar = this->length() - 1 ; |
| 86 | if (pathname[lastchar] == '/') |
| 87 | pathname[lastchar] = 0; |
| 88 | |
| 89 | char * next = index(pathname,'/'); |
| 90 | if ( pathname[0] == '/') |
| 91 | next = index(&pathname[1],'/'); |
| 92 | while ( next != 0 ) |
| 93 | { |
| 94 | *next = 0; |
| 95 | if (0 != access(pathname, F_OK | R_OK)) |
| 96 | if (0 != mkdir(pathname, S_IRWXU | S_IRWXG)) |
| 97 | ThrowErrno(pathname); |
| 98 | char* save = next; |
| 99 | next = index(pathname,'/'); |
| 100 | *save = '/'; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | void |
| 105 | Path::remove_directory() |
| 106 | { |
| 107 | char pathname[MAXPATHLEN]; |
| 108 | this->fill(pathname,MAXPATHLEN); |
| 109 | int lastchar = this->length() - 1 ; |
| 110 | if (pathname[lastchar] == '/') |
| 111 | pathname[lastchar] = 0; |
| 112 | if ( 0 != rmdir(pathname)) |
| 113 | ThrowErrno(pathname); |
| 114 | } |
| 115 | |
| 116 | void |
| 117 | Path::create_file() { |
| 118 | char pathname[MAXPATHLEN]; |
| 119 | this->fill(pathname,MAXPATHLEN); |
| 120 | int lastchar = this->length() - 1 ; |
| 121 | if (pathname[lastchar] == '/') |
| 122 | pathname[lastchar] = 0; |
| 123 | if (0 != creat(pathname, S_IRUSR | S_IWUSR)) |
| 124 | ThrowErrno(pathname); |
| 125 | } |
| 126 | |
| 127 | void |
| 128 | Path::remove_file() { |
| 129 | char pathname[MAXPATHLEN]; |
| 130 | this->fill(pathname,MAXPATHLEN); |
| 131 | int lastchar = this->length() - 1 ; |
| 132 | if (pathname[lastchar] == '/') |
| 133 | pathname[lastchar] = 0; |
| 134 | if (0 != unlink(pathname)) |
| 135 | ThrowErrno(pathname); |
| 136 | } |
| 137 | |
| 138 | // vim: sw=2 |