Logo AND Algorithmique Numérique Distribuée

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