1 /* Copyright (c) 2004 - 2013. The SimGrid Team.
2 * All rights reserved. */
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. */
7 #include "msg_private.h"
10 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(msg_io, msg,
11 "Logging specific to MSG (io)");
13 /** @addtogroup msg_file_management
14 * \htmlonly <!-- DOXYGEN_NAVBAR_LABEL="Files" --> \endhtmlonly
15 * (#msg_file_t) and the functions for managing it.
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 fd->info->content_type = xbt_dynar_pop_as(info, char *);
24 fd->info->storage_type = xbt_dynar_pop_as(info, char *);
25 fd->info->storageId = xbt_dynar_pop_as(info, char *);
26 fd->info->mount_point = xbt_dynar_pop_as(info, char *);
27 fd->info->size = xbt_dynar_pop_as(info, size_t);
29 xbt_dynar_free_container(&info);
32 /** \ingroup msg_file_management
34 * \brief Set the user data of a #msg_file_t.
36 * This functions checks whether some data has already been associated to \a file
37 or not and attach \a data to \a file if it is possible.
39 msg_error_t MSG_file_set_data(msg_file_t fd, void *data)
41 SIMIX_file_set_data(fd->simdata->smx_file,data);
46 /** \ingroup msg_file_management
48 * \brief Return the user data of a #msg_file_t.
50 * This functions checks whether \a file is a valid pointer or not and return
51 the user data associated to \a file if it is possible.
53 void *MSG_file_get_data(msg_file_t fd)
55 return SIMIX_file_get_data(fd->simdata->smx_file);
58 /** \ingroup msg_file_management
59 * \brief Display information related to a file descriptor
61 * \param fd is a the file descriptor
64 void MSG_file_dump (msg_file_t fd){
65 // THROW_UNIMPLEMENTED;
66 /* Update the cached information first */
67 __MSG_file_get_info(fd);
68 XBT_INFO("File Descriptor information:\n\t\tFull name: '%s'"
69 "\n\t\tSize: %zu\n\t\tMount point: '%s'\n\t\tStorage Id: '%s'"
70 "\n\t\tStorage Type: '%s'\n\t\tContent Type: '%s'",
71 fd->fullname, fd->info->size, fd->info->mount_point, fd->info->storageId,
72 fd->info->storage_type, fd->info->content_type);
75 /** \ingroup msg_file_management
78 * \param size of the file to read
79 * \param fd is a the file descriptor
80 * \return the number of bytes successfully read
82 size_t MSG_file_read(size_t size, msg_file_t fd)
84 return simcall_file_read(size, fd->simdata->smx_file);
87 /** \ingroup msg_file_management
88 * \brief Write into a file
90 * \param size of the file to write
91 * \param fd is a the file descriptor
92 * \return the number of bytes successfully write
94 size_t MSG_file_write(size_t size, msg_file_t fd)
96 return simcall_file_write(size, fd->simdata->smx_file);
99 /** \ingroup msg_file_management
100 * \brief Opens the file whose name is the string pointed to by path
102 * \param mount is the mount point where find the file is located
103 * \param fullname is the file location on the storage
104 * \param data user data to attach to the file
106 * \return An #msg_file_t associated to the file
108 msg_file_t MSG_file_open(const char* mount, const char* fullname, void* data)
110 msg_file_t file = xbt_new(s_msg_file_t,1);
111 file->fullname = xbt_strdup(fullname);
112 file->simdata = xbt_new0(s_simdata_file_t,1);
113 file->info = xbt_new0(s_file_info_t,1);
114 file->simdata->smx_file = simcall_file_open(mount, fullname);
115 SIMIX_file_set_data(file->simdata->smx_file, data);
120 /** \ingroup msg_file_management
121 * \brief Close the file
123 * \param fd is the file to close
124 * \return 0 on success or 1 on error
126 int MSG_file_close(msg_file_t fd)
128 int res = simcall_file_close(fd->simdata->smx_file);
130 xbt_free(fd->simdata);
136 /** \ingroup msg_file_management
137 * \brief Unlink the file pointed by fd
139 * \param fd is the file descriptor (#msg_file_t)
140 * \return 0 on success or 1 on error
142 int MSG_file_unlink(msg_file_t fd)
144 return simcall_file_unlink(fd->simdata->smx_file);
147 /** \ingroup msg_file_management
148 * \brief Return the size of a file
150 * \param fd is the file descriptor (#msg_file_t)
151 * \return the size of the file (as a size_t)
154 size_t MSG_file_get_size(msg_file_t fd){
155 return simcall_file_get_size(fd->simdata->smx_file);
158 /** \ingroup msg_file_management
159 * \brief Search for file
161 * \param mount is the mount point where find the file is located
162 * \param path the file regex to find
163 * \return a xbt_dict_t of file where key is the name of file and the
164 * value the msg_stat_t corresponding to the key
166 xbt_dict_t MSG_file_ls(const char *mount, const char *path)
168 xbt_assert(path,"You must set path");
169 int size = strlen(path);
170 if(size && path[size-1] != '/')
172 char *new_path = bprintf("%s/",path);
173 XBT_DEBUG("Change '%s' for '%s'",path,new_path);
174 xbt_dict_t dict = simcall_file_ls(mount, new_path);
179 return simcall_file_ls(mount, path);
182 /********************************* Storage **************************************/
184 /** \ingroup msg_storage_management
185 * \brief Return the free space size of a storage element
186 * \param the storage name (#char*)
187 * \return the free space size of the storage element (as a size_t)
189 size_t MSG_storage_get_free_size(const char* name){
190 return simcall_storage_get_free_size(name);
193 /** \ingroup msg_storage_management
194 * \brief Return the used space size of a storage element
195 * \param the storage name (#char*)
196 * \return the used space size of the storage element (as a size_t)
198 size_t MSG_storage_get_used_size(const char* name){
199 return simcall_storage_get_used_size(name);
202 /** \ingroup msg_storage_management
203 * \brief Returns a xbt_dict_t consisting of the list of properties assigned to this storage
204 * \param storage a storage
205 * \return a dict containing the properties
207 xbt_dict_t MSG_storage_get_properties(msg_storage_t storage)
209 xbt_assert((storage != NULL), "Invalid parameters (storage is NULL)");
211 xbt_die( "Not implemented yet");
212 return xbt_dict_new();
213 //return (simcall_host_get_properties(storage));
216 /** \ingroup msg_storage_management
217 * \brief Finds a msg_storage_t using its name.
218 * \param name the name of a storage.
219 * \return the corresponding storage
221 msg_storage_t MSG_storage_get_by_name(const char *name)
223 return (msg_storage_t) xbt_lib_get_elm_or_null(host_lib,name);
226 /** \ingroup msg_storage_management
227 * \brief Return a dynar containing all the storages declared at a given point of time
229 xbt_dynar_t MSG_storages_as_dynar(void) {
231 xbt_dynar_t storages = xbt_dynar_new(sizeof(msg_host_t), NULL);
236 hosts = MSG_hosts_as_dynar();
237 xbt_dynar_foreach(hosts, i, host){
238 xbt_dynar_push(storages,xbt_lib_get_level((void *)host, SURF_STORAGE_LEVEL));