Supports hardlinks in both Unix and Windows.

This commit is contained in:
Andrea Mazzoleni 2013-01-27 18:06:15 +01:00
parent 3cb0a2b8b4
commit cc21721b2a
13 changed files with 296 additions and 72 deletions

View file

@ -3,7 +3,7 @@ SnapRAID HISTORY
2.2 2013/2
==========
* Syncs and fixes also empty directories.
* Added support also for hardlinks and empty directories.
2.1 2013/1
==========

View file

@ -105,13 +105,22 @@ check-local:
$(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS) --test-skip-self -c $(srcdir)/test-raid5.conf -a check
$(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS) --test-skip-self -c $(srcdir)/test-raid6.conf check
$(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS) --test-skip-self -c $(srcdir)/test-raid6.conf -a check
echo --- Create some more files and empty directories, sync raid5 and check
echo --- Create some more files, hardlinks and empty directories, sync raid5 and check
$(TESTENV) ./mktest$(EXEEXT) generate 2 3 $(CHECKCOUNT) $(CHECKSIZE)
mkdir test/disk1/empty1
mkdir test/disk2/empty2
mkdir test/disk3/empty3a
mkdir test/disk3/empty3a/emptyinner
mkdir test/disk3/empty3b
echo 1 > test/disk1/file_hardlink1
ln test/disk1/file_hardlink1 test/disk1/file_hardlink2
ln test/disk1/file_hardlink1 test/disk1/file_hardlink3
echo 2 > test/disk2/file_hardlink1
ln test/disk2/file_hardlink1 test/disk2/file_hardlink2
ln test/disk2/file_hardlink1 test/disk2/file_hardlink3
echo 3 > test/disk3/file_hardlink1
ln test/disk3/file_hardlink1 test/disk3/file_hardlink2
ln test/disk3/file_hardlink1 test/disk3/file_hardlink3
$(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS) --test-skip-self -v -c $(srcdir)/test-raid5.conf diff
$(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS) --test-skip-self -v -c $(srcdir)/test-raid5.conf dup
$(TESTENV) ./snapraid$(EXEEXT) $(CHECKFLAGS) --test-skip-self -v -c $(srcdir)/test-raid5.conf sync

130
check.c
View file

