1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use std::{convert::TryInto, fs::OpenOptions, io::Write, path::Path, time::Duration};
use bstr::BStr;
use git_hash::oid;
use git_index::Entry;
use io_close::Close;
use crate::{fs, index, os};
pub struct Context<'a, 'paths, Find> {
pub find: &'a mut Find,
pub path_cache: &'a mut fs::Cache<'paths>,
pub buf: &'a mut Vec<u8>,
}
#[cfg_attr(not(unix), allow(unused_variables))]
pub fn checkout<Find, E>(
entry: &mut Entry,
entry_path: &BStr,
Context { find, path_cache, buf }: Context<'_, '_, Find>,
index::checkout::Options {
fs: crate::fs::Capabilities {
symlink,
executable_bit,
..
},
destination_is_initially_empty,
overwrite_existing,
..
}: index::checkout::Options,
) -> Result<usize, index::checkout::Error<E>>
where
Find: for<'a> FnMut(&oid, &'a mut Vec<u8>) -> Result<git_object::BlobRef<'a>, E>,
E: std::error::Error + Send + Sync + 'static,
{
let dest_relative = git_path::try_from_bstr(entry_path).map_err(|_| index::checkout::Error::IllformedUtf8 {
path: entry_path.to_owned(),
})?;
let is_dir = Some(entry.mode == git_index::entry::Mode::COMMIT || entry.mode == git_index::entry::Mode::DIR);
let dest = path_cache.at_path(dest_relative, is_dir, &mut *find)?.path();
let object_size = match entry.mode {
git_index::entry::Mode::FILE | git_index::entry::Mode::FILE_EXECUTABLE => {
let obj = find(&entry.id, buf).map_err(|err| index::checkout::Error::Find {
err,
oid: entry.id,
path: dest.to_path_buf(),
})?;
#[cfg_attr(not(unix), allow(unused_mut))]
let mut options = open_options(dest, destination_is_initially_empty, overwrite_existing);
let needs_executable_bit = executable_bit && entry.mode == git_index::entry::Mode::FILE_EXECUTABLE;
#[cfg(unix)]
if needs_executable_bit && destination_is_initially_empty {
use std::os::unix::fs::OpenOptionsExt;
options.mode(0o777);
}
let mut file = try_write_or_unlink(dest, overwrite_existing, |p| options.open(p))?;
file.write_all(obj.data)?;
#[cfg(unix)]
if needs_executable_bit && !destination_is_initially_empty {
use std::os::unix::fs::PermissionsExt;
let mut perm = std::fs::symlink_metadata(&dest)?.permissions();
perm.set_mode(0o777);
std::fs::set_permissions(&dest, perm)?;
}
update_fstat(entry, file.metadata()?)?;
file.close()?;
obj.data.len()
}
git_index::entry::Mode::SYMLINK => {
let obj = find(&entry.id, buf).map_err(|err| index::checkout::Error::Find {
err,
oid: entry.id,
path: dest.to_path_buf(),
})?;
let symlink_destination = git_path::try_from_byte_slice(obj.data)
.map_err(|_| index::checkout::Error::IllformedUtf8 { path: obj.data.into() })?;
if symlink {
try_write_or_unlink(dest, overwrite_existing, |p| {
crate::os::create_symlink(symlink_destination, p)
})?;
} else {
let mut file = try_write_or_unlink(dest, overwrite_existing, |p| {
open_options(p, destination_is_initially_empty, overwrite_existing).open(&dest)
})?;
file.write_all(obj.data)?;
file.close()?;
}
update_fstat(entry, std::fs::symlink_metadata(&dest)?)?;
obj.data.len()
}
git_index::entry::Mode::DIR => todo!(),
git_index::entry::Mode::COMMIT => todo!(),
_ => unreachable!(),
};
Ok(object_size)
}
fn try_write_or_unlink<T>(
path: &Path,
overwrite_existing: bool,
op: impl Fn(&Path) -> std::io::Result<T>,
) -> std::io::Result<T> {
if overwrite_existing {
match op(path) {
Ok(res) => Ok(res),
Err(err) if os::indicates_collision(&err) => {
try_unlink_path_recursively(path, &std::fs::symlink_metadata(path)?)?;
op(path)
}
Err(err) => Err(err),
}
} else {
op(path)
}
}
fn try_unlink_path_recursively(path: &Path, path_meta: &std::fs::Metadata) -> std::io::Result<()> {
if path_meta.is_dir() {
std::fs::remove_dir_all(path)
} else if path_meta.file_type().is_symlink() {
os::remove_symlink(path)
} else {
std::fs::remove_file(path)
}
}
#[cfg(not(debug_assertions))]
fn debug_assert_dest_is_no_symlink(_path: &Path) {}
#[cfg(debug_assertions)]
fn debug_assert_dest_is_no_symlink(path: &Path) {
if let Ok(meta) = path.metadata() {
debug_assert!(
!meta.file_type().is_symlink(),
"BUG: should not ever allow to overwrite/write-into the target of a symbolic link: {}",
path.display()
);
}
}
fn open_options(path: &Path, destination_is_initially_empty: bool, overwrite_existing: bool) -> OpenOptions {
if overwrite_existing || !destination_is_initially_empty {
debug_assert_dest_is_no_symlink(path);
}
let mut options = git_features::fs::open_options_no_follow();
options
.create_new(destination_is_initially_empty && !overwrite_existing)
.create(!destination_is_initially_empty || overwrite_existing)
.write(true);
options
}
fn update_fstat<E>(entry: &mut Entry, meta: std::fs::Metadata) -> Result<(), index::checkout::Error<E>>
where
E: std::error::Error + Send + Sync + 'static,
{
let ctime = meta
.created()
.map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH))?;
let mtime = meta
.modified()
.map_or(Ok(Duration::default()), |x| x.duration_since(std::time::UNIX_EPOCH))?;
let stat = &mut entry.stat;
stat.mtime.secs = mtime
.as_secs()
.try_into()
.expect("by 2038 we found a solution for this");
stat.mtime.nsecs = mtime.subsec_nanos();
stat.ctime.secs = ctime
.as_secs()
.try_into()
.expect("by 2038 we found a solution for this");
stat.ctime.nsecs = ctime.subsec_nanos();
Ok(())
}