1 //===- llvm/System/Unix/Path.cpp - Unix Path Implementation -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the Unix specific portion of the Path class.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 //=== WARNING: Implementation here must contain only generic UNIX code that
16 //=== is guaranteed to work on all UNIX variants.
17 //===----------------------------------------------------------------------===//
24 Path::is_file() const {
25 if (!empty() && ((*this)[length()-1] != '/'))
31 Path::is_directory() const {
32 if ((!empty()) && ((*this)[length()-1] == '/'))
38 Path::create( bool create_parents) {
39 if ( is_directory() ) {
41 this->create_directories( );
42 this->create_directory( );
43 } else if ( is_file() ) {
45 this->create_directories( );
52 if ( is_directory() ) {
54 this->remove_directory( );
55 } else if ( is_file() )
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 );
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))
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;
89 char * next = index(pathname,'/');
90 if ( pathname[0] == '/')
91 next = index(&pathname[1],'/');
95 if (0 != access(pathname, F_OK | R_OK))
96 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
99 next = index(pathname,'/');
105 Path::remove_directory()
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);
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);
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);