abraunegg-onedrive/src/util.d
2015-09-01 20:47:44 +02:00

45 lines
899 B
D

import std.conv: to;
import std.digest.crc;
import std.digest.digest;
import std.stdio;
import std.string: chomp;
import std.file: exists, rename;
import std.path: extension;
private string deviceName;
static this()
{
import std.socket;
deviceName = Socket.hostName;
}
// give a new name to the specified file or directory
void safeRename(const(char)[] path)
{
auto ext = extension(path);
auto newPath = path.chomp(ext) ~ "-" ~ deviceName;
if (exists(newPath ~ ext)) {
int n = 2;
char[] newPath2;
do {
newPath2 = newPath ~ "-" ~ n.to!string;
n++;
} while (exists(newPath2 ~ ext));
newPath = newPath2;
}
newPath ~= ext;
rename(path, newPath);
}
// return the crc32 hex string of a file
string computeCrc32(string path)
{
CRC32 crc;
auto file = File(path, "rb");
foreach (ubyte[] data; chunks(file, 4096)) {
crc.put(data);
}
return crc.finish().toHexString().dup;
}