Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Change sg_storage_size_t to sg_size_t and fix surf network bug
[simgrid.git] / examples / msg / io / file_unlink.c
1 /* Copyright (c) 2008-2010, 2012-2013. 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 #include "inttypes.h"
25
26 int host(int argc, char *argv[]);
27
28 XBT_LOG_NEW_DEFAULT_CATEGORY(io_file,
29                              "Messages specific for this io example");
30
31 int host(int argc, char *argv[])
32 {
33   msg_file_t file = NULL;
34   char* mount = xbt_strdup("/home");
35   sg_size_t write;
36
37   // First open
38   XBT_INFO("\tOpen file '%s'",FILENAME1);
39   file = MSG_file_open(mount,FILENAME1, NULL);
40
41   // Unlink the file
42   XBT_INFO("\tUnlink file '%s'",file->fullname);
43   MSG_file_unlink(file);
44
45   // Re Open the file wich is in fact created
46   XBT_INFO("\tOpen file '%s'",FILENAME1);
47   file = MSG_file_open(mount,FILENAME1, NULL);
48
49   // Write into the new file
50   write = MSG_file_write(file,100000);  // Write for 100Ko
51   XBT_INFO("\tHave written %" PRIu64 " on %s",write,file->fullname);
52
53   // Close the file
54   XBT_INFO("\tClose file '%s'",file->fullname);
55   MSG_file_close(file);
56
57   xbt_dict_t dict_ls;
58   char* key;
59   surf_stat_t data = NULL;
60   xbt_dict_cursor_t cursor = NULL;
61
62   dict_ls = MSG_file_ls(mount,"./");
63   XBT_INFO(" ");XBT_INFO("ls ./");
64   xbt_dict_foreach(dict_ls,cursor,key,data){
65     if(data) XBT_INFO("FILE : %s",key);
66     else     XBT_INFO("DIR  : %s",key);
67   }
68   xbt_dict_free(&dict_ls);
69
70   dict_ls = MSG_file_ls(mount,"./doc/simgrid/examples/platforms/");
71   XBT_INFO(" ");XBT_INFO("ls ./doc/simgrid/examples/platforms/");
72   xbt_dict_foreach(dict_ls,cursor,key,data){
73     if(data) XBT_INFO("FILE : %s",key);
74     else     XBT_INFO("DIR  : %s",key);
75   }
76   xbt_dict_free(&dict_ls);
77
78   dict_ls = MSG_file_ls(mount,"./doc/simgrid/examples/msg/");
79   XBT_INFO(" ");XBT_INFO("ls ./doc/simgrid/examples/msg/");
80   xbt_dict_foreach(dict_ls,cursor,key,data){
81     if(data) XBT_INFO("FILE : %s",key);
82     else     XBT_INFO("DIR  : %s",key);
83   }
84   xbt_dict_free(&dict_ls);
85
86   free(mount);
87
88   return 0;
89 }
90
91 int main(int argc, char **argv)
92 {
93   int res;
94   MSG_init(&argc, argv);
95   MSG_create_environment(argv[1]);
96   xbt_dynar_t hosts =  MSG_hosts_as_dynar();
97   MSG_function_register("host", host);
98   unsigned long nb_hosts = xbt_dynar_length(hosts);
99   XBT_INFO("Number of host '%lu'",nb_hosts);
100   char* name_host = xbt_strdup("0");
101   MSG_process_create( name_host, host, NULL, xbt_dynar_get_as(hosts,0,msg_host_t) );
102   free(name_host);
103
104   xbt_dynar_free(&hosts);
105
106   res = MSG_main();
107   XBT_INFO("Simulation time %g", MSG_get_clock());
108   if (res == MSG_OK)
109     return 0;
110   else
111     return 1;
112
113 }