Skip to content

Commit b0bc770

Browse files
committed
clear directory contents in reset without removing it
1 parent 7c474cc commit b0bc770

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

tmc-langs-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ lazy_static = "1"
2020
percent-encoding = "2"
2121
serde_json = "1"
2222
schemars = "0.7"
23+
walkdir = "2"
2324

2425
[dev-dependencies]
2526
env_logger = "0.7"

tmc-langs-core/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ pub enum CoreError {
5757
Response(#[from] response::ResponseError),
5858
#[error(transparent)]
5959
ResponseErrors(#[from] response::ResponseErrors),
60+
#[error(transparent)]
61+
WalkDir(#[from] walkdir::Error),
6062
}
6163

6264
impl From<TokenError> for CoreError {

tmc-langs-core/src/tmc_core.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use std::thread;
2424
use std::time::Duration;
2525
use tempfile::NamedTempFile;
2626
use tmc_langs_util::task_executor;
27+
use walkdir::WalkDir;
2728

2829
pub type Token =
2930
oauth2::StandardTokenResponse<oauth2::EmptyExtraTokenFields, oauth2::basic::BasicTokenType>;
@@ -493,15 +494,29 @@ impl TmcCore {
493494
}
494495

495496
pub fn reset(&self, exercise_id: usize, exercise_path: &Path) -> Result<()> {
496-
// windows sometimes fails due to files being in use, retry a few times
497-
// todo: handle properly
497+
// clear out the exercise directory
498498
let mut tries = 0;
499-
while let Err(err) = fs::remove_dir_all(exercise_path) {
500-
tries += 1;
501-
if tries > 8 {
502-
return Err(CoreError::DirRemove(exercise_path.to_path_buf(), err));
499+
for entry in WalkDir::new(exercise_path).min_depth(1) {
500+
let entry = entry?;
501+
// windows sometimes fails due to files being in use, retry a few times
502+
// todo: handle properly
503+
if entry.path().is_dir() {
504+
while let Err(err) = fs::remove_dir_all(exercise_path) {
505+
tries += 1;
506+
if tries > 8 {
507+
return Err(CoreError::DirRemove(exercise_path.to_path_buf(), err));
508+
}
509+
thread::sleep(Duration::from_secs(1));
510+
}
511+
} else {
512+
while let Err(err) = fs::remove_file(exercise_path) {
513+
tries += 1;
514+
if tries > 8 {
515+
return Err(CoreError::DirRemove(exercise_path.to_path_buf(), err));
516+
}
517+
thread::sleep(Duration::from_secs(1));
518+
}
503519
}
504-
thread::sleep(Duration::from_secs(1));
505520
}
506521
self.download_or_update_exercises(vec![(exercise_id, exercise_path)])
507522
}

0 commit comments

Comments
 (0)