monotone

monotone Mtn Source Tree

Root/src/file_io.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 __FILE_IO_H__
11#define __FILE_IO_H__
12
13#include "vocab.hh"
14#include "paths.hh"
15#include "platform-wrapped.hh"
16#include "vector.hh"
17#include "ui.hh"
18
19// this layer deals with talking to the filesystem, loading and saving
20// files, walking trees, etc.
21
22// this code mostly deals in any_path's, because these operations are too low
23// level for us to say whether applying them in any given case is valid or
24// not.
25
26// use I()
27void assert_path_is_nonexistent(any_path const & path);
28void assert_path_is_file(any_path const & path);
29void assert_path_is_directory(any_path const & path);
30
31// use E()
32void require_path_is_nonexistent(any_path const & path,
33 i18n_format const & message);
34void require_path_is_file(any_path const & path,
35 i18n_format const & message_if_nonexistent,
36 i18n_format const & message_if_directory);
37void require_path_is_directory(any_path const & path,
38 i18n_format const & message_if_nonexistent,
39 i18n_format const & message_if_file);
40
41// returns true if there is a file or directory at 'path'
42bool path_exists(any_path const & path);
43// returns true if there is a directory at 'path'
44bool directory_exists(any_path const & path);
45// returns true if there is a file at 'path'
46bool file_exists(any_path const & path);
47// returns true if there is a directory at 'path' with no files or sub-directories
48bool directory_empty(any_path const & path);
49
50// returns true if the string content is binary according to monotone heuristic
51bool guess_binary(std::string const & s);
52
53// creates and returns the name of a temporary file
54bool monotone_mkstemp(std::string & tmpl);
55
56void mkdir_p(any_path const & path);
57void make_dir_for(any_path const & p);
58
59void delete_file(any_path const & path);
60void delete_dir_shallow(any_path const & path);
61void delete_file_or_dir_shallow(any_path const & path);
62void delete_dir_recursive(any_path const & path);
63
64void move_file(any_path const & old_path,
65 any_path const & new_path);
66
67void move_dir(any_path const & old_path,
68 any_path const & new_path);
69
70// calls move_file or move_dir as appropriate
71void move_path(any_path const & old_path,
72 any_path const & new_path);
73
74void read_data(any_path const & path, data & data);
75
76void read_data_stdin(data & dat);
77
78// This function knows that "-" means "stdin".
79void read_data_for_command_line(utf8 const & path, data & dat);
80
81// These are not any_path's because we make our write somewhat atomic -- we
82// first write to a temp file in _MTN/ (and it must be in _MTN/, not like /tmp
83// or something, because we can't necessarily atomic rename from /tmp to the
84// workspace). But that means we can't use it in general, only for the
85// workspace.
86void write_data(file_path const & path, data const & data);
87void write_data(bookkeeping_path const & path, data const & data);
88
89// Version that takes a system_path. To work with the "somewhat atomic"
90// goal, it also takes as an argument the place to put the temp file. Whoever
91// uses this is responsible to make sure that the tmpdir argument is somewhere
92// that the file can be atomically renamed from (same file system)
93void write_data(system_path const & path,
94 data const & data,
95 system_path const & tmpdir);
96
97// Identical to the above, but the file will be inaccessible to anyone but
98// the user. Use for things like private keys.
99void write_data_userprivate(system_path const & path,
100 data const & data,
101 system_path const & tmpdir);
102
103class tree_walker
104{
105public:
106 // returns true if the directory should be descended into
107 virtual bool visit_dir(file_path const & path);
108 virtual void visit_file(file_path const & path) = 0;
109 virtual ~tree_walker();
110};
111
112// from some safe sub-dir of cwd
113// file_path of "" means cwd
114void walk_tree(file_path const & path,
115 tree_walker & walker);
116
117
118class directory_cleanup_helper
119{
120public:
121 directory_cleanup_helper(system_path const & new_dir)
122 : committed(false), dir(new_dir) {}
123 ~directory_cleanup_helper()
124 {
125 if (!committed && directory_exists(dir))
126 {
127 // This is probably happening in the middle of another exception.
128 // Do not let anything that delete_dir_recursive throws escape, or
129 // the runtime will call std::terminate...
130 try
131 {
132 delete_dir_recursive(dir);
133 }
134 catch (std::exception const & ex)
135 {
136 ui.fatal_exception(ex);
137 }
138 catch (...)
139 {
140 ui.fatal_exception();
141 }
142 }
143 }
144 void commit(void) { committed = true; }
145private:
146 bool committed;
147 system_path dir;
148};
149
150bool ident_existing_file(file_path const & p, file_id & ident);
151bool ident_existing_file(file_path const & p, file_id & ident, path::status status);
152
153void calculate_ident(file_path const & file,
154 file_id & ident);
155
156#endif // __FILE_IO_H__
157
158// Local Variables:
159// mode: C++
160// fill-column: 76
161// c-file-style: "gnu"
162// indent-tabs-mode: nil
163// End:
164// 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