blob: 84c2d2d23d5cf79152b8f65d4678195950d00c31 [file] [log] [blame]
Reid Spencerb89a2232004-08-25 06:20:07 +00001//===- 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
Reid Spencer8e665952004-08-29 05:24:01 +000016//=== is guaranteed to work on *all* UNIX variants.
Reid Spencerb89a2232004-08-25 06:20:07 +000017//===----------------------------------------------------------------------===//
18
Reid Spencer551ccae2004-09-01 22:55:40 +000019#include <llvm/Config/config.h>
Reid Spencerbe31d2a2004-11-16 17:14:08 +000020#include <llvm/Config/alloca.h>
Reid Spencerb89a2232004-08-25 06:20:07 +000021#include "Unix.h"
22#include <sys/stat.h>
23#include <fcntl.h>
Reid Spencereaf18152004-11-14 22:08:36 +000024#include <utime.h>
25#include <dirent.h>
Reid Spencerb89a2232004-08-25 06:20:07 +000026
Reid Spencer8e665952004-08-29 05:24:01 +000027namespace llvm {
28using namespace sys;
Reid Spencerb89a2232004-08-25 06:20:07 +000029
Reid Spencer732f92d2004-12-13 06:57:15 +000030Path::Path(const std::string& unverified_path)
Reid Spencer8e665952004-08-29 05:24:01 +000031 : path(unverified_path)
Reid Spencerb89a2232004-08-25 06:20:07 +000032{
Reid Spencer8e665952004-08-29 05:24:01 +000033 if (unverified_path.empty())
34 return;
Reid Spencer07adb282004-11-05 22:15:36 +000035 if (this->isValid())
Reid Spencer8e665952004-08-29 05:24:01 +000036 return;
37 // oops, not valid.
38 path.clear();
39 ThrowErrno(unverified_path + ": path is not valid");
Reid Spencerb89a2232004-08-25 06:20:07 +000040}
41
Reid Spencer8e665952004-08-29 05:24:01 +000042Path
43Path::GetRootDirectory() {
44 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +000045 result.setDirectory("/");
Reid Spencer8e665952004-08-29 05:24:01 +000046 return result;
47}
48
Reid Spencer1b6b99b2004-12-13 03:00:51 +000049static void getPathList(const char*path, std::vector<sys::Path>& Paths) {
50 const char* at = path;
51 const char* delim = strchr(at, ':');
52 Path tmpPath;
53 while( delim != 0 ) {
54 std::string tmp(at, size_t(delim-at));
55 if (tmpPath.setDirectory(tmp))
56 if (tmpPath.readable())
57 Paths.push_back(tmpPath);
58 at = delim + 1;
59 delim = strchr(at, ':');
Reid Spencer74e72612004-09-14 00:16:39 +000060 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +000061 if (*at != 0)
62 if (tmpPath.setDirectory(std::string(at)))
63 if (tmpPath.readable())
64 Paths.push_back(tmpPath);
65
Reid Spencer74e72612004-09-14 00:16:39 +000066}
Reid Spencer1b6b99b2004-12-13 03:00:51 +000067void
68Path::GetSystemLibraryPaths(std::vector<sys::Path>& Paths) {
69#ifdef LTDL_SHLIBPATH_VAR
70 char* env_var = getenv(LTDL_SHLIBPATH_VAR);
71 if (env_var != 0) {
72 getPathList(env_var,Paths);
Reid Spencer74e72612004-09-14 00:16:39 +000073 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +000074#endif
75 // FIXME: Should this look at LD_LIBRARY_PATH too?
76 Paths.push_back(sys::Path("/usr/local/lib/"));
77 Paths.push_back(sys::Path("/usr/X11R6/lib/"));
78 Paths.push_back(sys::Path("/usr/lib/"));
79 Paths.push_back(sys::Path("/lib/"));
Reid Spencer74e72612004-09-14 00:16:39 +000080}
81
Reid Spencer1b6b99b2004-12-13 03:00:51 +000082void
83Path::GetBytecodeLibraryPaths(std::vector<sys::Path>& Paths) {
84 char * env_var = getenv("LLVM_LIB_SEARCH_PATH");
85 if (env_var != 0) {
86 getPathList(env_var,Paths);
87 }
88#ifdef LLVMGCCDIR
89 {
Reid Spencerc29befb2004-12-15 01:50:13 +000090 Path tmpPath(std::string(LLVMGCCDIR) + "lib/");
Reid Spencer1b6b99b2004-12-13 03:00:51 +000091 if (tmpPath.readable())
92 Paths.push_back(tmpPath);
93 }
94#endif
95#ifdef LLVM_LIBDIR
96 {
97 Path tmpPath;
98 if (tmpPath.setDirectory(LLVM_LIBDIR))
99 if (tmpPath.readable())
100 Paths.push_back(tmpPath);
101 }
102#endif
103 GetSystemLibraryPaths(Paths);
Reid Spencer8e665952004-08-29 05:24:01 +0000104}
105
106Path
107Path::GetLLVMDefaultConfigDir() {
108 return Path("/etc/llvm/");
109}
110
Reid Spencer8e665952004-08-29 05:24:01 +0000111Path
112Path::GetUserHomeDirectory() {
113 const char* home = getenv("HOME");
114 if (home) {
115 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000116 if (result.setDirectory(home))
Reid Spencer8e665952004-08-29 05:24:01 +0000117 return result;
118 }
119 return GetRootDirectory();
120}
121
122bool
Reid Spencer07adb282004-11-05 22:15:36 +0000123Path::isFile() const {
124 return (isValid() && path[path.length()-1] != '/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000125}
126
127bool
Reid Spencer07adb282004-11-05 22:15:36 +0000128Path::isDirectory() const {
129 return (isValid() && path[path.length()-1] == '/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000130}
131
132std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000133Path::getBasename() const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000134 // Find the last slash
135 size_t slash = path.rfind('/');
136 if (slash == std::string::npos)
137 slash = 0;
138 else
139 slash++;
140
141 return path.substr(slash, path.rfind('.'));
142}
143
Reid Spencer07adb282004-11-05 22:15:36 +0000144bool Path::hasMagicNumber(const std::string &Magic) const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000145 size_t len = Magic.size();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000146 assert(len < 1024 && "Request for magic string too long");
147 char* buf = (char*) alloca(1 + len);
148 int fd = ::open(path.c_str(),O_RDONLY);
149 if (fd < 0)
150 return false;
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000151 size_t read_len = ::read(fd, buf, len);
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000152 close(fd);
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000153 if (len != read_len)
154 return false;
Reid Spencer1b554b42004-09-11 04:55:08 +0000155 buf[len] = '\0';
156 return Magic == buf;
157}
158
Reid Spencereaf18152004-11-14 22:08:36 +0000159bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
160 if (!isFile())
161 return false;
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000162 assert(len < 1024 && "Request for magic string too long");
163 char* buf = (char*) alloca(1 + len);
164 int fd = ::open(path.c_str(),O_RDONLY);
165 if (fd < 0)
166 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000167 ssize_t bytes_read = ::read(fd, buf, len);
168 ::close(fd);
169 if (ssize_t(len) != bytes_read) {
170 Magic.clear();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000171 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000172 }
173 Magic.assign(buf,len);
Reid Spencereaf18152004-11-14 22:08:36 +0000174 return true;
175}
176
Reid Spencer1b554b42004-09-11 04:55:08 +0000177bool
Reid Spencer07adb282004-11-05 22:15:36 +0000178Path::isBytecodeFile() const {
Reid Spencer9195f372004-11-09 20:26:31 +0000179 char buffer[ 4];
180 buffer[0] = 0;
Reid Spencer1fce0912004-12-11 00:14:15 +0000181 int fd = ::open(path.c_str(),O_RDONLY);
182 if (fd < 0)
183 return false;
184 ssize_t bytes_read = ::read(fd, buffer, 4);
185 ::close(fd);
186 if (4 != bytes_read)
187 return false;
Reid Spencereaf18152004-11-14 22:08:36 +0000188
189 return (buffer[0] == 'l' && buffer[1] == 'l' && buffer[2] == 'v' &&
190 (buffer[3] == 'c' || buffer[3] == 'm'));
Reid Spencer1b554b42004-09-11 04:55:08 +0000191}
192
193bool
Reid Spencer8e665952004-08-29 05:24:01 +0000194Path::exists() const {
195 return 0 == access(path.c_str(), F_OK );
196}
197
198bool
199Path::readable() const {
200 return 0 == access(path.c_str(), F_OK | R_OK );
201}
202
203bool
204Path::writable() const {
205 return 0 == access(path.c_str(), F_OK | W_OK );
206}
207
208bool
209Path::executable() const {
210 return 0 == access(path.c_str(), R_OK | X_OK );
211}
212
213std::string
214Path::getLast() const {
215 // Find the last slash
216 size_t pos = path.rfind('/');
217
218 // Handle the corner cases
219 if (pos == std::string::npos)
220 return path;
221
222 // If the last character is a slash
223 if (pos == path.length()-1) {
224 // Find the second to last slash
225 size_t pos2 = path.rfind('/', pos-1);
226 if (pos2 == std::string::npos)
227 return path.substr(0,pos);
228 else
229 return path.substr(pos2+1,pos-pos2-1);
230 }
231 // Return everything after the last slash
232 return path.substr(pos+1);
233}
234
Reid Spencerb608a812004-11-16 06:15:19 +0000235void
236Path::getStatusInfo(StatusInfo& info) const {
Reid Spencer9195f372004-11-09 20:26:31 +0000237 struct stat buf;
238 if (0 != stat(path.c_str(), &buf)) {
239 ThrowErrno(std::string("Can't get status: ")+path);
240 }
241 info.fileSize = buf.st_size;
Reid Spencereaf18152004-11-14 22:08:36 +0000242 info.modTime.fromEpochTime(buf.st_mtime);
Reid Spencer9195f372004-11-09 20:26:31 +0000243 info.mode = buf.st_mode;
244 info.user = buf.st_uid;
245 info.group = buf.st_gid;
Reid Spencereaf18152004-11-14 22:08:36 +0000246 info.isDir = S_ISDIR(buf.st_mode);
247 if (info.isDir && path[path.length()-1] != '/')
248 path += '/';
249}
250
Reid Spencer77cc91d2004-12-13 19:59:50 +0000251static bool AddPermissionBits(const std::string& Filename, int bits) {
252 // Get the umask value from the operating system. We want to use it
253 // when changing the file's permissions. Since calling umask() sets
254 // the umask and returns its old value, we must call it a second
255 // time to reset it to the user's preference.
256 int mask = umask(0777); // The arg. to umask is arbitrary.
257 umask(mask); // Restore the umask.
258
259 // Get the file's current mode.
260 struct stat st;
261 if ((stat(Filename.c_str(), &st)) == -1)
262 return false;
263
264 // Change the file to have whichever permissions bits from 'bits'
265 // that the umask would not disable.
266 if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
267 return false;
268
269 return true;
270}
271
272void Path::makeReadable() {
273 if (!AddPermissionBits(path,0444))
274 ThrowErrno(path + ": can't make file readable");
275}
276
277void Path::makeWriteable() {
278 if (!AddPermissionBits(path,0222))
279 ThrowErrno(path + ": can't make file writable");
280}
281
282void Path::makeExecutable() {
283 if (!AddPermissionBits(path,0111))
284 ThrowErrno(path + ": can't make file executable");
285}
286
Reid Spencereaf18152004-11-14 22:08:36 +0000287bool
Reid Spencerb608a812004-11-16 06:15:19 +0000288Path::getDirectoryContents(std::set<Path>& result) const {
Reid Spencereaf18152004-11-14 22:08:36 +0000289 if (!isDirectory())
290 return false;
291 DIR* direntries = ::opendir(path.c_str());
292 if (direntries == 0)
293 ThrowErrno(path + ": can't open directory");
294
295 result.clear();
296 struct dirent* de = ::readdir(direntries);
297 while (de != 0) {
298 if (de->d_name[0] != '.') {
299 Path aPath(path + (const char*)de->d_name);
300 struct stat buf;
301 if (0 != stat(aPath.path.c_str(), &buf))
302 ThrowErrno(aPath.path + ": can't get status");
303 if (S_ISDIR(buf.st_mode))
304 aPath.path += "/";
Reid Spencerb608a812004-11-16 06:15:19 +0000305 result.insert(aPath);
Reid Spencereaf18152004-11-14 22:08:36 +0000306 }
307 de = ::readdir(direntries);
308 }
309
310 closedir(direntries);
311 return true;
Reid Spencer9195f372004-11-09 20:26:31 +0000312}
313
Reid Spencer8e665952004-08-29 05:24:01 +0000314bool
Reid Spencer07adb282004-11-05 22:15:36 +0000315Path::setDirectory(const std::string& a_path) {
Reid Spencer8e665952004-08-29 05:24:01 +0000316 if (a_path.size() == 0)
317 return false;
318 Path save(*this);
319 path = a_path;
320 size_t last = a_path.size() -1;
Reid Spencer3d595cb2004-12-13 07:51:07 +0000321 if (a_path[last] != '/')
Reid Spencer8e665952004-08-29 05:24:01 +0000322 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000323 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000324 path = save.path;
325 return false;
326 }
327 return true;
328}
329
330bool
Reid Spencer07adb282004-11-05 22:15:36 +0000331Path::setFile(const std::string& a_path) {
Reid Spencer8e665952004-08-29 05:24:01 +0000332 if (a_path.size() == 0)
333 return false;
334 Path save(*this);
335 path = a_path;
336 size_t last = a_path.size() - 1;
337 while (last > 0 && a_path[last] == '/')
338 last--;
339 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000340 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000341 path = save.path;
342 return false;
343 }
344 return true;
345}
346
347bool
Reid Spencer07adb282004-11-05 22:15:36 +0000348Path::appendDirectory(const std::string& dir) {
349 if (isFile())
Reid Spencer8e665952004-08-29 05:24:01 +0000350 return false;
351 Path save(*this);
352 path += dir;
353 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000354 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000355 path = save.path;
356 return false;
357 }
358 return true;
359}
360
361bool
Reid Spencer07adb282004-11-05 22:15:36 +0000362Path::elideDirectory() {
363 if (isFile())
Reid Spencer8e665952004-08-29 05:24:01 +0000364 return false;
365 size_t slashpos = path.rfind('/',path.size());
366 if (slashpos == 0 || slashpos == std::string::npos)
367 return false;
368 if (slashpos == path.size() - 1)
369 slashpos = path.rfind('/',slashpos-1);
370 if (slashpos == std::string::npos)
371 return false;
372 path.erase(slashpos);
373 return true;
374}
375
376bool
Reid Spencer07adb282004-11-05 22:15:36 +0000377Path::appendFile(const std::string& file) {
378 if (!isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000379 return false;
380 Path save(*this);
381 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000382 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000383 path = save.path;
384 return false;
385 }
386 return true;
387}
388
389bool
Reid Spencer07adb282004-11-05 22:15:36 +0000390Path::elideFile() {
391 if (isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000392 return false;
393 size_t slashpos = path.rfind('/',path.size());
394 if (slashpos == std::string::npos)
395 return false;
396 path.erase(slashpos+1);
397 return true;
398}
399
400bool
Reid Spencer07adb282004-11-05 22:15:36 +0000401Path::appendSuffix(const std::string& suffix) {
402 if (isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000403 return false;
404 Path save(*this);
405 path.append(".");
406 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000407 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000408 path = save.path;
409 return false;
410 }
411 return true;
412}
413
414bool
Reid Spencer07adb282004-11-05 22:15:36 +0000415Path::elideSuffix() {
416 if (isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000417 size_t dotpos = path.rfind('.',path.size());
418 size_t slashpos = path.rfind('/',path.size());
419 if (slashpos != std::string::npos && dotpos != std::string::npos &&
420 dotpos > slashpos) {
421 path.erase(dotpos, path.size()-dotpos);
422 return true;
423 }
424 return false;
425}
426
427
428bool
Reid Spencer07adb282004-11-05 22:15:36 +0000429Path::createDirectory( bool create_parents) {
Reid Spencer8e665952004-08-29 05:24:01 +0000430 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000431 if (!isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000432
433 // Get a writeable copy of the path name
434 char pathname[MAXPATHLEN];
435 path.copy(pathname,MAXPATHLEN);
436
437 // Null-terminate the last component
438 int lastchar = path.length() - 1 ;
439 if (pathname[lastchar] == '/')
440 pathname[lastchar] = 0;
Reid Spencereaf18152004-11-14 22:08:36 +0000441 else
442 pathname[lastchar+1] = 0;
Reid Spencer8e665952004-08-29 05:24:01 +0000443
444 // If we're supposed to create intermediate directories
445 if ( create_parents ) {
446 // Find the end of the initial name component
447 char * next = strchr(pathname,'/');
448 if ( pathname[0] == '/')
449 next = strchr(&pathname[1],'/');
450
451 // Loop through the directory components until we're done
452 while ( next != 0 ) {
453 *next = 0;
454 if (0 != access(pathname, F_OK | R_OK | W_OK))
455 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
456 ThrowErrno(std::string(pathname) + ": Can't create directory");
457 char* save = next;
Reid Spencereaf18152004-11-14 22:08:36 +0000458 next = strchr(next+1,'/');
Reid Spencer8e665952004-08-29 05:24:01 +0000459 *save = '/';
460 }
Reid Spencer8e665952004-08-29 05:24:01 +0000461 }
Reid Spencereaf18152004-11-14 22:08:36 +0000462
463 if (0 != access(pathname, F_OK | R_OK))
464 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
465 ThrowErrno(std::string(pathname) + ": Can't create directory");
Reid Spencer8e665952004-08-29 05:24:01 +0000466 return true;
467}
468
469bool
Reid Spencer07adb282004-11-05 22:15:36 +0000470Path::createFile() {
Reid Spencer8e665952004-08-29 05:24:01 +0000471 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000472 if (!isFile()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000473
474 // Create the file
Reid Spencer622e2202004-09-18 19:25:11 +0000475 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
476 if (fd < 0)
Reid Spencer9195f372004-11-09 20:26:31 +0000477 ThrowErrno(path + ": Can't create file");
Reid Spencer622e2202004-09-18 19:25:11 +0000478 ::close(fd);
Reid Spencer8e665952004-08-29 05:24:01 +0000479
480 return true;
Reid Spencerb89a2232004-08-25 06:20:07 +0000481}
482
Reid Spencer8e665952004-08-29 05:24:01 +0000483bool
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000484Path::createTemporaryFile(bool reuse_current) {
Reid Spencer9195f372004-11-09 20:26:31 +0000485 // Make sure we're dealing with a file
Reid Spencerc29befb2004-12-15 01:50:13 +0000486 if (!isFile())
487 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000488
Reid Spencerc29befb2004-12-15 01:50:13 +0000489 // Make this into a unique file name
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000490 makeUnique( reuse_current );
Reid Spencerc29befb2004-12-15 01:50:13 +0000491
492 // create the file
493 int outFile = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
494 if (outFile != -1) {
495 ::close(outFile);
496 return true;
Reid Spencer9195f372004-11-09 20:26:31 +0000497 }
Reid Spencerc29befb2004-12-15 01:50:13 +0000498 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000499}
500
501bool
Reid Spencer07adb282004-11-05 22:15:36 +0000502Path::destroyDirectory(bool remove_contents) {
Reid Spencer8e665952004-08-29 05:24:01 +0000503 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000504 if (!isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000505
506 // If it doesn't exist, we're done.
507 if (!exists()) return true;
508
509 if (remove_contents) {
510 // Recursively descend the directory to remove its content
511 std::string cmd("/bin/rm -rf ");
512 cmd += path;
513 system(cmd.c_str());
514 } else {
515 // Otherwise, try to just remove the one directory
516 char pathname[MAXPATHLEN];
517 path.copy(pathname,MAXPATHLEN);
518 int lastchar = path.length() - 1 ;
519 if (pathname[lastchar] == '/')
520 pathname[lastchar] = 0;
Reid Spencereaf18152004-11-14 22:08:36 +0000521 else
522 pathname[lastchar+1] = 0;
Reid Spencer8e665952004-08-29 05:24:01 +0000523 if ( 0 != rmdir(pathname))
524 ThrowErrno(std::string(pathname) + ": Can't destroy directory");
525 }
526 return true;
527}
528
529bool
Reid Spencer07adb282004-11-05 22:15:36 +0000530Path::destroyFile() {
531 if (!isFile()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000532 if (0 != unlink(path.c_str()))
Reid Spencereaf18152004-11-14 22:08:36 +0000533 ThrowErrno(path + ": Can't destroy file");
534 return true;
535}
536
537bool
538Path::renameFile(const Path& newName) {
539 if (!isFile()) return false;
540 if (0 != rename(path.c_str(), newName.c_str()))
Reid Spencer1fce0912004-12-11 00:14:15 +0000541 ThrowErrno(std::string("can't rename ") + path + " as " +
542 newName.toString());
Reid Spencereaf18152004-11-14 22:08:36 +0000543 return true;
544}
545
546bool
547Path::setStatusInfo(const StatusInfo& si) const {
548 if (!isFile()) return false;
549 struct utimbuf utb;
550 utb.actime = si.modTime.toPosixTime();
551 utb.modtime = utb.actime;
552 if (0 != ::utime(path.c_str(),&utb))
553 ThrowErrno(path + ": can't set file modification time");
554 if (0 != ::chmod(path.c_str(),si.mode))
555 ThrowErrno(path + ": can't set mode");
Reid Spencer8e665952004-08-29 05:24:01 +0000556 return true;
557}
558
Reid Spencerc29befb2004-12-15 01:50:13 +0000559void
560CopyFile(const sys::Path &Dest, const sys::Path &Src) {
561 int inFile = -1;
562 int outFile = -1;
563 try {
564 inFile = ::open(Src.c_str(), O_RDONLY);
565 if (inFile == -1)
566 ThrowErrno("Cannnot open source file to copy: " + Src.toString());
567
568 outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
569 if (outFile == -1)
570 ThrowErrno("Cannnot create destination file for copy: " +Dest.toString());
571
572 char Buffer[16*1024];
573 while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
574 if (Amt == -1) {
575 if (errno != EINTR && errno != EAGAIN)
576 ThrowErrno("Can't read source file: " + Src.toString());
577 } else {
578 char *BufPtr = Buffer;
579 while (Amt) {
580 ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
581 if (AmtWritten == -1) {
582 if (errno != EINTR && errno != EAGAIN)
583 ThrowErrno("Can't write destination file: " + Dest.toString());
584 } else {
585 Amt -= AmtWritten;
586 BufPtr += AmtWritten;
587 }
588 }
589 }
590 }
591 ::close(inFile);
592 ::close(outFile);
593 } catch (...) {
594 if (inFile != -1)
595 ::close(inFile);
596 if (outFile != -1)
597 ::close(outFile);
598 throw;
599 }
600}
601
602void
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000603Path::makeUnique(bool reuse_current) {
604 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000605 return; // File doesn't exist already, just use it!
606
607 // Append an XXXXXX pattern to the end of the file for use with mkstemp,
608 // mktemp or our own implementation.
609 char *FNBuffer = (char*) alloca(path.size()+8);
610 path.copy(FNBuffer,path.size());
611 strcpy(FNBuffer+path.size(), "-XXXXXX");
612
613#if defined(HAVE_MKSTEMP)
614 int TempFD;
615 if ((TempFD = mkstemp(FNBuffer)) == -1) {
616 ThrowErrno("Cannot make unique filename for '" + path + "'");
617 }
618
619 // We don't need to hold the temp file descriptor... we will trust that no one
620 // will overwrite/delete the file before we can open it again.
621 close(TempFD);
622
623 // Save the name
624 path = FNBuffer;
625#elif defined(HAVE_MKTEMP)
626 // If we don't have mkstemp, use the old and obsolete mktemp function.
627 if (mktemp(FNBuffer) == 0) {
628 ThrowErrno("Cannot make unique filename for '" + path + "'");
629 }
630
631 // Save the name
632 path = FNBuffer;
633#else
634 // Okay, looks like we have to do it all by our lonesome.
635 static unsigned FCounter = 0;
636 unsigned offset = path.size() + 1;
637 while ( FCounter < 999999 && exists()) {
638 sprintf(FNBuffer+offset,"%06u",++FCounter);
639 path = FNBuffer;
640 }
641 if (FCounter > 999999)
642 throw std::string("Cannot make unique filename for '" + path + "'");
643#endif
644
645}
Reid Spencerb89a2232004-08-25 06:20:07 +0000646}
647
648// vim: sw=2