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] / src / msg / msg_io.c
1 /* Copyright (c) 2004-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 #include "msg_private.h"
8 #include "xbt/log.h"
9
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg,
11                                 "Logging specific to MSG (io)");
12
13 /** @addtogroup msg_file_management
14  * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Files" --> \endhtmlonly
15  * (#msg_file_t) and the functions for managing it.
16  *
17  *  \see #msg_file_t
18  */
19
20 /********************************* File **************************************/
21 void __MSG_file_get_info(msg_file_t fd){
22   xbt_dynar_t info = simcall_file_get_info(fd->simdata->smx_file);
23   fd->info->content_type = xbt_dynar_pop_as(info, char *);
24   fd->info->storage_type = xbt_dynar_pop_as(info, char *);
25   fd->info->storageId = xbt_dynar_pop_as(info, char *);
26   fd->info->mount_point = xbt_dynar_pop_as(info, char *);
27   fd->info->size = xbt_dynar_pop_as(info, sg_storage_size_t);
28
29   xbt_dynar_free_container(&info);
30 }
31
32 /** \ingroup msg_file_management
33  *
34  * \brief Set the user data of a #msg_file_t.
35  *
36  * This functions checks whether some data has already been associated to \a file
37    or not and attach \a data to \a file if it is possible.
38  */
39 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
40 {
41   SIMIX_file_set_data(fd->simdata->smx_file,data);
42
43   return MSG_OK;
44 }
45
46 /** \ingroup msg_file_management
47  *
48  * \brief Return the user data of a #msg_file_t.
49  *
50  * This functions checks whether \a file is a valid pointer or not and return
51    the user data associated to \a file if it is possible.
52  */
53 void *MSG_file_get_data(msg_file_t fd)
54 {
55   return SIMIX_file_get_data(fd->simdata->smx_file);
56 }
57
58 /** \ingroup msg_file_management
59  * \brief Display information related to a file descriptor
60  *
61  * \param fd is a the file descriptor
62  */
63
64 void MSG_file_dump (msg_file_t fd){
65 //   THROW_UNIMPLEMENTED;
66   /* Update the cached information first */
67   __MSG_file_get_info(fd);
68   XBT_INFO("File Descriptor information:\n\t\tFull name: '%s'"
69       "\n\t\tSize: %zu\n\t\tMount point: '%s'\n\t\tStorage Id: '%s'"
70       "\n\t\tStorage Type: '%s'\n\t\tContent Type: '%s'",
71       fd->fullname, fd->info->size, fd->info->mount_point, fd->info->storageId,
72       fd->info->storage_type, fd->info->content_type);
73 }
74
75 /** \ingroup msg_file_management
76  * \brief Read a file
77  *
78  * \param size of the file to read
79  * \param fd is a the file descriptor
80  * \return the number of bytes successfully read
81  */
82 sg_storage_size_t MSG_file_read(sg_storage_size_t size, msg_file_t fd)
83 {
84   return simcall_file_read(size, fd->simdata->smx_file);
85 }
86
87 /** \ingroup msg_file_management
88  * \brief Write into a file
89  *
90  * \param size of the file to write
91  * \param fd is a the file descriptor
92  * \return the number of bytes successfully write
93  */
94 sg_storage_size_t MSG_file_write(sg_storage_size_t size, msg_file_t fd)
95 {
96   return simcall_file_write(size, fd->simdata->smx_file);
97 }
98
99 /** \ingroup msg_file_management
100  * \brief Opens the file whose name is the string pointed to by path
101  *
102  * \param mount is the mount point where find the file is located
103  * \param fullname is the file location on the storage
104  * \param data user data to attach to the file
105  *
106  * \return An #msg_file_t associated to the file
107  */
108 msg_file_t MSG_file_open(const char* mount, const char* fullname, void* data)
109 {
110   msg_file_t file = xbt_new(s_msg_file_t,1);
111   file->fullname = xbt_strdup(fullname);
112   file->simdata = xbt_new0(s_simdata_file_t,1);
113   file->info = xbt_new0(s_file_info_t,1);
114   file->simdata->smx_file = simcall_file_open(mount, fullname);
115   SIMIX_file_set_data(file->simdata->smx_file, data);
116   return file;
117 }
118
119 /** \ingroup msg_file_management
120  * \brief Close the file
121  *
122  * \param fd is the file to close
123  * \return 0 on success or 1 on error
124  */
125 int MSG_file_close(msg_file_t fd)
126 {
127   int res = simcall_file_close(fd->simdata->smx_file);
128   free(fd->fullname);
129   xbt_free(fd->simdata);
130   xbt_free(fd->info);
131   xbt_free(fd);
132   return res;
133 }
134
135 /** \ingroup msg_file_management
136  * \brief Unlink the file pointed by fd
137  *
138  * \param fd is the file descriptor (#msg_file_t)
139  * \return 0 on success or 1 on error
140  */
141 int MSG_file_unlink(msg_file_t fd)
142 {
143   return simcall_file_unlink(fd->simdata->smx_file);
144 }
145
146 /** \ingroup msg_file_management
147  * \brief Return the size of a file
148  *
149  * \param fd is the file descriptor (#msg_file_t)
150  * \return the size of the file (as a sg_storage_size_t)
151  */
152 sg_storage_size_t MSG_file_get_size(msg_file_t fd){
153   return simcall_file_get_size(fd->simdata->smx_file);
154 }
155
156 /** \ingroup msg_file_management
157  * \brief Search for file
158  *
159  * \param mount is the mount point where find the file is located
160  * \param path the file regex to find
161  * \return a xbt_dict_t of file where key is the name of file and the
162  * value the msg_stat_t corresponding to the key
163  */
164 xbt_dict_t MSG_file_ls(const char *mount, const char *path)
165 {
166   xbt_assert(path,"You must set path");
167   int size = strlen(path);
168   if(size && path[size-1] != '/')
169   {
170     char *new_path = bprintf("%s/",path);
171     XBT_DEBUG("Change '%s' for '%s'",path,new_path);
172     xbt_dict_t dict = simcall_file_ls(mount, new_path);
173     xbt_free(new_path);
174     return dict;
175   }
176
177   return simcall_file_ls(mount, path);
178 }
179
180 /********************************* Storage **************************************/
181
182 /** @addtogroup msg_storage_management
183  * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Storages" --> \endhtmlonly
184  * (#msg_storage_t) and the functions for managing it.
185  *
186  */
187
188
189 /* TODO: PV: to comment */
190 msg_storage_t __MSG_storage_create(smx_storage_t storage)
191 {
192   const char *name = SIMIX_storage_get_name(storage);
193   msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
194   xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
195   return xbt_lib_get_elm_or_null(storage_lib, name);
196 }
197
198 /*
199  * \brief Destroys a storage (internal call only)
200  */
201 void __MSG_storage_destroy(msg_storage_priv_t storage) {
202
203   free(storage);
204 }
205
206 /** \ingroup msg_storage_management
207  *
208  * \brief Returns the name of the #msg_storage_t.
209  *
210  * This functions checks whether a storage is a valid pointer or not and return its name.
211  */
212 const char *MSG_storage_get_name(msg_storage_t storage) {
213   xbt_assert((storage != NULL), "Invalid parameters");
214   return SIMIX_storage_get_name(storage);
215 }
216
217 /** \ingroup msg_storage_management
218  * \brief Returns the free space size of a storage element
219  * \param name the name of a storage
220  * \return the free space size of the storage element (as a sg_storage_size_t)
221  */
222 sg_storage_size_t MSG_storage_get_free_size(const char* name){
223   return simcall_storage_get_free_size(name);
224 }
225
226 /** \ingroup msg_storage_management
227  * \brief Returns the used space size of a storage element
228  * \param name the name of a storage
229  * \return the used space size of the storage element (as a sg_storage_size_t)
230  */
231 sg_storage_size_t MSG_storage_get_used_size(const char* name){
232   return simcall_storage_get_used_size(name);
233 }
234
235 /** \ingroup msg_storage_management
236  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
237  * \param storage a storage
238  * \return a dict containing the properties
239  */
240 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
241 {
242   xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
243   return (simcall_storage_get_properties(storage));
244 }
245
246 /** \ingroup msg_storage_management
247  * \brief Change the value of a given storage property
248  *
249  * \param storage a storage
250  * \param name a property name
251  * \param value what to change the property to
252  * \param free_ctn the freeing function to use to kill the value on need
253  */
254 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
255   xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
256 }
257
258 /** \ingroup msg_storage_management
259  * \brief Finds a msg_storage_t using its name.
260  * \param name the name of a storage
261  * \return the corresponding storage
262  */
263 msg_storage_t MSG_storage_get_by_name(const char *name)
264 {
265   return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
266 }
267
268 /** \ingroup msg_storage_management
269  * \brief Returns a dynar containing all the storage elements declared at a given point of time
270  *
271  */
272 xbt_dynar_t MSG_storages_as_dynar(void) {
273
274   xbt_lib_cursor_t cursor;
275   char *key;
276   void **data;
277   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),NULL);
278
279   xbt_lib_foreach(storage_lib, cursor, key, data) {
280     if(routing_get_network_element_type(key) == MSG_STORAGE_LEVEL) {
281       xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
282       xbt_dynar_push(res, &elm);
283     }
284   }
285
286   return res;
287 }
288
289 /** \ingroup msg_storage_management
290  *
291  * \brief Set the user data of a #msg_storage_t.
292  * This functions checks whether some data has already been associated to \a storage
293    or not and attach \a data to \a storage if it is possible.
294  */
295 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
296 {
297   SIMIX_storage_set_data(storage,data);
298
299   return MSG_OK;
300 }
301
302 /** \ingroup msg_host_management
303  *
304  * \brief Returns the user data of a #msg_storage_t.
305  *
306  * This functions checks whether \a storage is a valid pointer or not and returns
307    the user data associated to \a storage if it is possible.
308  */
309 void *MSG_storage_get_data(msg_storage_t storage)
310 {
311   return SIMIX_storage_get_data(storage);
312 }
313
314 /** \ingroup msg_storage_management
315  *
316  * \brief Returns the content (file list) of a #msg_storage_t.
317  * \param storage a storage
318  * \return The content of this storage element as a dict (full path file => size)
319  */
320 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
321 {
322   return SIMIX_storage_get_content(storage);
323   //return (simcall_storage_get_properties(storage));
324 }