Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Finalises MSG_file_rcopy() and MSG_file_rmove()
[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 path: '%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->fullpath, 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 fullpath is the file location on the storage
117  * \param data user data to attach to the file
118  *
119  * \return An #msg_file_t associated to the file
120  */
121 msg_file_t MSG_file_open(const char* fullpath, void* data)
122 {
123   msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
124   priv->data = data;
125   priv->fullpath = xbt_strdup(fullpath);
126   priv->simdata = xbt_new0(s_simdata_file_t,1);
127   priv->simdata->smx_file = simcall_file_open(fullpath);
128   xbt_lib_set(file_lib, fullpath, MSG_FILE_LEVEL, priv);
129   return (msg_file_t) xbt_lib_get_elm_or_null(file_lib, fullpath);
130 }
131
132 /**
133  * \brief Frees private data of a file (internal call only)
134  */
135 void __MSG_file_priv_free(msg_file_priv_t priv)
136 {
137   xbt_free(&priv->simdata->smx_file);
138   free(priv);
139 }
140
141 /** \ingroup msg_file_management
142  * \brief Close the file
143  *
144  * \param fd is the file to close
145  * \return 0 on success or 1 on error
146  */
147 int MSG_file_close(msg_file_t fd)
148 {
149   msg_file_priv_t priv = MSG_file_priv(fd);
150   int res = simcall_file_close(priv->simdata->smx_file);
151   xbt_lib_unset(file_lib, priv->fullpath, MSG_FILE_LEVEL, 1);
152   return res;
153 }
154
155 /** \ingroup msg_file_management
156  * \brief Unlink the file pointed by fd
157  *
158  * \param fd is the file descriptor (#msg_file_t)
159  * \return 0 on success or 1 on error
160  */
161 int MSG_file_unlink(msg_file_t fd)
162 {
163   msg_file_priv_t priv = MSG_file_priv(fd);
164   int res = simcall_file_unlink(priv->simdata->smx_file);
165   return res;
166 }
167
168 /** \ingroup msg_file_management
169  * \brief Return the size of a file
170  *
171  * \param fd is the file descriptor (#msg_file_t)
172  * \return the size of the file (as a #sg_size_t)
173  */
174 sg_size_t MSG_file_get_size(msg_file_t fd){
175   msg_file_priv_t priv = MSG_file_priv(fd);
176   return simcall_file_get_size(priv->simdata->smx_file);
177 }
178
179 /** \ingroup msg_file_management
180  * \brief Search for file
181  *
182  * \param mount is the mount point where find the file is located
183  * \param path the file regex to find
184  * \return a xbt_dict_t of file where key is the name of file and the
185  * value the msg_stat_t corresponding to the key
186  */
187 xbt_dict_t MSG_file_ls(const char *mount, const char *path)
188 {
189   xbt_assert(path,"You must set path");
190   int size = strlen(path);
191   if(size && path[size-1] != '/')
192   {
193     char *new_path = bprintf("%s/",path);
194     XBT_DEBUG("Change '%s' for '%s'",path,new_path);
195     xbt_dict_t dict = simcall_file_ls(mount, new_path);
196     xbt_free(new_path);
197     return dict;
198   }
199
200   return simcall_file_ls(mount, path);
201 }
202
203 /**
204  * \ingroup msg_file_management
205  * \brief Set the file position indicator in the msg_file_t by adding offset bytes
206  * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
207  *
208  * \param fd : file object that identifies the stream
209  * \param offset : number of bytes to offset from origin
210  * \param origin : Position used as reference for the offset. It is specified by
211  * one of the following constants defined in \<stdio.h\> exclusively to be used as
212  * arguments for this function (SEEK_SET = beginning of file, SEEK_CUR = current
213  * position of the file pointer, SEEK_END = end of file)
214  *
215  * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns
216  * MSG_TASK_CANCELED (=8).
217  *
218  */
219 msg_error_t MSG_file_seek(msg_file_t fd, sg_size_t offset, int origin)
220 {
221   msg_file_priv_t priv = MSG_file_priv(fd);
222   return simcall_file_seek(priv->simdata->smx_file, offset, origin);
223 }
224
225 /**
226  * \ingroup msg_file_management
227  * \brief Returns the current value of the position indicator of the file
228  *
229  * \param fd : file object that identifies the stream
230  * \return On success, the current value of the position indicator is returned.
231  *
232  */
233 sg_size_t MSG_file_tell(msg_file_t fd)
234 {
235   msg_file_priv_t priv = MSG_file_priv(fd);
236   return simcall_file_tell(priv->simdata->smx_file);
237 }
238
239 const char *MSG_file_get_name(msg_file_t fd) {
240   xbt_assert((fd != NULL), "Invalid parameters");
241   msg_file_priv_t priv = MSG_file_priv(fd);
242   return priv->fullpath;
243 }
244
245 /**
246  * \ingroup msg_file_management
247  * \brief Move a file to another location on the *same mount point*.
248  *
249  */
250 msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
251 {
252   msg_file_priv_t priv = MSG_file_priv(fd);
253   return simcall_file_move(priv->simdata->smx_file, fullpath);
254 }
255
256 /**
257  * \ingroup msg_file_management
258  * \brief Copy a file to another location on a remote host.
259  * \param fd : the file to move
260  * \param host : the remote host where the file has to be copied
261  * \param fullpath : the complete path destination on the remote host
262  * \return If successful, the function returns MSG_OK. Otherwise, it returns
263  * MSG_TASK_CANCELED.
264  */
265 msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
266 {
267   msg_file_priv_t file_priv = MSG_file_priv(file);
268   return simcall_file_rcopy(file_priv->simdata->smx_file, host, fullpath);
269 }
270
271 /**
272  * \ingroup msg_file_management
273  * \brief Move a file to another location on a remote host.
274  * \param fd : the file to move
275  * \param host : the remote host where the file has to be moved
276  * \param fullpath : the complete path destination on the remote host
277  * \return If successful, the function returns MSG_OK. Otherwise, it returns
278  * MSG_TASK_CANCELED.
279  */
280 msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
281 {
282   msg_file_priv_t file_priv = MSG_file_priv(file);
283   msg_error_t res = simcall_file_rcopy(file_priv->simdata->smx_file, host, fullpath);
284   simcall_file_unlink(file_priv->simdata->smx_file);
285   return res;
286 }
287
288 /**
289  * \brief Destroys a file (internal call only)
290  */
291 void __MSG_file_destroy(msg_file_priv_t file) {
292   xbt_free(file->fullpath);
293   xbt_free(file->simdata);
294   xbt_free(file);
295 }
296 /********************************* Storage **************************************/
297
298 /** @addtogroup msg_storage_management
299  * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Storages" --> \endhtmlonly
300  * (#msg_storage_t) and the functions for managing it.
301  *
302  */
303
304 msg_storage_t __MSG_storage_create(smx_storage_t storage)
305 {
306   const char *name = SIMIX_storage_get_name(storage);
307   const char *host = SIMIX_storage_get_host(storage);
308   msg_storage_priv_t storage_private = xbt_new0(s_msg_storage_priv_t, 1);
309   storage_private->host = host;
310   xbt_lib_set(storage_lib,name,MSG_STORAGE_LEVEL,storage_private);
311   return xbt_lib_get_elm_or_null(storage_lib, name);
312 }
313
314 /**
315  * \brief Destroys a storage (internal call only)
316  */
317 void __MSG_storage_destroy(msg_storage_priv_t storage) {
318   free(storage);
319 }
320
321
322 /** \ingroup msg_storage_management
323  *
324  * \brief Returns the name of the #msg_storage_t.
325  *
326  * This functions checks whether a storage is a valid pointer or not and return its name.
327  */
328 const char *MSG_storage_get_name(msg_storage_t storage) {
329   xbt_assert((storage != NULL), "Invalid parameters");
330   return SIMIX_storage_get_name(storage);
331 }
332
333 /** \ingroup msg_storage_management
334  * \brief Returns the free space size of a storage element
335  * \param name the name of a storage
336  * \return the free space size of the storage element (as a #sg_size_t)
337  */
338 sg_size_t MSG_storage_get_free_size(const char* name){
339   return simcall_storage_get_free_size(name);
340 }
341
342 /** \ingroup msg_storage_management
343  * \brief Returns the used space size of a storage element
344  * \param name the name of a storage
345  * \return the used space size of the storage element (as a #sg_size_t)
346  */
347 sg_size_t MSG_storage_get_used_size(const char* name){
348   return simcall_storage_get_used_size(name);
349 }
350
351 /** \ingroup msg_storage_management
352  * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
353  * \param storage a storage
354  * \return a dict containing the properties
355  */
356 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
357 {
358   xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
359   return (simcall_storage_get_properties(storage));
360 }
361
362 /** \ingroup msg_storage_management
363  * \brief Change the value of a given storage property
364  *
365  * \param storage a storage
366  * \param name a property name
367  * \param value what to change the property to
368  * \param free_ctn the freeing function to use to kill the value on need
369  */
370 void MSG_storage_set_property_value(msg_storage_t storage, const char *name, char *value,void_f_pvoid_t free_ctn) {
371   xbt_dict_set(MSG_storage_get_properties(storage), name, value,free_ctn);
372 }
373
374 /** \ingroup msg_storage_management
375  * \brief Finds a msg_storage_t using its name.
376  * \param name the name of a storage
377  * \return the corresponding storage
378  */
379 msg_storage_t MSG_storage_get_by_name(const char *name)
380 {
381   return (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,name);
382 }
383
384 /** \ingroup msg_storage_management
385  * \brief Returns a dynar containing all the storage elements declared at a given point of time
386  *
387  */
388 xbt_dynar_t MSG_storages_as_dynar(void) {
389
390   xbt_lib_cursor_t cursor;
391   char *key;
392   void **data;
393   xbt_dynar_t res = xbt_dynar_new(sizeof(msg_storage_t),NULL);
394
395   xbt_lib_foreach(storage_lib, cursor, key, data) {
396           if(xbt_lib_get_level(xbt_lib_get_elm_or_null(storage_lib, key), MSG_STORAGE_LEVEL) != NULL) {
397       xbt_dictelm_t elm = xbt_dict_cursor_get_elm(cursor);
398       xbt_dynar_push(res, &elm);
399     }
400   }
401   return res;
402 }
403
404 /** \ingroup msg_storage_management
405  *
406  * \brief Set the user data of a #msg_storage_t.
407  * This functions checks whether some data has already been associated to \a storage
408    or not and attach \a data to \a storage if it is possible.
409  */
410 msg_error_t MSG_storage_set_data(msg_storage_t storage, void *data)
411 {
412   msg_storage_priv_t priv = MSG_storage_priv(storage);
413   priv->data = data;
414   return MSG_OK;
415 }
416
417 /** \ingroup msg_host_management
418  *
419  * \brief Returns the user data of a #msg_storage_t.
420  *
421  * This functions checks whether \a storage is a valid pointer or not and returns
422    the user data associated to \a storage if it is possible.
423  */
424 void *MSG_storage_get_data(msg_storage_t storage)
425 {
426   xbt_assert((storage != NULL), "Invalid parameters");
427   msg_storage_priv_t priv = MSG_storage_priv(storage);
428   return priv->data;
429 }
430
431 /** \ingroup msg_storage_management
432  *
433  * \brief Returns the content (file list) of a #msg_storage_t.
434  * \param storage a storage
435  * \return The content of this storage element as a dict (full path file => size)
436  */
437 xbt_dict_t MSG_storage_get_content(msg_storage_t storage)
438 {
439   return SIMIX_storage_get_content(storage);
440 }
441
442 sg_size_t MSG_storage_get_size(msg_storage_t storage)
443 {
444   return SIMIX_storage_get_size(storage);
445 }
446
447 /** \ingroup msg_storage_management
448  *
449  * \brief Returns the host name the storage is attached to
450  *
451  * This functions checks whether a storage is a valid pointer or not and return its name.
452  */
453 const char *MSG_storage_get_host(msg_storage_t storage) {
454   xbt_assert((storage != NULL), "Invalid parameters");
455   msg_storage_priv_t priv = MSG_storage_priv(storage);
456   return priv->host;
457 }