1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012. 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 **************************************/
22 /** \ingroup msg_file_management
23 * \brief Read elements of a file
25 * \param ptr buffer to where the data is copied
26 * \param size of each element
27 * \param nmemb is the number of elements of data to read
28 * \param stream to read
29 * \return the number of items successfully read
31 double MSG_file_read(void* ptr, size_t size, size_t nmemb, msg_file_t stream)
33 return simcall_file_read(ptr, size, nmemb, stream->simdata->smx_file);
36 /** \ingroup msg_file_management
37 * \brief Write elements into a file
39 * \param ptr buffer from where the data is copied
40 * \param size of each element
41 * \param nmemb is the number of elements of data to write
42 * \param stream to write
43 * \return the number of items successfully write
45 size_t MSG_file_write(const void* ptr, size_t size, size_t nmemb, msg_file_t stream)
47 return simcall_file_write(ptr, size, nmemb, stream->simdata->smx_file);
50 /** \ingroup msg_file_management
51 * \brief Opens the file whose name is the string pointed to by path
53 * \param mount is the mount point where find the file is located
54 * \param path is the file location on the storage
55 * \param mode points to a string beginning with one of the following sequences (Additional characters may follow these sequences.):
56 * r Open text file for reading. The stream is positioned at the beginning of the file.
57 * r+ Open for reading and writing. The stream is positioned at the beginning of the file.
58 * w Truncate file to zero length or create text file for writing. The stream is positioned at the beginning of the file.
59 * w+ Open for reading and writing. The file is created if it does not exist, otherwise it is truncated. The stream is positioned at the
60 * beginning of the file.
61 * a Open for appending (writing at end of file). The file is created if it does not exist. The stream is positioned at the end of the file.
62 * a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading
63 * is at the beginning of the file, but output is always appended to the end of the file.
65 * \return An #msg_file_t associated to the file
67 msg_file_t MSG_file_open(const char* mount, const char* path, const char* mode)
69 msg_file_t file = xbt_new(s_msg_file_t,1);
70 file->name = strdup(path);
71 file->simdata = xbt_new0(s_simdata_file_t,1);
72 file->simdata->smx_file = simcall_file_open(mount, path, mode);
76 /** \ingroup msg_file_management
77 * \brief Close the file
79 * \param fp is the file to close
80 * \return 0 on success or 1 on error
82 int MSG_file_close(msg_file_t fp)
84 int res = simcall_file_close(fp->simdata->smx_file);
86 xbt_free(fp->simdata);
91 /** \ingroup msg_file_management
92 * \brief Stats the file pointed by fd
94 * \param fd is the file descriptor (#msg_file_t)
95 * \param buf is the return structure with informations
96 * \return 0 on success or 1 on error
98 int MSG_file_stat(msg_file_t fd, s_msg_stat_t *buf)
101 res = simcall_file_stat(fd->simdata->smx_file, buf);
105 /** \ingroup msg_file_management
106 * \brief Free the stat structure
108 * \param stat the #s_msg_stat_t to free
110 void MSG_file_free_stat(s_msg_stat_t *stat)
116 free(stat->user_rights);
119 /** \ingroup msg_file_management
120 * \brief Unlink the file pointed by fd
122 * \param fd is the file descriptor (#msg_file_t)
123 * \return 0 on success or 1 on error
125 int MSG_file_unlink(msg_file_t fd)
127 int res = simcall_file_unlink(fd->simdata->smx_file);
129 xbt_free(fd->simdata);
134 /** \ingroup msg_file_management
135 * \brief Search for file
137 * \param mount is the mount point where find the file is located
138 * \param path the file regex to find
139 * \return a xbt_dict_t of file where key is the name of file and the
140 * value the msg_stat_t corresponding to the key
142 xbt_dict_t MSG_file_ls(const char *mount, const char *path)
144 xbt_assert(path,"You must set path");
145 int size = strlen(path);
146 if(size && path[size-1] != '/')
148 char *new_path = bprintf("%s/",path);
149 XBT_DEBUG("Change '%s' for '%s'",path,new_path);
150 xbt_dict_t dict = simcall_file_ls(mount, new_path);
155 return simcall_file_ls(mount, path);