monotone

monotone Mtn Source Tree

Root/src/platform.hh

1// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
2//
3// This program is made available under the GNU GPL version 2.0 or
4// greater. See the accompanying file COPYING for details.
5//
6// This program is distributed WITHOUT ANY WARRANTY; without even the
7// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
8// PURPOSE.
9
10#ifndef __PLATFORM_HH__
11#define __PLATFORM_HH__
12
13// this describes functions to be found, alternatively, in win32/* or unix/*
14// directories.
15
16
17#include <stdio.h>
18#include <time.h>
19
20void read_password(std::string const & prompt, char * buf, size_t bufsz);
21void get_system_flavour(std::string & ident);
22bool is_executable(const char *path);
23
24// For LUA
25int existsonpath(const char *exe);
26int set_executable(const char *path);
27int clear_executable(const char *path);
28pid_t process_spawn(const char * const argv[]);
29pid_t process_spawn_redirected(char const * in,
30 char const * out,
31 char const * err,
32 char const * const argv[]);
33pid_t process_spawn_pipe(char const * const argv[], FILE** in, FILE** out);
34int process_wait(pid_t pid, int *res, int timeout = -1);// default infinite
35int process_kill(pid_t pid, int signal);
36int process_sleep(unsigned int seconds);
37
38// stop "\n"->"\r\n" from breaking automate on Windows
39void make_io_binary();
40
41#ifdef WIN32
42std::string munge_argv_into_cmdline(const char* const argv[]);
43#endif
44// for term selection
45bool have_smart_terminal();
46// this function cannot call W/P/L, because it is called by the tick printing
47// code.
48// return value of 0 means "unlimited"
49unsigned int terminal_width();
50
51// for "reckless mode" workspace change detection.
52// returns 'true' if it has generated a valid inodeprint; returns 'false' if
53// there was a problem, in which case we should act as if the inodeprint has
54// changed.
55class inodeprint_calculator
56{
57public:
58 template<typename T> void add_item(T obj)
59 {
60 size_t size(sizeof(obj));
61 add_item(&size, sizeof(size));
62 add_item(&obj, sizeof(obj));
63 }
64 // When adding a time to the print use these to note if it's
65 // close to the current time (within about 3 seconds) or
66 // in the future.
67 // To make this more robust, there are some tricks:
68 // -- we refuse to inodeprint files whose times are within a few seconds of
69 // 'now'. This is because, we might memorize the inodeprint, then
70 // someone writes to the file, and this write does not update the
71 // timestamp -- or rather, it does update the timestamp, but nothing
72 // happens, because the new value is the same as the old value. We use
73 // "a few seconds" to make sure that it is larger than whatever the
74 // filesystem's timekeeping granularity is (rounding to 2 seconds is
75 // known to exist in the wild).
76 // -- by the same reasoning, we should also refuse to inodeprint files whose
77 // time is in the future, because it is possible that someone will write
78 // to that file exactly when that future second arrives, and we will
79 // never notice. However, this would create persistent and hard to
80 // diagnosis slowdowns, whenever a tree accidentally had its times set
81 // into the future. Therefore, to handle this case, we include a "is
82 // this time in the future?" bit in the hashed information. This bit
83 // will change when we pass the future point, and trigger a re-check of
84 // the file's contents.
85 //
86 // This is, of course, still not perfect. There is no way to make our stat
87 // atomic with the actual read of the file, so there's always a race condition
88 // there. Additionally, this handling means that checkout will never actually
89 // inodeprint anything, but rather the first command after checkout will be
90 // slow. There doesn't seem to be anything that could be done about this.
91 virtual void note_future(bool f = true) = 0;
92 virtual void note_nowish(bool f = true) = 0;
93 virtual ~inodeprint_calculator() {};
94protected:
95 virtual void add_item(void *dat, size_t size) = 0;
96};
97bool inodeprint_file(std::string const & file, inodeprint_calculator & calc);
98
99// for netsync 'serve' pidfile support
100pid_t get_process_id();
101
102// netsync wants to ignore sigpipe; this is meaningless on Win32
103#ifdef WIN32
104inline void ignore_sigpipe() {}
105#else
106void ignore_sigpipe(); // in unix/process.cc
107#endif
108
109// filesystem stuff
110// FIXME: BUG: this returns a string in the filesystem charset/encoding
111std::string get_current_working_dir();
112// calls E() if fails
113void change_current_working_dir(std::string const & to);
114std::string tilde_expand(std::string const & path);
115std::string get_default_confdir();
116
117inline std::string get_default_keydir()
118{ return get_default_confdir() + "/keys"; }
119
120std::string get_homedir();
121namespace path
122{
123 typedef enum { nonexistent, directory, file } status;
124};
125path::status get_path_status(std::string const & path);
126
127struct dirent_consumer
128{
129 virtual ~dirent_consumer() {}
130 virtual void consume(const char *) = 0;
131};
132void read_directory(std::string const & path,
133 dirent_consumer & files,
134 dirent_consumer & dirs,
135 dirent_consumer & other_files);
136
137void make_accessible(std::string const & name);
138void rename_clobberingly(std::string const & from, std::string const & to);
139
140// path must be an existing file, or an existing empty directory.
141void do_remove(std::string const & path);
142
143// This is platform-specific because it uses raw pathname strings
144// internally; some raw pathnames cannot be represented as any_path objects.
145// It may also be more efficient to let the OS do all of this.
146//
147// It is not an error to call this function on a path that doesn't exist,
148// or is a file rather than a directory.
149void do_remove_recursive(std::string const & path);
150
151void do_mkdir(std::string const & path);
152void write_data_worker(std::string const & p,
153 std::string const & dat,
154 std::string const & tmpdir,
155 bool user_private);
156
157// strerror wrapper for OS-specific errors (e.g. use FormatMessage on Win32)
158std::string os_strerror(os_err_t errnum);
159
160// for running cpu benchmarks
161// Returns the processor time used by the current process, plus some
162// arbitrary constant, measured in seconds.
163double cpu_now();
164
165// determine directory to load locale data from
166std::string get_locale_dir();
167
168// Fill tp from s, using format fmt.
169// throws on failure.
170//
171// This is strptime on Unix, something else on MinGW.
172void parse_date(const std::string s, const std::string fmt, struct tm *tp);
173
174#endif // __PLATFORM_HH__
175
176// Local Variables:
177// mode: C++
178// fill-column: 76
179// c-file-style: "gnu"
180// indent-tabs-mode: nil
181// End:
182// 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