Logo AND Algorithmique Numérique Distribuée

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