blob: a78b513c1184568bb59fbf8014531788997ee551 [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 }
Reid Spencer1b6b99b2004-12-13 03:00:51 +000088#ifdef LLVM_LIBDIR
89 {
90 Path tmpPath;
91 if (tmpPath.setDirectory(LLVM_LIBDIR))
92 if (tmpPath.readable())
93 Paths.push_back(tmpPath);
94 }
95#endif
96 GetSystemLibraryPaths(Paths);
Reid Spencer8e665952004-08-29 05:24:01 +000097}
98
99Path
100Path::GetLLVMDefaultConfigDir() {
101 return Path("/etc/llvm/");
102}
103
Reid Spencer8e665952004-08-29 05:24:01 +0000104Path
105Path::GetUserHomeDirectory() {
106 const char* home = getenv("HOME");
107 if (home) {
108 Path result;
Reid Spencer07adb282004-11-05 22:15:36 +0000109 if (result.setDirectory(home))
Reid Spencer8e665952004-08-29 05:24:01 +0000110 return result;
111 }
112 return GetRootDirectory();
113}
114
115bool
Reid Spencer07adb282004-11-05 22:15:36 +0000116Path::isFile() const {
117 return (isValid() && path[path.length()-1] != '/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000118}
119
120bool
Reid Spencer07adb282004-11-05 22:15:36 +0000121Path::isDirectory() const {
122 return (isValid() && path[path.length()-1] == '/');
Reid Spencer1b554b42004-09-11 04:55:08 +0000123}
124
125std::string
Reid Spencer07adb282004-11-05 22:15:36 +0000126Path::getBasename() const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000127 // Find the last slash
128 size_t slash = path.rfind('/');
129 if (slash == std::string::npos)
130 slash = 0;
131 else
132 slash++;
133
134 return path.substr(slash, path.rfind('.'));
135}
136
Reid Spencer07adb282004-11-05 22:15:36 +0000137bool Path::hasMagicNumber(const std::string &Magic) const {
Reid Spencer1b554b42004-09-11 04:55:08 +0000138 size_t len = Magic.size();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000139 assert(len < 1024 && "Request for magic string too long");
140 char* buf = (char*) alloca(1 + len);
141 int fd = ::open(path.c_str(),O_RDONLY);
142 if (fd < 0)
143 return false;
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000144 size_t read_len = ::read(fd, buf, len);
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000145 close(fd);
Reid Spencer1b6b99b2004-12-13 03:00:51 +0000146 if (len != read_len)
147 return false;
Reid Spencer1b554b42004-09-11 04:55:08 +0000148 buf[len] = '\0';
149 return Magic == buf;
150}
151
Reid Spencereaf18152004-11-14 22:08:36 +0000152bool Path::getMagicNumber(std::string& Magic, unsigned len) const {
153 if (!isFile())
154 return false;
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000155 assert(len < 1024 && "Request for magic string too long");
156 char* buf = (char*) alloca(1 + len);
157 int fd = ::open(path.c_str(),O_RDONLY);
158 if (fd < 0)
159 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000160 ssize_t bytes_read = ::read(fd, buf, len);
161 ::close(fd);
162 if (ssize_t(len) != bytes_read) {
163 Magic.clear();
Reid Spencerbe31d2a2004-11-16 17:14:08 +0000164 return false;
Reid Spencer86ac2dc2004-12-02 09:09:48 +0000165 }
166 Magic.assign(buf,len);
Reid Spencereaf18152004-11-14 22:08:36 +0000167 return true;
168}
169
Reid Spencer1b554b42004-09-11 04:55:08 +0000170bool
Reid Spencer07adb282004-11-05 22:15:36 +0000171Path::isBytecodeFile() const {
Reid Spencer9195f372004-11-09 20:26:31 +0000172 char buffer[ 4];
173 buffer[0] = 0;
Reid Spencer1fce0912004-12-11 00:14:15 +0000174 int fd = ::open(path.c_str(),O_RDONLY);
175 if (fd < 0)
176 return false;
177 ssize_t bytes_read = ::read(fd, buffer, 4);
178 ::close(fd);
179 if (4 != bytes_read)
180 return false;
Reid Spencereaf18152004-11-14 22:08:36 +0000181
182 return (buffer[0] == 'l' && buffer[1] == 'l' && buffer[2] == 'v' &&
183 (buffer[3] == 'c' || buffer[3] == 'm'));
Reid Spencer1b554b42004-09-11 04:55:08 +0000184}
185
186bool
Reid Spencer8e665952004-08-29 05:24:01 +0000187Path::exists() const {
188 return 0 == access(path.c_str(), F_OK );
189}
190
191bool
192Path::readable() const {
193 return 0 == access(path.c_str(), F_OK | R_OK );
194}
195
196bool
197Path::writable() const {
198 return 0 == access(path.c_str(), F_OK | W_OK );
199}
200
201bool
202Path::executable() const {
203 return 0 == access(path.c_str(), R_OK | X_OK );
204}
205
206std::string
207Path::getLast() const {
208 // Find the last slash
209 size_t pos = path.rfind('/');
210
211 // Handle the corner cases
212 if (pos == std::string::npos)
213 return path;
214
215 // If the last character is a slash
216 if (pos == path.length()-1) {
217 // Find the second to last slash
218 size_t pos2 = path.rfind('/', pos-1);
219 if (pos2 == std::string::npos)
220 return path.substr(0,pos);
221 else
222 return path.substr(pos2+1,pos-pos2-1);
223 }
224 // Return everything after the last slash
225 return path.substr(pos+1);
226}
227
Reid Spencerb608a812004-11-16 06:15:19 +0000228void
229Path::getStatusInfo(StatusInfo& info) const {
Reid Spencer9195f372004-11-09 20:26:31 +0000230 struct stat buf;
231 if (0 != stat(path.c_str(), &buf)) {
232 ThrowErrno(std::string("Can't get status: ")+path);
233 }
234 info.fileSize = buf.st_size;
Reid Spencereaf18152004-11-14 22:08:36 +0000235 info.modTime.fromEpochTime(buf.st_mtime);
Reid Spencer9195f372004-11-09 20:26:31 +0000236 info.mode = buf.st_mode;
237 info.user = buf.st_uid;
238 info.group = buf.st_gid;
Reid Spencereaf18152004-11-14 22:08:36 +0000239 info.isDir = S_ISDIR(buf.st_mode);
240 if (info.isDir && path[path.length()-1] != '/')
241 path += '/';
242}
243
Reid Spencer77cc91d2004-12-13 19:59:50 +0000244static bool AddPermissionBits(const std::string& Filename, int bits) {
245 // Get the umask value from the operating system. We want to use it
246 // when changing the file's permissions. Since calling umask() sets
247 // the umask and returns its old value, we must call it a second
248 // time to reset it to the user's preference.
249 int mask = umask(0777); // The arg. to umask is arbitrary.
250 umask(mask); // Restore the umask.
251
252 // Get the file's current mode.
253 struct stat st;
254 if ((stat(Filename.c_str(), &st)) == -1)
255 return false;
256
257 // Change the file to have whichever permissions bits from 'bits'
258 // that the umask would not disable.
259 if ((chmod(Filename.c_str(), (st.st_mode | (bits & ~mask)))) == -1)
260 return false;
261
262 return true;
263}
264
265void Path::makeReadable() {
266 if (!AddPermissionBits(path,0444))
267 ThrowErrno(path + ": can't make file readable");
268}
269
270void Path::makeWriteable() {
271 if (!AddPermissionBits(path,0222))
272 ThrowErrno(path + ": can't make file writable");
273}
274
275void Path::makeExecutable() {
276 if (!AddPermissionBits(path,0111))
277 ThrowErrno(path + ": can't make file executable");
278}
279
Reid Spencereaf18152004-11-14 22:08:36 +0000280bool
Reid Spencerb608a812004-11-16 06:15:19 +0000281Path::getDirectoryContents(std::set<Path>& result) const {
Reid Spencereaf18152004-11-14 22:08:36 +0000282 if (!isDirectory())
283 return false;
284 DIR* direntries = ::opendir(path.c_str());
285 if (direntries == 0)
286 ThrowErrno(path + ": can't open directory");
287
288 result.clear();
289 struct dirent* de = ::readdir(direntries);
290 while (de != 0) {
291 if (de->d_name[0] != '.') {
292 Path aPath(path + (const char*)de->d_name);
293 struct stat buf;
294 if (0 != stat(aPath.path.c_str(), &buf))
295 ThrowErrno(aPath.path + ": can't get status");
296 if (S_ISDIR(buf.st_mode))
297 aPath.path += "/";
Reid Spencerb608a812004-11-16 06:15:19 +0000298 result.insert(aPath);
Reid Spencereaf18152004-11-14 22:08:36 +0000299 }
300 de = ::readdir(direntries);
301 }
302
303 closedir(direntries);
304 return true;
Reid Spencer9195f372004-11-09 20:26:31 +0000305}
306
Reid Spencer8e665952004-08-29 05:24:01 +0000307bool
Reid Spencer07adb282004-11-05 22:15:36 +0000308Path::setDirectory(const std::string& a_path) {
Reid Spencer8e665952004-08-29 05:24:01 +0000309 if (a_path.size() == 0)
310 return false;
311 Path save(*this);
312 path = a_path;
313 size_t last = a_path.size() -1;
Reid Spencer3d595cb2004-12-13 07:51:07 +0000314 if (a_path[last] != '/')
Reid Spencer8e665952004-08-29 05:24:01 +0000315 path += '/';
Reid Spencer07adb282004-11-05 22:15:36 +0000316 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000317 path = save.path;
318 return false;
319 }
320 return true;
321}
322
323bool
Reid Spencer07adb282004-11-05 22:15:36 +0000324Path::setFile(const std::string& a_path) {
Reid Spencer8e665952004-08-29 05:24:01 +0000325 if (a_path.size() == 0)
326 return false;
327 Path save(*this);
328 path = a_path;
329 size_t last = a_path.size() - 1;
330 while (last > 0 && a_path[last] == '/')
331 last--;
332 path.erase(last+1);
Reid Spencer07adb282004-11-05 22:15:36 +0000333 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000334 path = save.path;
335 return false;
336 }
337 return true;
338}
339
340bool
Reid Spencer07adb282004-11-05 22:15:36 +0000341Path::appendDirectory(const std::string& dir) {
342 if (isFile())
Reid Spencer8e665952004-08-29 05:24:01 +0000343 return false;
344 Path save(*this);
345 path += dir;
346 path += "/";
Reid Spencer07adb282004-11-05 22:15:36 +0000347 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000348 path = save.path;
349 return false;
350 }
351 return true;
352}
353
354bool
Reid Spencer07adb282004-11-05 22:15:36 +0000355Path::elideDirectory() {
356 if (isFile())
Reid Spencer8e665952004-08-29 05:24:01 +0000357 return false;
358 size_t slashpos = path.rfind('/',path.size());
359 if (slashpos == 0 || slashpos == std::string::npos)
360 return false;
361 if (slashpos == path.size() - 1)
362 slashpos = path.rfind('/',slashpos-1);
363 if (slashpos == std::string::npos)
364 return false;
365 path.erase(slashpos);
366 return true;
367}
368
369bool
Reid Spencer07adb282004-11-05 22:15:36 +0000370Path::appendFile(const std::string& file) {
371 if (!isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000372 return false;
373 Path save(*this);
374 path += file;
Reid Spencer07adb282004-11-05 22:15:36 +0000375 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000376 path = save.path;
377 return false;
378 }
379 return true;
380}
381
382bool
Reid Spencer07adb282004-11-05 22:15:36 +0000383Path::elideFile() {
384 if (isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000385 return false;
386 size_t slashpos = path.rfind('/',path.size());
387 if (slashpos == std::string::npos)
388 return false;
389 path.erase(slashpos+1);
390 return true;
391}
392
393bool
Reid Spencer07adb282004-11-05 22:15:36 +0000394Path::appendSuffix(const std::string& suffix) {
395 if (isDirectory())
Reid Spencer8e665952004-08-29 05:24:01 +0000396 return false;
397 Path save(*this);
398 path.append(".");
399 path.append(suffix);
Reid Spencer07adb282004-11-05 22:15:36 +0000400 if (!isValid()) {
Reid Spencer8e665952004-08-29 05:24:01 +0000401 path = save.path;
402 return false;
403 }
404 return true;
405}
406
407bool
Reid Spencer07adb282004-11-05 22:15:36 +0000408Path::elideSuffix() {
409 if (isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000410 size_t dotpos = path.rfind('.',path.size());
411 size_t slashpos = path.rfind('/',path.size());
412 if (slashpos != std::string::npos && dotpos != std::string::npos &&
413 dotpos > slashpos) {
414 path.erase(dotpos, path.size()-dotpos);
415 return true;
416 }
417 return false;
418}
419
420
421bool
Reid Spencer07adb282004-11-05 22:15:36 +0000422Path::createDirectory( bool create_parents) {
Reid Spencer8e665952004-08-29 05:24:01 +0000423 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000424 if (!isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000425
426 // Get a writeable copy of the path name
427 char pathname[MAXPATHLEN];
428 path.copy(pathname,MAXPATHLEN);
429
430 // Null-terminate the last component
431 int lastchar = path.length() - 1 ;
432 if (pathname[lastchar] == '/')
433 pathname[lastchar] = 0;
Reid Spencereaf18152004-11-14 22:08:36 +0000434 else
435 pathname[lastchar+1] = 0;
Reid Spencer8e665952004-08-29 05:24:01 +0000436
437 // If we're supposed to create intermediate directories
438 if ( create_parents ) {
439 // Find the end of the initial name component
440 char * next = strchr(pathname,'/');
441 if ( pathname[0] == '/')
442 next = strchr(&pathname[1],'/');
443
444 // Loop through the directory components until we're done
445 while ( next != 0 ) {
446 *next = 0;
447 if (0 != access(pathname, F_OK | R_OK | W_OK))
448 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
449 ThrowErrno(std::string(pathname) + ": Can't create directory");
450 char* save = next;
Reid Spencereaf18152004-11-14 22:08:36 +0000451 next = strchr(next+1,'/');
Reid Spencer8e665952004-08-29 05:24:01 +0000452 *save = '/';
453 }
Reid Spencer8e665952004-08-29 05:24:01 +0000454 }
Reid Spencereaf18152004-11-14 22:08:36 +0000455
456 if (0 != access(pathname, F_OK | R_OK))
457 if (0 != mkdir(pathname, S_IRWXU | S_IRWXG))
458 ThrowErrno(std::string(pathname) + ": Can't create directory");
Reid Spencer8e665952004-08-29 05:24:01 +0000459 return true;
460}
461
462bool
Reid Spencer07adb282004-11-05 22:15:36 +0000463Path::createFile() {
Reid Spencer8e665952004-08-29 05:24:01 +0000464 // Make sure we're dealing with a file
Reid Spencer07adb282004-11-05 22:15:36 +0000465 if (!isFile()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000466
467 // Create the file
Reid Spencer622e2202004-09-18 19:25:11 +0000468 int fd = ::creat(path.c_str(), S_IRUSR | S_IWUSR);
469 if (fd < 0)
Reid Spencer9195f372004-11-09 20:26:31 +0000470 ThrowErrno(path + ": Can't create file");
Reid Spencer622e2202004-09-18 19:25:11 +0000471 ::close(fd);
Reid Spencer8e665952004-08-29 05:24:01 +0000472
473 return true;
Reid Spencerb89a2232004-08-25 06:20:07 +0000474}
475
Reid Spencer8e665952004-08-29 05:24:01 +0000476bool
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000477Path::createTemporaryFile(bool reuse_current) {
Reid Spencer9195f372004-11-09 20:26:31 +0000478 // Make sure we're dealing with a file
Reid Spencerc29befb2004-12-15 01:50:13 +0000479 if (!isFile())
480 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000481
Reid Spencerc29befb2004-12-15 01:50:13 +0000482 // Make this into a unique file name
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000483 makeUnique( reuse_current );
Reid Spencerc29befb2004-12-15 01:50:13 +0000484
485 // create the file
486 int outFile = ::open(path.c_str(), O_WRONLY|O_CREAT|O_TRUNC, 0666);
487 if (outFile != -1) {
488 ::close(outFile);
489 return true;
Reid Spencer9195f372004-11-09 20:26:31 +0000490 }
Reid Spencerc29befb2004-12-15 01:50:13 +0000491 return false;
Reid Spencer9195f372004-11-09 20:26:31 +0000492}
493
494bool
Reid Spencer00e89302004-12-15 23:02:10 +0000495Path::destroyDirectory(bool remove_contents) const {
Reid Spencer8e665952004-08-29 05:24:01 +0000496 // Make sure we're dealing with a directory
Reid Spencer07adb282004-11-05 22:15:36 +0000497 if (!isDirectory()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000498
499 // If it doesn't exist, we're done.
500 if (!exists()) return true;
501
502 if (remove_contents) {
503 // Recursively descend the directory to remove its content
504 std::string cmd("/bin/rm -rf ");
505 cmd += path;
506 system(cmd.c_str());
507 } else {
508 // Otherwise, try to just remove the one directory
509 char pathname[MAXPATHLEN];
510 path.copy(pathname,MAXPATHLEN);
511 int lastchar = path.length() - 1 ;
512 if (pathname[lastchar] == '/')
513 pathname[lastchar] = 0;
Reid Spencereaf18152004-11-14 22:08:36 +0000514 else
515 pathname[lastchar+1] = 0;
Reid Spencer8e665952004-08-29 05:24:01 +0000516 if ( 0 != rmdir(pathname))
517 ThrowErrno(std::string(pathname) + ": Can't destroy directory");
518 }
519 return true;
520}
521
522bool
Reid Spencer00e89302004-12-15 23:02:10 +0000523Path::destroyFile() const {
Reid Spencer07adb282004-11-05 22:15:36 +0000524 if (!isFile()) return false;
Reid Spencer8e665952004-08-29 05:24:01 +0000525 if (0 != unlink(path.c_str()))
Reid Spencereaf18152004-11-14 22:08:36 +0000526 ThrowErrno(path + ": Can't destroy file");
527 return true;
528}
529
530bool
531Path::renameFile(const Path& newName) {
532 if (!isFile()) return false;
533 if (0 != rename(path.c_str(), newName.c_str()))
Reid Spencer1fce0912004-12-11 00:14:15 +0000534 ThrowErrno(std::string("can't rename ") + path + " as " +
535 newName.toString());
Reid Spencereaf18152004-11-14 22:08:36 +0000536 return true;
537}
538
539bool
540Path::setStatusInfo(const StatusInfo& si) const {
541 if (!isFile()) return false;
542 struct utimbuf utb;
543 utb.actime = si.modTime.toPosixTime();
544 utb.modtime = utb.actime;
545 if (0 != ::utime(path.c_str(),&utb))
546 ThrowErrno(path + ": can't set file modification time");
547 if (0 != ::chmod(path.c_str(),si.mode))
548 ThrowErrno(path + ": can't set mode");
Reid Spencer8e665952004-08-29 05:24:01 +0000549 return true;
550}
551
Reid Spencerc29befb2004-12-15 01:50:13 +0000552void
553CopyFile(const sys::Path &Dest, const sys::Path &Src) {
554 int inFile = -1;
555 int outFile = -1;
556 try {
557 inFile = ::open(Src.c_str(), O_RDONLY);
558 if (inFile == -1)
559 ThrowErrno("Cannnot open source file to copy: " + Src.toString());
560
561 outFile = ::open(Dest.c_str(), O_WRONLY|O_CREAT, 0666);
562 if (outFile == -1)
563 ThrowErrno("Cannnot create destination file for copy: " +Dest.toString());
564
565 char Buffer[16*1024];
566 while (ssize_t Amt = ::read(inFile, Buffer, 16*1024)) {
567 if (Amt == -1) {
568 if (errno != EINTR && errno != EAGAIN)
569 ThrowErrno("Can't read source file: " + Src.toString());
570 } else {
571 char *BufPtr = Buffer;
572 while (Amt) {
573 ssize_t AmtWritten = ::write(outFile, BufPtr, Amt);
574 if (AmtWritten == -1) {
575 if (errno != EINTR && errno != EAGAIN)
576 ThrowErrno("Can't write destination file: " + Dest.toString());
577 } else {
578 Amt -= AmtWritten;
579 BufPtr += AmtWritten;
580 }
581 }
582 }
583 }
584 ::close(inFile);
585 ::close(outFile);
586 } catch (...) {
587 if (inFile != -1)
588 ::close(inFile);
589 if (outFile != -1)
590 ::close(outFile);
591 throw;
592 }
593}
594
595void
Reid Spencer07f9f4e2004-12-15 08:32:45 +0000596Path::makeUnique(bool reuse_current) {
597 if (reuse_current && !exists())
Reid Spencerc29befb2004-12-15 01:50:13 +0000598 return; // File doesn't exist already, just use it!
599
600 // Append an XXXXXX pattern to the end of the file for use with mkstemp,
601 // mktemp or our own implementation.
602 char *FNBuffer = (char*) alloca(path.size()+8);
603 path.copy(FNBuffer,path.size());
604 strcpy(FNBuffer+path.size(), "-XXXXXX");
605
606#if defined(HAVE_MKSTEMP)
607 int TempFD;
608 if ((TempFD = mkstemp(FNBuffer)) == -1) {
609 ThrowErrno("Cannot make unique filename for '" + path + "'");
610 }
611
612 // We don't need to hold the temp file descriptor... we will trust that no one
613 // will overwrite/delete the file before we can open it again.
614 close(TempFD);
615
616 // Save the name
617 path = FNBuffer;
618#elif defined(HAVE_MKTEMP)
619 // If we don't have mkstemp, use the old and obsolete mktemp function.
620 if (mktemp(FNBuffer) == 0) {
621 ThrowErrno("Cannot make unique filename for '" + path + "'");
622 }
623
624 // Save the name
625 path = FNBuffer;
626#else
627 // Okay, looks like we have to do it all by our lonesome.
628 static unsigned FCounter = 0;
629 unsigned offset = path.size() + 1;
630 while ( FCounter < 999999 && exists()) {
631 sprintf(FNBuffer+offset,"%06u",++FCounter);
632 path = FNBuffer;
633 }
634 if (FCounter > 999999)
635 throw std::string("Cannot make unique filename for '" + path + "'");
636#endif
637
638}
Reid Spencerb89a2232004-08-25 06:20:07 +0000639}
640
641// vim: sw=2