--- a/brewco/util.c Thu Dec 17 22:45:54 2015 +0100 +++ b/brewco/util.c Sun Dec 20 20:37:40 2015 +0100 @@ -76,6 +76,69 @@ +/* + * Buffered filecopy, filetime is preserved. + */ +int file_cp(char *from, char *to) +{ + char *line; + FILE *stfrom, *stto; + int dummy, bread; + static int error; + struct stat sb; + struct utimbuf ut; + + stfrom = fopen(from, "r"); + if (stfrom == NULL) + return errno; + + stto = fopen(to, "w"); + if (stto == NULL) { + error = errno; + fclose(stfrom); + return error; + } + + line = malloc(16384); + do { + bread = fread(line, 1, 16384, stfrom); + dummy = fwrite(line, 1, bread, stto); + if (bread != dummy) { + error = errno; + fclose(stfrom); + fclose(stto); + unlink(to); + free(line); + return error; + } + } while (bread != 0); + free(line); + fclose(stfrom); + if (fclose(stto) != 0) { + error = errno; + unlink(to); + return error; + } + + /* + * copy successfull, now copy file- and modification-time + */ + if (stat(from, &sb) == 0) { + ut.actime = mktime(localtime(&sb.st_atime)); + ut.modtime = mktime(localtime(&sb.st_mtime)); + if (utime(to, &ut) != 0) { + error = errno; + unlink(to); + return error; + } + chmod(to, sb.st_mode); + } + + return 0; +} + + + long millis(void) { struct timespec now;