~ubuntu-branches/ubuntu/breezy/fl-cow/breezy

« back to all changes in this revision

Viewing changes to test/flcow-test.c

  • Committer: Bazaar Package Importer
  • Author(s): Robert Collins
  • Date: 2005-05-28 17:27:21 UTC
  • Revision ID: james.westby@ubuntu.com-20050528172721-jj5vkjr77mx2awg7
Tags: upstream-0.4
ImportĀ upstreamĀ versionĀ 0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  FL-COW by Davide Libenzi ( File Links Copy On Write )
 
3
 *  Copyright (C) 2003  Davide Libenzi
 
4
 *
 
5
 *  This program is free software; you can redistribute it and/or modify
 
6
 *  it under the terms of the GNU General Public License as published by
 
7
 *  the Free Software Foundation; either version 2 of the License, or
 
8
 *  (at your option) any later version.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 *
 
19
 *  Davide Libenzi <davidel@xmailserver.org>
 
20
 *
 
21
 */
 
22
 
 
23
#include <stdlib.h>
 
24
#include <string.h>
 
25
#include <unistd.h>
 
26
#include <fcntl.h>
 
27
#include <errno.h>
 
28
#include <sys/types.h>
 
29
#include <sys/time.h>
 
30
#include <sys/stat.h>
 
31
#include <stdio.h>
 
32
#include <time.h>
 
33
 
 
34
 
 
35
 
 
36
#define DUMMY_DATA1 "FLCOW Test Data Before"
 
37
#define DUMMY_DATA2 "FLCOW Test Data After"
 
38
 
 
39
 
 
40
static int create_file(char const *name, char const *data) {
 
41
        int fd;
 
42
 
 
43
        if ((fd = open(name, O_WRONLY | O_CREAT, 0666)) == -1) {
 
44
                perror(name);
 
45
                return -1;
 
46
        }
 
47
        write(fd, data, strlen(data));
 
48
        close(fd);
 
49
 
 
50
        return 0;
 
51
}
 
52
 
 
53
 
 
54
 
 
55
int main(int argc, char **argv) {
 
56
        struct stat stb1, stb2;
 
57
        char buf[512], name1[512], name2[512];
 
58
 
 
59
        getcwd(buf, sizeof(buf) - 1);
 
60
        setenv("FLCOW_PATH", buf, 1);
 
61
 
 
62
        sprintf(name1, ",,flcow-test1++.%u", getpid());
 
63
        if (create_file(name1, DUMMY_DATA1) < 0) {
 
64
                fprintf(stdout, "Test Result\t\t[ FAILED ]\n");
 
65
                return 1;
 
66
        }
 
67
        fprintf(stdout, "File Creation\t\t[ OK ]\n");
 
68
 
 
69
        sprintf(name2, ",,flcow-test2++.%u", getpid());
 
70
        if (link(name1, name2) < 0) {
 
71
                unlink(name1);
 
72
                fprintf(stdout, "Test Result\t\t[ FAILED ]\n");
 
73
                return 2;
 
74
        }
 
75
        fprintf(stdout, "Link Creation\t\t[ OK ]\n");
 
76
 
 
77
        stat(name1, &stb1);
 
78
        stat(name2, &stb2);
 
79
        if (stb1.st_nlink < 2 || stb2.st_nlink < 2) {
 
80
                unlink(name2);
 
81
                unlink(name1);
 
82
                fprintf(stdout, "Link Check\t\t[ FAILED ]\n");
 
83
                return 3;
 
84
        }
 
85
        fprintf(stdout, "Link Check\t\t[ OK ]\n");
 
86
 
 
87
        if (create_file(name1, DUMMY_DATA2) < 0) {
 
88
                unlink(name2);
 
89
                unlink(name1);
 
90
                fprintf(stdout, "File Rewrite\t\t[ FAILED ]\n");
 
91
                return 4;
 
92
        }
 
93
        fprintf(stdout, "File Rewrite\t\t[ OK ]\n");
 
94
 
 
95
        stat(name1, &stb1);
 
96
        stat(name2, &stb2);
 
97
        if (stb1.st_nlink > 1 || stb2.st_nlink > 1) {
 
98
                unlink(name2);
 
99
                unlink(name1);
 
100
                fprintf(stdout, "COW Check\t\t[ FAILED ]\n");
 
101
                return 5;
 
102
        }
 
103
        fprintf(stdout, "COW Check\t\t[ OK ]\n");
 
104
 
 
105
        unlink(name2);
 
106
        unlink(name1);
 
107
 
 
108
        fprintf(stdout, "Test Result\t\t[ OK ]\n");
 
109
 
 
110
        return 0;
 
111
}
 
112