1.1 --- a/sync.cc Fri Dec 13 00:47:13 2019 +0100
1.2 +++ b/sync.cc Sat Dec 14 22:05:53 2019 +0100
1.3 @@ -22,13 +22,21 @@
1.4 #include <stdlib.h>
1.5 #include <stdio.h>
1.6 #include <string.h>
1.7 -#include <unistd.h>
1.8
1.9 #include <fsclient/client.h>
1.10 #include <systypes/fcntl.h>
1.11
1.12
1.13
1.14 +static char *dirname(char *filename)
1.15 +{
1.16 + char *s = strdup(filename);
1.17 + char *t = strrchr(s, '/');
1.18 + if (t != NULL)
1.19 + *t = '\0';
1.20 + return s;
1.21 +}
1.22 +
1.23 static void read_data(file_descriptor_t *desc)
1.24 {
1.25 char buffer[80];
1.26 @@ -49,7 +57,8 @@
1.27 int main(int argc, char *argv[])
1.28 {
1.29 char buffer[] = "Some data written to the file.";
1.30 - file_descriptor_t *desc1 = NULL;
1.31 + file_descriptor_t *desc1 = NULL, *descD = NULL;
1.32 + char *dir = NULL;
1.33
1.34 if (argc < 3)
1.35 {
1.36 @@ -57,10 +66,24 @@
1.37 return 1;
1.38 }
1.39
1.40 + dir = dirname(argv[2]);
1.41 +
1.42 /* Obtain a reference to a file. */
1.43
1.44 if (!strcmp(argv[1], "create"))
1.45 {
1.46 + descD = client_open(dir, O_DIRECTORY, 0);
1.47 +
1.48 + if (descD == NULL)
1.49 + {
1.50 + printf("No such directory %s.\n", dir);
1.51 + return 1;
1.52 + }
1.53 +
1.54 + printf("Waiting to create a file in %s...\n", dir);
1.55 +
1.56 + client_begin(descD);
1.57 +
1.58 printf("Open file...\n");
1.59
1.60 desc1 = client_open(argv[2], O_RDWR | O_CREAT, 0);
1.61 @@ -71,6 +94,8 @@
1.62 return 1;
1.63 }
1.64
1.65 + client_yield(descD);
1.66 +
1.67 printf("Wait for another client...\n");
1.68
1.69 client_begin(desc1);
1.70 @@ -132,17 +157,25 @@
1.71 }
1.72 else if (!strcmp(argv[1], "observe"))
1.73 {
1.74 - /* NOTE: It would be nicer to be able to wait for the file to be created. */
1.75 + descD = client_open(dir, O_DIRECTORY, 0);
1.76
1.77 - printf("Waiting for the file to be created...\n");
1.78 - sleep(2);
1.79 + if (descD == NULL)
1.80 + {
1.81 + printf("No such directory %s.\n", dir);
1.82 + return 1;
1.83 + }
1.84
1.85 - desc1 = client_open(argv[2], O_RDONLY, 0);
1.86 + while (desc1 == NULL)
1.87 + {
1.88 + printf("Waiting for the file to be created in %s...\n", dir);
1.89 +
1.90 + client_begin(descD);
1.91
1.92 - if (desc1 == NULL)
1.93 - {
1.94 - printf("Could not open file.\n");
1.95 - return 1;
1.96 + printf("Try to open the file...\n");
1.97 +
1.98 + desc1 = client_open(argv[2], O_RDONLY, 0);
1.99 +
1.100 + client_yield(descD);
1.101 }
1.102
1.103 printf("Wait for the other client...\n");
1.104 @@ -176,5 +209,7 @@
1.105 client_close(desc1);
1.106
1.107 printf("\nClosed.\n");
1.108 +
1.109 + free(dir);
1.110 return 0;
1.111 }