1
0
mirror of https://github.com/weechat/weechat.git synced 2026-06-29 22:36:38 +02:00

api: add function file_compare (issue #2250)

This commit is contained in:
Sébastien Helleu
2025-05-07 20:42:46 +02:00
parent 9a661aecd0
commit 51d24fd2da
11 changed files with 391 additions and 7 deletions
+124
View File
@@ -1431,3 +1431,127 @@ dir_file_compress (const char *filename_input,
return 0;
}
}
/*
* Compares the content of two files (the attributes of the files like the
* permissions or timestamp are ignored).
*
* Comparison is done like this:
* 1. if sizes are different, return 1 (different content)
* 2. if sizes are the same, read both files until a difference is found
*
* Returns:
* 0: both files exist and their content is exactly the same
* 1: content is different
* 2: other error (file not found, read error)
*/
int
dir_file_compare (const char *filename1, const char *filename2)
{
char buffer1[4096], buffer2[4096];
FILE *f1, *f2;
struct stat st;
off_t size1, size2;
size_t count1, count2;
int rc, eof1, eof2;
if (!filename1 || !filename1[0] || !filename2 || !filename2[0])
return 2;
rc = 1;
f1 = NULL;
f2 = NULL;
/* get size of first file */
if (access (filename1, R_OK) != 0)
{
rc = 2;
goto end;
}
if (stat (filename1, &st) != 0)
{
rc = 2;
goto end;
}
size1 = st.st_size;
/* get size of second file */
if (access (filename2, R_OK) != 0)
{
rc = 2;
goto end;
}
if (stat (filename2, &st) != 0)
{
rc = 2;
goto end;
}
size2 = st.st_size;
if (size1 != size2)
{
/* different size, so different content */
rc = 1;
goto end;
}
f1 = fopen (filename1, "r");
if (!f1)
{
rc = 2;
goto end;
}
f2 = fopen (filename2, "r");
if (!f2)
{
rc = 2;
goto end;
}
while (1)
{
eof1 = feof (f1);
eof2 = feof (f2);
if ((eof1 && !eof2) || (!eof1 && eof2))
{
/* different content */
rc = 1;
goto end;
}
if (eof1 && eof2)
{
/* same content */
rc = 0;
goto end;
}
count1 = fread (buffer1, sizeof (char), sizeof (buffer1), f1);
count2 = fread (buffer2, sizeof (char), sizeof (buffer2), f2);
if (count1 != count2)
{
/* different content */
rc = 1;
goto end;
}
if ((count1 == 0) || (count2 == 0))
{
/* read error */
rc = 2;
goto end;
}
if (memcmp (buffer1, buffer2, count1) != 0)
{
/* different content */
rc = 1;
goto end;
}
}
end:
if (f1)
fclose (f1);
if (f2)
fclose (f2);
return rc;
}
+1
View File
@@ -41,5 +41,6 @@ extern char *dir_file_get_content (const char *filename);
extern int dir_file_copy (const char *from, const char *to);
extern int dir_file_compress (const char *from, const char *to,
const char *compressor, int compression_level);
extern int dir_file_compare (const char *filename1, const char *filename2);
#endif /* WEECHAT_DIR_H */