Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Fix doxygen warnings.
[simgrid.git] / src / msg / msg_io.c
1 /* Copyright (c) 2004-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 #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
23   msg_file_priv_t priv = MSG_file_priv(fd);
24   xbt_dynar_t info = simcall_file_get_info(priv->simdata->smx_file);
25   sg_size_t *psize;
26
27   priv->content_type = xbt_dynar_pop_as(info, char *);
28   priv->storage_type = xbt_dynar_pop_as(info, char *);
29   priv->storageId = xbt_dynar_pop_as(info, char *);
30   priv->mount_point = xbt_dynar_pop_as(info, char *);
31   psize = xbt_dynar_pop_as(info, sg_size_t*);
32   priv->size = *psize;
33   xbt_free(psize);
34   xbt_dynar_free_container(&info);
35 }
36
37 /** \ingroup msg_file_management
38  *
39  * \brief Set the user data of a #msg_file_t.
40  *
41  * This functions checks whether some data has already been associated to \a file
42    or not and attach \a data to \a file if it is possible.
43  */
44 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
45 {
46   msg_file_priv_t priv = MSG_file_priv(fd);
47   priv->data = data;
48   return MSG_OK;
49 }
50
51 /** \ingroup msg_file_management
52  *
53  * \brief Return the user data of a #msg_file_t.
54  *
55  * This functions checks whether \a file is a valid pointer or not and return
56    the user data associated to \a file if it is possible.
57  */
58 void *MSG_file_get_data(msg_file_t fd)
59 {
60   msg_file_priv_t priv = MSG_file_priv(fd);
61   return priv->data;
62 }
63
64 /** \ingroup msg_file_management
65  * \brief Display information related to a file descriptor
66  *
67  * \param fd is a the file descriptor
68  */
69
70 void MSG_file_dump (msg_file_t fd){
71   /* Update the cached information first */
72   __MSG_file_get_info(fd);
73
74   msg_file_priv_t priv = MSG_file_priv(fd);
75   XBT_INFO("File Descriptor information:\n"
76            "\t\tFull name: '%s'\n"
77            "\t\tSize: %llu\n"
78            "\t\tMount point: '%s'\n"
79            "\t\tStorage Id: '%s'\n"
80            "\t\tStorage Type: '%s'\n"
81            "\t\tContent Type: '%s'",
82            priv->fullname, priv->size, priv->mount_point,
83            priv->storageId, priv->storage_type,
84            priv->content_type);
85 }
86
87 /** \ingroup msg_file_management
88  * \brief Read a file
89  *
90  * \param size of the file to read
91  * \param fd is a the file descriptor
92  * \return the number of bytes successfully read
93  */
94 sg_size_t MSG_file_read(msg_file_t fd, sg_size_t size)
95 {
96   msg_file_priv_t priv = MSG_file_priv(fd);
97   return simcall_file_read(priv->simdata->smx_file, size);
98 }
99
100 /** \ingroup msg_file_management
101  * \brief Write into a file
102  *
103  * \param size of the file to write
104  * \param fd is a the file descriptor
105  * \return the number of bytes successfully write
106  */
107 sg_size_t MSG_file_write(msg_file_t fd, sg_size_t size)
108 {
109   msg_file_priv_t priv = MSG_file_priv(fd);
110   return simcall_file_write(priv->simdata->smx_file, size);
111 }
112
113 /** \ingroup msg_file_management
114  * \brief Opens the file whose name is the string pointed to by path
115  *
116  * \param mount is the mount point where find the file is located
117  * \param fullname is the file location on the storage
118  * \param data user data to attach to the file
119  *
120  * \return An #msg_file_t associated to the file
121  */
122 msg_file_t MSG_file_open(const char* mount,const char* fullname, void* data)
123 {
124   msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
125   priv->data = data;
126   priv->fullname = xbt_strdup(fullname);
127   priv->simdata = xbt_new0(s_simdata_file_t,1);
128   priv->simdata->smx_file = simcall_file_open(mount, fullname);
129   xbt_lib_set(file_lib, fullname, MSG_FILE_LEVEL, priv);
130   return (msg_file_t) xbt_lib_get_elm_or_null(file_lib, fullname);
131 }
132
133 /**
134  * \brief Frees private data of a file (internal call only)
135  */
136 void __MSG_file_priv_free(msg_file_priv_t priv)
137 {
138   xbt_free(&priv->simdata->smx_file);
139   free(priv);
140 }
141
142 /** \ingroup msg_file_management
143  * \brief Close the file
144  *
145  * \param fd is the file to close
146  * \return 0 on success or 1 on error
147  */
148 int MSG_file_close(msg_file_t fd)
149 {
150   msg_file_priv_t priv = MSG_file_priv(fd);
151   int res = simcall_file_close(priv->simdata->smx_file);
152   xbt_lib_unset(file_lib, priv->fullname, MSG_FILE_LEVEL, 1);
153   return res;
154 }
155
156 /** \ingroup msg_file_management
157  * \brief Unlink the file pointed by fd
158  *
159  * \param fd is the file descriptor (#msg_file_t)
160  * \return 0 on success or 1 on error
161  */
162 int MSG_file_unlink(msg_file_t fd)
163 {
164   msg_file_priv_t priv = MSG_file_priv(fd);
165   int res = simcall_file_unlink(priv->simdata->smx_file);
166   return res;
167 }
168
169 /** \ingroup msg_file_management
170  * \brief Return the size of a file
171  *
172  * \param fd is the file descriptor (#msg_file_t)
173  * \return the size of the file (as a #sg_size_t)
174  */
175 sg_size_t MSG_file_get_size(msg_file_t fd){
176   msg_file_priv_t priv = MSG_file_priv(fd);
177   return simcall_file_get_size(priv->simdata->smx_file);
178 }
179
180 /** \ingroup msg_file_management
181  * \brief Search for file
182  *
183  * \param mount is the mount point where find the file is located
184  * \param path the file regex to find
185  * \return a xbt_dict_t of file where key is the name of file and the
186  * value the msg_stat_t corresponding to the key
187  */
188 xbt_dict_t MSG_file_ls(const char *mount, const char *path)
189 {
190   xbt_assert(path,"You must set path");
191   int size = strlen(path);
192   if(size && path[size-1] != '/')
193   {
194     char *new_path = bprintf("%s/",path);
195     XBT_DEBUG("Change '%s' for '%s'",path,new_path);
196     xbt_dict_t dict = simcall_file_ls(mount, new_path);
197     xbt_free(new_path);
198     return dict;
199   }
200
201   return simcall_file_ls(mount, path);
202 }
203
204 /**
205  * \ingroup msg_file_management
206  * \brief Set the file position indicator in the msg_file_t by adding offset bytes
207  * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
208  *
209  * \param fd : file object that identifies the stream
210  * \param offset : number of bytes to offset from origin
211  * \param origin : Position used as reference for the offset. It is specified by
212  * one of the following constants defined in \<stdio.h\> exclusively to be used as
213  * arguments for this function (SEEK_SET = beginning of file, SEEK_CUR = current
214  * position of the file pointer, SEEK_END = end of file)
215  *
216  * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns
217  * MSG_TASK_CANCELED (=8).
218  *
219  */
220 msg_error_t MSG_file_seek(msg_file_t fd, sg_size_t offset, int origin)
221 {
222   msg_file_priv_t priv = MSG_file_priv(fd);
223   return simcall_file_seek(priv->simdata->smx_file, offset, origin);
224 }
225
226 /**
227  * \ingroup msg_file_management
228  * \brief Returns the current value of the position indicator of the file
229  *
230  * \param fd : file object that identifies the stream
231  * \return On success, the current value of the position indicator is returned.
232  *
233  */
234 sg_size_t MSG_file_tell(msg_file_t fd)
235 {
236   msg_file_priv_t priv = MSG_file_priv(fd);
237   return simcall_file_tell(priv->simdata->smx_file);
238 }
239
240 const char *MSG_file_get_name(msg_file_t fd) {
241   xbt_assert((fd != NULL), "Invalid parameters");
242   msg_file_priv_t priv = MSG_file_priv(fd);
243   return priv->fullname;
244 }
245
246
247 /*
248  * \brief Destroys a file (internal call only)
249  */
250 void __MSG_file_destroy(msg_file_priv_t file) {
251   xbt_free(file);
252 }
253 /********************************* Storage **************************************/
254
255 /** @addtogroup msg_storage_management
256  * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Storages" --> \endhtmlonly
257  * (#msg_storage_t) and the functions for managing it.
258  *
259  */
260
261 msg_storage_t __MSG_storage_create(smx_storage_t storage)
262 {
263   const char *name = SIMIX_storage_get_name(storage);
264   msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
265   xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
266   return xbt_lib_get_elm_or_null(storage_lib, name);
267 }
268
269 /*
270  * \brief Destroys a storage (internal call only)
271  */
272 void __MSG_storage_destroy(msg_storage_priv_t storage) {
273   free(storage);
274 }
275
276
277 /** \ingroup msg_storage_management
278  *
279  * \brief Returns the name of the #msg_storage_t.
280  *
281  * This functions checks whether a storage is a valid pointer or not and return its name.
282  */
283 const char *MSG_storage_get_name(msg_storage_t storage) {
284   xbt_assert((storage != NULL), "Invalid parameters");
285   return SIMIX_storage_get_name(storage);
286 }
287
288 /** \ingroup msg_storage_management
289  * \brief Returns the free space size of a storage element
290  * \param name the name of a storage
291  * \return the free space size of the storage element (as a #sg_size_t)
292  */
293 sg_size_t MSG_storage_get_free_size(const char* name){
294   return simcall_storage_get_free_size(name);
295 }
296
297 /** \ingroup msg_storage_management
298  * \brief Returns the used space size of a storage element
299  * \param name the name of a storage
300  * \return the used space size of the storage element (as a #sg_size_t)
301  */
302 sg_size_t MSG_storage_get_used_size(const char* name){
303   return simcall_storage_get_used_size(name);
304 }
305
306 /** \ingroup msg_storage_management
307  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
308  * \param storage a storage
309  * \return a dict containing the properties
310  */
311 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
312 {
313   xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
314   return (simcall_storage_get_properties(storage));
315 }
316
317 /** \ingroup msg_storage_management
318  * \brief Change the value of a given storage property
319  *
320  * \param storage a storage
321  * \param name a property name
322  * \param value what to change the property to
323  * \param free_ctn the freeing function to use to kill the value on need
324  */
325 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
326   xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
327 }
328
329 /** \ingroup msg_storage_management
330  * \brief Finds a msg_storage_t using its name.
331  * \param name the name of a storage
332  * \return the corresponding storage
333  */
334 msg_storage_t MSG_storage_get_by_name(const char *name)
335 {
336   return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
337 }
338
339 /** \ingroup msg_storage_management
340  * \brief Returns a dynar containing all the storage elements declared at a given point of time
341  *
342  */
343 xbt_dynar_t MSG_storages_as_dynar(void) {
344
345   xbt_lib_cursor_t cursor;
346   char *key;
347   void **data;
348   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),NULL);
349
350   xbt_lib_foreach(storage_lib, cursor, key, data) {
351     if(routing_get_network_element_type(key) == MSG_STORAGE_LEVEL) {
352       xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
353       xbt_dynar_push(res, &elm);
354     }
355   }
356
357   return res;
358 }
359
360 /** \ingroup msg_storage_management
361  *
362  * \brief Set the user data of a #msg_storage_t.
363  * This functions checks whether some data has already been associated to \a storage
364    or not and attach \a data to \a storage if it is possible.
365  */
366 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
367 {
368   SIMIX_storage_set_data(storage,data);
369
370   return MSG_OK;
371 }
372
373 /** \ingroup msg_host_management
374  *
375  * \brief Returns the user data of a #msg_storage_t.
376  *
377  * This functions checks whether \a storage is a valid pointer or not and returns
378    the user data associated to \a storage if it is possible.
379  */
380 void *MSG_storage_get_data(msg_storage_t storage)
381 {
382   return SIMIX_storage_get_data(storage);
383 }
384
385 /** \ingroup msg_storage_management
386  *
387  * \brief Returns the content (file list) of a #msg_storage_t.
388  * \param storage a storage
389  * \return The content of this storage element as a dict (full path file => size)
390  */
391 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
392 {
393   return SIMIX_storage_get_content(storage);
394 }
395
396 sg_size_t MSG_storage_get_size(msg_storage_t storage)
397 {
398   return SIMIX_storage_get_size(storage);
399 }
400
401 /**
402  * \ingroup msg_storage_management
403  *
404  * \brief Rename the file in the contents of its associated storage.
405  */
406 msg_error_t MSG_storage_file_rename(msg_storage_t storage, const char* src,  const char* dest)
407 {
408   simcall_storage_file_rename(storage, src, dest);
409   return MSG_OK;
410 }
411
412 /**
413  * \ingroup msg_storage_management
414  * \brief Move a file to another location. Depending on the values of dest, dest, mount,
415  * and fullname, this move can be local or remote and, within a host, on the same
416  * mounted disk or between mounted disks.
417  *
418  */
419 msg_error_t MSG_storage_file_move (msg_file_t fd, msg_host_t dest, char* mount, char* fullname)
420 {
421   THROW_UNIMPLEMENTED;
422   return MSG_OK;
423 }