Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
cosmetic changes - Adrien
[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
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
22 /** \ingroup msg_file_management
23  * \brief Read elements of a file
24  *
25  * \param ptr buffer to where the data is copied
26  * \param size of the file to read
27  * \param fd is a the file descriptor
28  * \return the number of items successfully read
29  */
30 size_t MSG_file_read(void* ptr, size_t size, msg_file_t fd)
31 {
32   return simcall_file_read(ptr, size, fd->simdata->smx_file);
33 }
34
35 /** \ingroup msg_file_management
36  * \brief Write elements into a file
37  *
38  * \param ptr buffer from where the data is copied
39  * \param size of the file to write
40  * \param fd is a the file descriptor
41  * \return the number of items successfully write
42  */
43 size_t MSG_file_write(const void* ptr, size_t size, msg_file_t fd)
44 {
45   return simcall_file_write(ptr, size, fd->simdata->smx_file);
46 }
47
48 /** \ingroup msg_file_management
49  * \brief Opens the file whose name is the string pointed to by path
50  *
51  * \param mount is the mount point where find the file is located
52  * \param path is the file location on the storage
53  *
54  * \return An #msg_file_t associated to the file
55  */
56 msg_file_t MSG_file_open(const char* mount, const char* path)
57 {
58   msg_file_t file = xbt_new(s_msg_file_t,1);
59   file->name = xbt_strdup(path);
60   file->simdata = xbt_new0(s_simdata_file_t,1);
61   file->simdata->smx_file = simcall_file_open(mount, path);
62   return file;
63 }
64
65 /** \ingroup msg_file_management
66  * \brief Close the file
67  *
68  * \param fd is the file to close
69  * \return 0 on success or 1 on error
70  */
71 int MSG_file_close(msg_file_t fd)
72 {
73   int res = simcall_file_close(fd->simdata->smx_file);
74   free(fd->name);
75   xbt_free(fd->simdata);
76   xbt_free(fd);
77   return res;
78 }
79
80 /** \ingroup msg_file_management
81  * \brief Unlink the file pointed by fd
82  *
83  * \param fd is the file descriptor (#msg_file_t)
84  * \return 0 on success or 1 on error
85  */
86 int MSG_file_unlink(msg_file_t fd)
87 {
88   return simcall_file_unlink(fd->simdata->smx_file);
89 }
90
91 /** \ingroup msg_file_management
92  * \brief Return the size of a file
93  *
94  * \param fd is the file descriptor (#msg_file_t)
95  * \return the size of the file (as a size_t)
96  */
97
98 size_t MSG_file_get_size(msg_file_t fd){
99   return simcall_file_get_size(fd->simdata->smx_file);
100 }
101
102 /** \ingroup msg_file_management
103  * \brief Search for file
104  *
105  * \param mount is the mount point where find the file is located
106  * \param path the file regex to find
107  * \return a xbt_dict_t of file where key is the name of file and the
108  * value the msg_stat_t corresponding to the key
109  */
110 xbt_dict_t MSG_file_ls(const char *mount, const char *path)
111 {
112   xbt_assert(path,"You must set path");
113   int size = strlen(path);
114   if(size && path[size-1] != '/')
115   {
116     char *new_path = bprintf("%s/",path);
117     XBT_DEBUG("Change '%s' for '%s'",path,new_path);
118     xbt_dict_t dict = simcall_file_ls(mount, new_path);
119     xbt_free(new_path);
120     return dict;
121   }
122
123   return simcall_file_ls(mount, path);
124 }