Logo AND Algorithmique Numérique Distribuée

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