@ -1030,7 +1030,10 @@ static int state_check_process(struct snapraid_state* state, int check, int fix,
node = disk->linklist;
while (node) {
char path[PATH_MAX];
char pathto[PATH_MAX];
char linkto[PATH_MAX];
struct stat st;
struct stat stto;
struct snapraid_link* link;
int failed = 0;
@ -1042,29 +1045,69 @@ static int state_check_process(struct snapraid_state* state, int check, int fix,
continue;
}
/* read the symlink */
pathprint(path, sizeof(path), "%s%s", disk->dir, link->sub);
ret = readlink(path, linkto, sizeof(linkto));
if (ret < 0) {
failed = 1;
fprintf(stderr, "Error reading symlink '%s'. %s.\n", path, strerror(errno));
fprintf(stderr, "symlink_error:%s:%s: Symlink read error\n", disk->name, link->sub);
++error;
} else if (ret == sizeof(linkto)) {
failed = 1;
fprintf(stderr, "Error reading symlink '%s'. Symlink too long.\n", path);
fprintf(stderr, "symlink_error:%s:%s: Symlink read error\n", disk->name, link->sub);
++error;
} else {
linkto[ret] = 0;
if (strcmp(linkto, link->linkto) != 0) {
if (link_is_hardlink(link)) {
/* stat the link */
pathprint(path, sizeof(path), "%s%s", disk->dir, link->sub);
ret = stat(path, &st);
if (ret == -1) {
failed = 1;
fprintf(stderr, "symlink_error:%s:%s: Symlink data error '%s' instead of '%s'\n", disk->name, link->sub, linkto, link->linkto);
fprintf(stderr, "Error stating hardlink '%s'. %s.\n", path, strerror(errno));
fprintf(stderr, "hardlinkerror:%s:%s:%s: Hardlink stat error\n", disk->name, link->sub, link->linkto);
++error;
} else if (!S_ISREG(st.st_mode)) {
failed = 1;
fprintf(stderr, "hardlinkerror:%s:%s:%s: Hardlink error for not regular file\n", disk->name, link->sub, link->linkto);
++error;
}
/* stat the "to" file */
pathprint(pathto, sizeof(pathto), "%s%s", disk->dir, link->linkto);
ret = stat(pathto, &stto);
if (ret == -1) {
failed = 1;
fprintf(stderr, "Error stating hardlink-to '%s'. %s.\n", pathto, strerror(errno));
fprintf(stderr, "hardlinkerror:%s:%s:%s: Hardlink to stat error\n", disk->name, link->sub, link->linkto);
++error;
} else if (!S_ISREG(st.st_mode)) {
failed = 1;
fprintf(stderr, "hardlinkerror:%s:%s:%s: Hardlink-to error for not regular file\n", disk->name, link->sub, link->linkto);
++error;
} else if (st.st_ino != stto.st_ino) {
failed = 1;
fprintf(stderr, "Mismatch hardlink '%s' and '%s'. Different inode.\n", path, pathto);
fprintf(stderr, "hardlinkerror:%s:%s:%s: Hardlink mismatch for different inode\n", disk->name, link->sub, link->linkto);
++error;
}
} else {
/* read the symlink */
pathprint(path, sizeof(path), "%s%s", disk->dir, link->sub);
ret = readlink(path, linkto, sizeof(linkto));
if (ret < 0) {
failed = 1;
fprintf(stderr, "Error reading symlink '%s'. %s.\n", path, strerror(errno));
fprintf(stderr, "symlinkerror:%s:%s: Symlink read error\n", disk->name, link->sub);
++error;
} else if (ret == sizeof(linkto)) {
failed = 1;
fprintf(stderr, "Error reading symlink '%s'. Symlink too long.\n", path);
fprintf(stderr, "symlinkerror:%s:%s: Symlink read error\n", disk->name, link->sub);
++error;
} else {
linkto[ret] = 0;
if (strcmp(linkto, link->linkto) != 0) {
failed = 1;
fprintf(stderr, "symlinkerror:%s:%s: Symlink data error '%s' instead of '%s'\n", disk->name, link->sub, linkto, link->linkto);
++error;
}
}
}
@ -1089,22 +1132,41 @@ static int state_check_process(struct snapraid_state* state, int check, int fix,
}
/* create it */
ret = symlink(link->linkto, path);
if (ret != 0) {
fprintf(stderr, "Error writing symlink '%s'. %s.\n", path, strerror(errno));
if (errno == EACCES) {
fprintf(stderr, "WARNING! Please give write permission to the symlink.\n");
} else {
/* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
if (link_is_hardlink(link)) {
ret = hardlink(pathto, path);
if (ret != 0) {
fprintf(stderr, "Error writing hardlink '%s'. %s.\n", path, strerror(errno));
if (errno == EACCES) {
fprintf(stderr, "WARNING! Please give write permission to the hardlink.\n");
} else {
/* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
}
printf("Stopping\n");
++unrecoverable_error;
goto bail;
}
printf("Stopping\n");
++unrecoverable_error;
goto bail;
}
fprintf(stderr, "symlink_fixed:%s:%s: Fixed symlink error\n", disk->name, link->sub);
++recovered_error;
fprintf(stderr, "hardlinkfixed:%s:%s: Fixed hardlink error\n", disk->name, link->sub);
++recovered_error;
} else {
ret = symlink(link->linkto, path);
if (ret != 0) {
fprintf(stderr, "Error writing symlink '%s'. %s.\n", path, strerror(errno));
if (errno == EACCES) {
fprintf(stderr, "WARNING! Please give write permission to the symlink.\n");
} else {
/* we do not use DANGER because it could be ENOSPC which is not always correctly reported */
fprintf(stderr, "WARNING! Without a working data disk, it isn't possible to fix errors on it.\n");
}
printf("Stopping\n");
++unrecoverable_error;
goto bail;
}
fprintf(stderr, "symlinkfixed:%s:%s: Fixed symlink error\n", disk->name, link->sub);
++recovered_error;
}
}
}

4
elem.c
View file

@ -349,14 +349,14 @@ int file_path_compare(const void* void_arg, const void* void_data)
return strcmp(arg, file->sub);
}
struct snapraid_link* link_alloc(const char* sub, const char* linkto)
struct snapraid_link* link_alloc(const char* sub, const char* linkto, int is_hardlink)
{
struct snapraid_link* link;
link = malloc_nofail(sizeof(struct snapraid_link));
pathcpy(link->sub, sizeof(link->sub), sub);
pathcpy(link->linkto, sizeof(link->linkto), linkto);
link->flag = 0;
link->flag = is_hardlink != 0 ? FILE_IS_HARDLINK : 0;
return link;
}

