diff options
author | Lakshmipathi <lakshmipathi.g@gmail.com> | 2011-07-08 11:51:53 +0530 |
---|---|---|
committer | Lakshmipathi <lakshmipathi.g@gmail.com> | 2011-07-08 11:51:53 +0530 |
commit | b47528cc4bb7a308c74d4dae7c2adcf9f7392f4e (patch) | |
tree | 35742effa1325ea42a99492167d16e75feabd373 /community-scripts/rename/atomic/writer.c | |
parent | bb2ca55002b8faac2b4687a97b611724081e601b (diff) |
Added community provided scripts for lock and rename
Diffstat (limited to 'community-scripts/rename/atomic/writer.c')
-rw-r--r-- | community-scripts/rename/atomic/writer.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/community-scripts/rename/atomic/writer.c b/community-scripts/rename/atomic/writer.c new file mode 100644 index 0000000..16d07e1 --- /dev/null +++ b/community-scripts/rename/atomic/writer.c @@ -0,0 +1,48 @@ +/*
+ gcc writer.c -o writer -Wall
+*/
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+
+int main(void)
+{
+ int fd_old, fd_new, ret;
+
+ for (;;) {
+ fd_old = open("dovecot.index", O_RDWR);
+ if (fd_old == -1) {
+ perror("open(dovecot.index");
+ break;
+ }
+
+ usleep(rand() % 1000);
+ fd_new = creat("dovecot.index.tmp", 0600);
+ if (fd_new == -1) {
+ perror("creat(dovecot.index.tmp)");
+ break;
+ }
+ write(fd_new, "foo", 3);
+ close(fd_new);
+
+ ret = link("dovecot.index", "dovecot.index.backup.tmp");
+ if (ret < 0) {
+ perror("link(dovecot.index, dovecot.index.backup.tmp)");
+ break;
+ }
+ if (rename("dovecot.index.backup.tmp", "dovecot.index.backup") < 0) {
+ perror("rename(dovecot.index.backup.tmp, dovecot.index.backup)");
+ break;
+ }
+
+ usleep(rand() % 1000);
+ if (rename("dovecot.index.tmp", "dovecot.index") < 0) {
+ perror("rename(dovecot.index.tmp, dovecot.index)");
+ break;
+ }
+ usleep(rand() % 1000);
+ close(fd_old);
+ }
+ return 0;
+}
|