Logo AND Algorithmique Numérique Distribuée

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