12
elem.h
View file

@ -136,6 +136,7 @@ struct snapraid_block {
#define FILE_IS_LARGER 4 /**< If a larger file was already detected. */
#define FILE_IS_DAMAGED 8 /**< If a fix was attempted but it failed. */
#define FILE_IS_FIXED 16 /**< If a fix was done. */
#define FILE_IS_HARDLINK 32 /**< If it's an hardlink. */
/**
* Value used to mark files without the nanoseconds value.
@ -187,7 +188,6 @@ struct snapraid_dir {
tommy_hashdyn_node nodeset;
};
/**
* Disk.
*/
@ -415,10 +415,18 @@ static inline void link_flag_clear(struct snapraid_link* link, unsigned mask)
link->flag &= ~mask;
}
/**
* Checks if the link is a hardlink. Otherwise it's a symbolic link.
*/
static inline int link_is_hardlink(struct snapraid_link* link)
{
return link_flag_has(link, FILE_IS_HARDLINK);
}
/**
* Allocates a link.
*/
struct snapraid_link* link_alloc(const char* name, const char* link);
struct snapraid_link* link_alloc(const char* name, const char* link, int is_hardlink);
/**
* Deallocates a link.

12
mingw.c
View file

@ -41,6 +41,8 @@ typedef struct _FILE_ATTRIBUTE_TAG_INFO {
#define IO_REPARSE_TAG_SYMLINK (0xA000000C)
#endif
BOOL WINAPI CreateHardLinkW(LPCWSTR lpFileName, LPCWSTR lpExistingFileName, LPSECURITY_ATTRIBUTES lpSecurityAttributes);
/**
* Number of conversion buffers.
*/
@ -799,5 +801,15 @@ void windows_sleep(unsigned seconds)
Sleep(seconds * 1000);
}
int windows_link(const char* existing, const char* file)
{
if (!CreateHardLinkW(convert(file), convert(existing), 0)) {
windows_errno(GetLastError());
return -1;
}
return 0;
}
#endif

View file

@ -62,6 +62,7 @@
#define S_ISLNK(mode) 0
#define readlink(a,b,c) -1
#define symlink(a,b) -1
#define link windows_link
/**
* Generic stat information.
@ -225,6 +226,11 @@ const char* windows_stat_desc(struct stat* st);
*/
void windows_sleep(unsigned seconds);
/**
* Like link().
*/
int windows_link(const char* existing, const char* file);
#endif
#endif

View file

@ -185,5 +185,13 @@ static inline const char* stat_desc(struct stat* st)
}
#endif
/**
* Another name for link() to avoid confusion with local variables called "link".
*/
static inline int hardlink(const char* a, const char* b)
{
return link(a, b);
}
#endif

