monotone

monotone Mtn Source Tree

Root/unix/fs.cc

1// copyright (C) 2005 nathaniel smith <njs@pobox.com>
2// all rights reserved.
3// licensed to the public under the terms of the GNU GPL (>= 2)
4// see the file COPYING for details
5
6#include <unistd.h>
7#include <errno.h>
8#include <stdlib.h>
9#include <sys/types.h>
10#include <sys/stat.h>
11#include <pwd.h>
12#include <stdio.h>
13#include <cstring>
14
15#include <boost/filesystem/path.hpp>
16#include <boost/filesystem/operations.hpp>
17#include <boost/filesystem/convenience.hpp>
18
19#include "sanity.hh"
20#include "platform.hh"
21
22namespace fs = boost::filesystem;
23
24std::string
25get_current_working_dir()
26{
27 char buffer[4096];
28 E(getcwd(buffer, 4096),
29 F("cannot get working directory: %s") % os_strerror(errno));
30 return std::string(buffer);
31}
32
33void
34change_current_working_dir(std::string const & to)
35{
36 E(!chdir(to.c_str()),
37 F("cannot change to directory %s: %s") % to % os_strerror(errno));
38}
39
40std::string
41get_default_confdir()
42{
43 return get_homedir() + "/.monotone";
44}
45
46// FIXME: BUG: this probably mangles character sets
47// (as in, we're treating system-provided data as utf8, but it's probably in
48// the filesystem charset)
49std::string
50get_homedir()
51{
52 char * home = getenv("HOME");
53 if (home != NULL)
54 return std::string(home);
55
56 struct passwd * pw = getpwuid(getuid());
57 N(pw != NULL, F("could not find home directory for uid %d") % getuid());
58 return std::string(pw->pw_dir);
59}
60
61std::string
62tilde_expand(std::string const & in)
63{
64 if (in.empty() || in[0] != '~')
65 return in;
66 fs::path tmp(in, fs::native);
67 fs::path::iterator i = tmp.begin();
68 if (i != tmp.end())
69 {
70 fs::path res;
71 if (*i == "~")
72 {
73 fs::path restmp(get_homedir(), fs::native);
74 res /= restmp;
75 ++i;
76 }
77 else if (i->size() > 1 && i->at(0) == '~')
78 {
79 struct passwd * pw;
80 // FIXME: BUG: this probably mangles character sets (as in, we're
81 // treating system-provided data as utf8, but it's probably in the
82 // filesystem charset)
83 pw = getpwnam(i->substr(1).c_str());
84 N(pw != NULL,
85 F("could not find home directory for user %s") % i->substr(1));
86 res /= std::string(pw->pw_dir);
87 ++i;
88 }
89 while (i != tmp.end())
90 res /= *i++;
91 return res.string();
92 }
93
94 return tmp.string();
95}
96
97path::status
98get_path_status(std::string const & path)
99{
100 struct stat buf;
101 int res;
102 res = stat(path.c_str(), &buf);
103 if (res < 0)
104 {
105 if (errno == ENOENT)
106 return path::nonexistent;
107 else
108 E(false, F("error accessing file %s: %s") % path % os_strerror(errno));
109 }
110 if (S_ISREG(buf.st_mode))
111 return path::file;
112 else if (S_ISDIR(buf.st_mode))
113 return path::directory;
114 else
115 {
116 // fifo or device or who knows what...
117 E(false, F("cannot handle special file %s") % path);
118 }
119}
120
121void
122rename_clobberingly(std::string const & from, std::string const & to)
123{
124 E(!rename(from.c_str(), to.c_str()),
125 F("renaming '%s' to '%s' failed: %s") % from % to % os_strerror(errno));
126}
127
128// Local Variables:
129// mode: C++
130// fill-column: 76
131// c-file-style: "gnu"
132// indent-tabs-mode: nil
133// End:
134// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status