Skip to content

Commit

Permalink
fix: backup file name
Browse files Browse the repository at this point in the history
  • Loading branch information
kucaahbe committed Jan 30, 2024
1 parent f2c6bee commit 197ecfc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 15 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Added

- better error messages for `myrc install`

### Fixed

- backup file name (now contains timestamp to avoid clashes and increases
probability of backup success)

## [0.1.0] - 2024-01-24

### Added
Expand Down
26 changes: 11 additions & 15 deletions source/symlink.d
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import std.file: readLink, isSymlink, symlink, rename, exists, FileException;
import std.path: absolutePath, dirName, asNormalizedPath;
import std.functional: memoize;
import std.datetime.systime : SysTime, Clock;
import std.array;
import path;

Expand Down Expand Up @@ -74,8 +75,6 @@ struct Symlink {
/** creates symbolic _link in the file system
* (creates backup of the destination if it exists)
* Returns: path of the backup file if backup was created
* Bugs: the name of the backup file is currently incorrect (must be
* `filename.TIMESTAMP.bak`)
*/
string link()
{
Expand Down Expand Up @@ -130,6 +129,7 @@ struct Symlink {
{
import std.file;
import std.path;
import std.regex;
import test_file;

if (".test".exists) ".test".rmdirRecurse;
Expand All @@ -145,11 +145,13 @@ struct Symlink {
auto link = Symlink(srcPath, destPath);
immutable auto backup = link.link();

immutable auto destPathBackup = cwd ~ '/' ~ destPath ~ ".bak";
immutable auto destPathBackup = cwd ~ '/' ~ destPath ~ r".\d+T\d+.\d+.bak$";

assert(link.destination.absolute.isSymlink);
assert(link.destination.absolute.readLink == link.source.absolute);
assert(backup == destPathBackup, backup ~ "!=" ~ destPathBackup);
assert(backup.exists, `backup file "` ~ backup ~ `" does not exist`);
assert(backup.matchFirst(regex(destPathBackup)),
`backup file "` ~ backup ~ `" doesn't match /` ~ destPathBackup ~ `/`);
}

/** **fail case**: destination does not exist
Expand Down Expand Up @@ -285,17 +287,11 @@ struct Symlink {

private string backup()
{
auto backup = destination.absolute ~ ".bak";
try
{
rename(destination.absolute, backup);
} catch (FileException e)
{
import std.stdio: writeln;
writeln("DEBUG:", destination.absolute, " ", backup);
writeln(e.message, " ", e.errno);
backup = "FAILED BACKUP";
}
SysTime now = Clock.currTime();
auto backup = destination.absolute ~ '.' ~ now.toISOString() ~ ".bak";

rename(destination.absolute, backup);

return backup;
}
}

0 comments on commit 197ecfc

Please sign in to comment.