26
scan.c
View file

@ -142,6 +142,9 @@ static void scan_file_insert(struct snapraid_state* state, struct snapraid_disk*
tommy_list_insert_tail(&disk->filelist, &file->nodelist, file);
}
/* Forward declaration */
static void scan_link(struct snapraid_scan* scan, struct snapraid_state* state, int output, struct snapraid_disk* disk, const char* sub, const char* linkto, int is_hardlink);
/**
* Processes a file.
*/
@ -162,7 +165,8 @@ static void scan_file(struct snapraid_scan* scan, struct snapraid_state* state,
/* check if multiple files have the same inode */
if (file_flag_has(file, FILE_IS_PRESENT)) {
if (st->st_nlink > 1) {
printf("warning: Ignored hardlink '%s%s'\n", disk->dir, sub);
/* it's a hardlink */
scan_link(scan, state, output, disk, sub, file->sub, 1);
return;
} else {
fprintf(stderr, "Internal inode '%"PRIu64"' inconsistency for file '%s%s'\n", (uint64_t)st->st_ino, disk->dir, sub);
@ -363,7 +367,7 @@ static void scan_link_insert(struct snapraid_state* state, struct snapraid_disk*
/**
* Processes a symbolic link.
*/
static void scan_link(struct snapraid_scan* scan, struct snapraid_state* state, int output, struct snapraid_disk* disk, const char* sub, const char* linkto)
static void scan_link(struct snapraid_scan* scan, struct snapraid_state* state, int output, struct snapraid_disk* disk, const char* sub, const char* linkto, int is_hardlink)
{
struct snapraid_link* link;
@ -372,15 +376,18 @@ static void scan_link(struct snapraid_scan* scan, struct snapraid_state* state,
if (link) {
/* check if multiple files have the same name */
if (link_flag_has(link, FILE_IS_PRESENT)) {
fprintf(stderr, "Internal inconsistency for symlink '%s%s'\n", disk->dir, sub);
if (link_is_hardlink(link))
fprintf(stderr, "Internal inconsistency for hardlink '%s%s'\n", disk->dir, sub);
else
fprintf(stderr, "Internal inconsistency for symlink '%s%s'\n", disk->dir, sub);
exit(EXIT_FAILURE);
}
/* mark as present */
link_flag_set(link, FILE_IS_PRESENT);
/* check if the link is not changed */
if (strcmp(link->linkto, linkto) == 0) {
/* check if the link is not changed and it's of the same kind */
if (strcmp(link->linkto, linkto) == 0 && is_hardlink == link_is_hardlink(link)) {
/* it's equal */
++scan->count_equal;
@ -405,6 +412,10 @@ static void scan_link(struct snapraid_scan* scan, struct snapraid_state* state,
/* update it */
pathcpy(link->linkto, sizeof(link->linkto), linkto);
if (is_hardlink)
link_flag_set(link, FILE_IS_HARDLINK);
else
link_flag_clear(link, FILE_IS_HARDLINK);
/* nothing more to do */
return;
@ -425,7 +436,7 @@ static void scan_link(struct snapraid_scan* scan, struct snapraid_state* state,
}
/* insert it */
link = link_alloc(sub, linkto);
link = link_alloc(sub, linkto, is_hardlink);
/* mark it as present */
link_flag_set(link, FILE_IS_PRESENT);
@ -635,7 +646,8 @@ static int scan_dir(struct snapraid_scan* scan, struct snapraid_state* state, in
/* readlink doesn't put the final 0 */
subnew[ret] = 0;
scan_link(scan, state, output, disk, sub_next, subnew);
/* process as a symbolic link */
scan_link(scan, state, output, disk, sub_next, subnew, 0);
processed = 1;
} else {
if (state->verbose) {

View file

@ -113,8 +113,8 @@ It\(cqs able to recover damages only from up to two disks.
With a Backup you are able to recover from a complete
failure of the whole disk array.
.IP \(bu
Only file data, time and symlinks are saved. Permissions,
ownership, extended attributes and hard\(hylinks are not saved.
Only file, timestamps, symlinks and hardlinks are saved.
Permissions, ownership, extended attributes are not saved.
.PD
.SH GETTING STARTED
To use SnapRAID you need to first select one disk of your disk array
@ -969,8 +969,10 @@ This field is used only when you interrupt manually the \(a"sync\(a"
command.
.SS symlink DISK PATH
Defines a symbolic link in the specified DISK.
.SS hardlink DISK PATH
Defines a hard link in the specified DISK.
.SS to PATH
Defines where the previous symlink links to.
Defines where the previous symlink or hardlink links to.
.SS dir DISK PATH
Defines an empty directory in the specified DISK.
.PP

View file

@ -70,8 +70,8 @@ Limitations
* It's able to recover damages only from up to two disks.
With a Backup you are able to recover from a complete
failure of the whole disk array.
* Only file data, time and symlinks are saved. Permissions,
ownership, extended attributes and hard-links are not saved.
* Only file, timestamps, symlinks and hardlinks are saved.
Permissions, ownership, extended attributes are not saved.
Getting Started
To use SnapRAID you need to first select one disk of your disk array
@ -713,8 +713,11 @@ Content
symlink DISK PATH
Defines a symbolic link in the specified DISK.
hardlink DISK PATH
Defines a hard link in the specified DISK.
to PATH
Defines where the previous symlink links to.
Defines where the previous symlink or hardlink links to.
dir DISK PATH
Defines an empty directory in the specified DISK.

View file

@ -80,8 +80,8 @@ Other limitations are:
* It's able to recover damages only from up to two disks.
With a Backup you are able to recover from a complete
failure of the whole disk array.
* Only file data, time and symlinks are saved. Permissions,
ownership, extended attributes and hard-links are not saved.
* Only file, timestamps, symlinks and hardlinks are saved.
Permissions, ownership, extended attributes are not saved.
4 GETTING STARTED
@ -794,12 +794,17 @@ command.
Defines a symbolic link in the specified DISK.
10.13 to PATH
10.13 hardlink DISK PATH
------------------------
Defines a hard link in the specified DISK.
10.14 to PATH
-------------
Defines where the previous symlink links to.
Defines where the previous symlink or hardlink links to.
10.14 dir DISK PATH
10.15 dir DISK PATH
-------------------
Defines an empty directory in the specified DISK.

125
state.c
View file

@ -690,14 +690,16 @@ void state_read(struct snapraid_state* state)
unsigned line;
unsigned count_file;
unsigned count_block;
unsigned count_link;
unsigned count_symlink;
unsigned count_hardlink;
unsigned count_dir;
tommy_node* node;
oathash_t hash;
count_file = 0;
count_block = 0;
count_link = 0;
count_symlink = 0;
count_hardlink = 0;
count_dir = 0;
hash = 0;
@ -1118,17 +1120,102 @@ void state_read(struct snapraid_state* state)
exit(EXIT_FAILURE);
}
/* allocate the link */
link = link_alloc(sub, linkto);
/* allocate the link as symbolic link */
link = link_alloc(sub, linkto, 0);
/* insert the link in the link containers */
tommy_hashdyn_insert(&disk->linkset, &link->nodeset, link, link_name_hash(link->sub));
tommy_list_insert_tail(&disk->linklist, &link->nodelist, link);
/* stat */
++count_link;
++count_symlink;
} else if (strcmp(tag, "hardlink") == 0) {
/* hardlink */
char sub[PATH_MAX];
char linkto[PATH_MAX];
char tokento[32];
tommy_node* i;
struct snapraid_link* link;
hash = oathash8(hash, 'a');
ret = sgettok(f, buffer, sizeof(buffer));
if (ret < 0) {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
hash = oathashs(hash, buffer);
c = sgetc(f);
if (c != ' ') {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
ret = sgetline(f, sub, sizeof(sub));
if (ret < 0) {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
hash = oathashs(hash, sub);
c = sgeteol(f);
if (c != '\n') {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
++line;
ret = sgettok(f, tokento, sizeof(tokento));
if (ret < 0) {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
if (strcmp(tokento, "to") != 0) {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
c = sgetc(f);
if (c != ' ') {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
ret = sgetline(f, linkto, sizeof(linkto));
if (ret < 0) {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
hash = oathashs(hash, linkto);
if (!*sub || !*linkto) {
fprintf(stderr, "Invalid 'hardlink' specification in '%s' at line %u\n", path, line);
exit(EXIT_FAILURE);
}
/* find the disk */
for(i=state->disklist;i!=0;i=i->next) {
disk = i->data;
if (strcmp(disk->name, buffer) == 0)
break;
}
if (!i) {
fprintf(stderr, "Disk named '%s' not found in '%s' at line %u\n", buffer, path, line);
exit(EXIT_FAILURE);
}
/* allocate the link as hard link */
link = link_alloc(sub, linkto, 1);
/* insert the link in the link containers */
tommy_hashdyn_insert(&disk->linkset, &link->nodeset, link, link_name_hash(link->sub));
tommy_list_insert_tail(&disk->linklist, &link->nodelist, link);
/* stat */
++count_hardlink;
} else if (strcmp(tag, "dir") == 0) {
/* symlink */
/* dir */
char sub[PATH_MAX];
tommy_node* i;
struct snapraid_dir* dir;
@ -1312,7 +1399,8 @@ void state_read(struct snapraid_state* state)
if (state->verbose) {
printf("\tfile %u\n", count_file);
printf("\tblock %u\n", count_block);
printf("\tsymlink %u\n", count_link);
printf("\tsymlink %u\n", count_symlink);
printf("\thardlink %u\n", count_hardlink);
printf("\temptydir %u\n", count_dir);
}
}
@ -1322,7 +1410,8 @@ void state_write(struct snapraid_state* state)
STREAM* f;
unsigned count_file;
unsigned count_block;
unsigned count_link;
unsigned count_symlink;
unsigned count_hardlink;
unsigned count_dir;
unsigned count_content;
tommy_node* i;
@ -1331,7 +1420,8 @@ void state_write(struct snapraid_state* state)
count_file = 0;
count_block = 0;
count_link = 0;
count_symlink = 0;
count_hardlink = 0;
count_dir = 0;
hash = 0;
@ -1504,8 +1594,16 @@ void state_write(struct snapraid_state* state)
for(j=disk->linklist;j!=0;j=j->next) {
struct snapraid_link* link = j->data;
sputsl("symlink ", f);
hash = oathash8(hash, 's');
if (link_is_hardlink(link)) {
sputsl("hardlink ", f);
hash = oathash8(hash, 'a');
++count_hardlink;
} else {
sputsl("symlink ", f);
hash = oathash8(hash, 's');
++count_symlink;
}
sputs(disk->name, f);
hash = oathashs(hash, disk->name);
sputc(' ', f);
@ -1520,8 +1618,6 @@ void state_write(struct snapraid_state* state)
fprintf(stderr, "Error writing the content file '%s'. %s.\n", serrorfile(f), strerror(errno));
exit(EXIT_FAILURE);
}
++count_link;
}
/* for each dir */
@ -1629,7 +1725,8 @@ void state_write(struct snapraid_state* state)
if (state->verbose) {
printf("\tfile %u\n", count_file);
printf("\tblock %u\n", count_block);
printf("\tsymlink %u\n", count_link);
printf("\tsymlink %u\n", count_symlink);
printf("\thardlink %u\n", count_hardlink);
printf("\temptydir %u\n", count_dir);
}