Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / examples / msg / io / file_unlink.c
1 /* Copyright (c) 2008-2010, 2012-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /** @addtogroup MSG_examples
8  * 
9  * @subsection MSG_ex_resources Other resource kinds
10  * 
11  * This section contains some sparse examples of how to use the other
12  * kind of resources, such as disk or GPU. These resources are quite
13  * experimental for now, but here we go anyway.
14  * 
15  * - <b>io/file.c</b> Example with the disk resource
16  */
17
18 #define FILENAME1 "./doc/simgrid/examples/platforms/g5k.xml"
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include "msg/msg.h"
23 #include "surf/surf_private.h"
24
25 int host(int argc, char *argv[]);
26
27 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
28                              "Messages specific for this io example");
29
30 int host(int argc, char *argv[])
31 {
32   msg_file_t file = NULL;
33   char* mount = xbt_strdup("/home");
34   sg_size_t write;
35
36   // First open
37   XBT_INFO("\tOpen file '%s'",FILENAME1);
38   file = MSG_file_open(mount,FILENAME1, NULL);
39
40   // Unlink the file
41   XBT_INFO("\tUnlink file '%s'",MSG_file_get_name(file));
42   MSG_file_unlink(file);
43
44   // Re Open the file wich is in fact created
45   XBT_INFO("\tOpen file '%s'",FILENAME1);
46   file = MSG_file_open(mount,FILENAME1, NULL);
47
48   // Write into the new file
49   write = MSG_file_write(file,100000);  // Write for 100Ko
50   XBT_INFO("\tHave written %llu on %s",write,MSG_file_get_name(file));
51
52   // Close the file
53   XBT_INFO("\tClose file '%s'",MSG_file_get_name(file));
54   MSG_file_close(file);
55
56   xbt_dict_t dict_ls;
57   char* key;
58   surf_stat_t data = NULL;
59   xbt_dict_cursor_t cursor = NULL;
60
61   dict_ls = MSG_file_ls(mount,"./");
62   XBT_INFO(" ");XBT_INFO("ls ./");
63   xbt_dict_foreach(dict_ls,cursor,key,data){
64     if(data) XBT_INFO("FILE : %s",key);
65     else     XBT_INFO("DIR  : %s",key);
66   }
67   xbt_dict_free(&dict_ls);
68
69   dict_ls = MSG_file_ls(mount,"./doc/simgrid/examples/platforms/");
70   XBT_INFO(" ");XBT_INFO("ls ./doc/simgrid/examples/platforms/");
71   xbt_dict_foreach(dict_ls,cursor,key,data){
72     if(data) XBT_INFO("FILE : %s",key);
73     else     XBT_INFO("DIR  : %s",key);
74   }
75   xbt_dict_free(&dict_ls);
76
77   dict_ls = MSG_file_ls(mount,"./doc/simgrid/examples/msg/");
78   XBT_INFO(" ");XBT_INFO("ls ./doc/simgrid/examples/msg/");
79   xbt_dict_foreach(dict_ls,cursor,key,data){
80     if(data) XBT_INFO("FILE : %s",key);
81     else     XBT_INFO("DIR  : %s",key);
82   }
83   xbt_dict_free(&dict_ls);
84
85   free(mount);
86
87   return 0;
88 }
89
90 int main(int argc, char **argv)
91 {
92   int res;
93   MSG_init(&argc, argv);
94   MSG_create_environment(argv[1]);
95   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
96   MSG_function_register("host", host);
97   unsigned long nb_hosts = xbt_dynar_length(hosts);
98   XBT_INFO("Number of host '%lu'",nb_hosts);
99   char* name_host = xbt_strdup("0");
100   MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
101   free(name_host);
102
103   xbt_dynar_free(&hosts);
104
105   res = MSG_main();
106   XBT_INFO("Simulation time %g", MSG_get_clock());
107   if (res == MSG_OK)
108     return 0;
109   else
110     return 1;
111
112 }