- m_host_list[0] = MSG_host_self();
- m_host_list[1] = attached_host;
- double flops_amount[] = { 0, 0 };
- double bytes_amount[] = { 0, (double)size, 0, 0 };
-
- msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, NULL);
- msg_error_t transfer = MSG_parallel_task_execute(task);
- MSG_task_destroy(task);
- free(m_host_list);
- if(transfer != MSG_OK){
- if (transfer == MSG_HOST_FAILURE)
- XBT_WARN("Transfer error, %s remote host just turned off!", MSG_host_get_name(attached_host));
- if (transfer == MSG_TASK_CANCELED)
- XBT_WARN("Transfer error, task has been canceled!");
-
- return -1;
- }
- }
- /* Write file on local or remote host */
- offset = simcall_file_tell(file_priv->simdata->smx_file);
- write_size = simcall_file_write(file_priv->simdata->smx_file, size, attached_host);
- file_priv->size = offset+write_size;
-
- return write_size;
-}
-
-/** \ingroup msg_file_management
- * \brief Opens the file whose name is the string pointed to by path
- *
- * \param fullpath is the file location on the storage
- * \param data user data to attach to the file
- *
- * \return An #msg_file_t associated to the file
- */
-msg_file_t MSG_file_open(const char* fullpath, void* data)
-{
- char *name;
- msg_file_priv_t priv = xbt_new(s_msg_file_priv_t, 1);
- priv->data = data;
- priv->fullpath = xbt_strdup(fullpath);
- priv->simdata = xbt_new0(s_simdata_file_t,1);
- priv->simdata->smx_file = simcall_file_open(fullpath, MSG_host_self());
- priv->desc_id = __MSG_host_get_file_descriptor_id(MSG_host_self());
-
- name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()),
- priv->desc_id);
-
- xbt_lib_set(file_lib, name, MSG_FILE_LEVEL, priv);
- msg_file_t fd = (msg_file_t) xbt_lib_get_elm_or_null(file_lib, name);
- __MSG_file_get_info(fd);
- xbt_free(name);
-
- return fd;
-}
-
-/** \ingroup msg_file_management
- * \brief Close the file
- *
- * \param fd is the file to close
- * \return 0 on success or 1 on error
- */
-int MSG_file_close(msg_file_t fd)
-{
- char *name;
- msg_file_priv_t priv = MSG_file_priv(fd);
- if (priv->data)
- xbt_free(priv->data);
-
- int res = simcall_file_close(priv->simdata->smx_file, MSG_host_self());
- name = bprintf("%s:%s:%d", priv->fullpath, MSG_host_get_name(MSG_host_self()),
- priv->desc_id);
- __MSG_host_release_file_descriptor_id(MSG_host_self(), priv->desc_id);
- xbt_lib_unset(file_lib, name, MSG_FILE_LEVEL, 1);
- xbt_free(name);
- return res;
-}
-
-/** \ingroup msg_file_management
- * \brief Unlink the file pointed by fd
- *
- * \param fd is the file descriptor (#msg_file_t)
- * \return 0 on success or 1 on error
- */
-msg_error_t MSG_file_unlink(msg_file_t fd)
-{
- msg_file_priv_t file_priv = MSG_file_priv(fd);
- /* Find the host where the file is physically located (remote or local)*/
- msg_storage_t storage_src =
- (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib,
- file_priv->storageId);
- msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
- msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
- int res = simcall_file_unlink(file_priv->simdata->smx_file, attached_host);
- return (msg_error_t) res;
-}
-
-/** \ingroup msg_file_management
- * \brief Return the size of a file
- *
- * \param fd is the file descriptor (#msg_file_t)
- * \return the size of the file (as a #sg_size_t)
- */
-sg_size_t MSG_file_get_size(msg_file_t fd){
- msg_file_priv_t priv = MSG_file_priv(fd);
- return simcall_file_get_size(priv->simdata->smx_file);
-}
-
-/**
- * \ingroup msg_file_management
- * \brief Set the file position indicator in the msg_file_t by adding offset bytes
- * to the position specified by origin (either SEEK_SET, SEEK_CUR, or SEEK_END).
- *
- * \param fd : file object that identifies the stream
- * \param offset : number of bytes to offset from origin
- * \param origin : Position used as reference for the offset. It is specified by
- * one of the following constants defined in \<stdio.h\> exclusively to be used as
- * arguments for this function (SEEK_SET = beginning of file, SEEK_CUR = current
- * position of the file pointer, SEEK_END = end of file)
- *
- * \return If successful, the function returns MSG_OK (=0). Otherwise, it returns
- * MSG_TASK_CANCELED (=8).
- *
- */
-msg_error_t MSG_file_seek(msg_file_t fd, sg_offset_t offset, int origin)
-{
- msg_file_priv_t priv = MSG_file_priv(fd);
- return (msg_error_t) simcall_file_seek(priv->simdata->smx_file, offset, origin);
-}
-
-/**
- * \ingroup msg_file_management
- * \brief Returns the current value of the position indicator of the file
- *
- * \param fd : file object that identifies the stream
- * \return On success, the current value of the position indicator is returned.
- *
- */
-sg_size_t MSG_file_tell(msg_file_t fd)
-{
- msg_file_priv_t priv = MSG_file_priv(fd);
- return simcall_file_tell(priv->simdata->smx_file);
-}
-
-const char *MSG_file_get_name(msg_file_t fd) {
- xbt_assert((fd != NULL), "Invalid parameters");
- msg_file_priv_t priv = MSG_file_priv(fd);
- return priv->fullpath;
-}
-
-/**
- * \ingroup msg_file_management
- * \brief Move a file to another location on the *same mount point*.
- *
- */
-msg_error_t MSG_file_move (msg_file_t fd, const char* fullpath)
-{
- msg_file_priv_t priv = MSG_file_priv(fd);
- return (msg_error_t) simcall_file_move(priv->simdata->smx_file, fullpath);
-}
-
-/**
- * \ingroup msg_file_management
- * \brief Copy a file to another location on a remote host.
- * \param file : the file to move
- * \param host : the remote host where the file has to be copied
- * \param fullpath : the complete path destination on the remote host
- * \return If successful, the function returns MSG_OK. Otherwise, it returns
- * MSG_TASK_CANCELED.
- */
-msg_error_t MSG_file_rcopy (msg_file_t file, msg_host_t host, const char* fullpath)
-{
- msg_file_priv_t file_priv = MSG_file_priv(file);
- sg_size_t read_size;
-
- /* Find the host where the file is physically located and read it */
- msg_storage_t storage_src =(msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, file_priv->storageId);
- msg_storage_priv_t storage_priv_src = MSG_storage_priv(storage_src);
- msg_host_t attached_host = MSG_host_by_name(storage_priv_src->hostname);
- MSG_file_seek(file, 0, SEEK_SET);
- read_size = simcall_file_read(file_priv->simdata->smx_file, file_priv->size, attached_host);
-
- /* Find the real host destination where the file will be physically stored */
- xbt_dict_cursor_t cursor = NULL;
- char *mount_name, *storage_name, *file_mount_name, *host_name_dest;
- msg_storage_t storage_dest = NULL;
- msg_host_t host_dest;
- size_t longest_prefix_length = 0;
-
- xbt_dict_t storage_list = host->mounted_storages_as_dict();
- xbt_dict_foreach(storage_list,cursor,mount_name,storage_name){
- file_mount_name = (char *) xbt_malloc ((strlen(mount_name)+1));
- strncpy(file_mount_name,fullpath,strlen(mount_name)+1);
- file_mount_name[strlen(mount_name)] = '\0';
-
- if(!strcmp(file_mount_name,mount_name) && strlen(mount_name)>longest_prefix_length){
- /* The current mount name is found in the full path and is bigger than the previous*/
- longest_prefix_length = strlen(mount_name);
- storage_dest = (msg_storage_t) xbt_lib_get_elm_or_null(storage_lib, storage_name);
- }
- free(file_mount_name);
- }
- xbt_dict_free(&storage_list);
-
- if(longest_prefix_length>0){
- /* Mount point found, retrieve the host the storage is attached to */
- msg_storage_priv_t storage_dest_priv = MSG_storage_priv(storage_dest);
- host_name_dest = (char*)storage_dest_priv->hostname;
- host_dest = MSG_host_by_name(host_name_dest);
-
- }else{
- XBT_WARN("Can't find mount point for '%s' on destination host '%s'", fullpath, sg_host_get_name(host));
- return MSG_TASK_CANCELED;
- }
-
- XBT_DEBUG("Initiate data transfer of %llu bytes between %s and %s.", read_size, storage_priv_src->hostname, host_name_dest);
- msg_host_t *m_host_list = NULL;
- m_host_list = (msg_host_t*) calloc(2, sizeof(msg_host_t));
-
- m_host_list[0] = attached_host;
- m_host_list[1] = host_dest;
- double flops_amount[] = { 0, 0 };
- double bytes_amount[] = { 0, (double)read_size, 0, 0 };
-
- msg_task_t task = MSG_parallel_task_create("file transfer for write", 2, m_host_list, flops_amount, bytes_amount, NULL);
- msg_error_t transfer = MSG_parallel_task_execute(task);
- MSG_task_destroy(task);
- free(m_host_list);
- if(transfer != MSG_OK){
- if (transfer == MSG_HOST_FAILURE)
- XBT_WARN("Transfer error, %s remote host just turned off!", host_name_dest);
- if (transfer == MSG_TASK_CANCELED)
- XBT_WARN("Transfer error, task has been canceled!");
-
- return (msg_error_t) -1;
- }
-
- /* Create file on remote host, write it and close it */
- smx_file_t smx_file = simcall_file_open(fullpath, host_dest);
- simcall_file_write(smx_file, read_size, host_dest);
- simcall_file_close(smx_file, host_dest);
- return MSG_OK;
-
-}
-
-/**
- * \ingroup msg_file_management
- * \brief Move a file to another location on a remote host.
- * \param file : the file to move
- * \param host : the remote host where the file has to be moved
- * \param fullpath : the complete path destination on the remote host
- * \return If successful, the function returns MSG_OK. Otherwise, it returns
- * MSG_TASK_CANCELED.
- */
-msg_error_t MSG_file_rmove (msg_file_t file, msg_host_t host, const char* fullpath)
-{
- msg_error_t res = MSG_file_rcopy(file, host, fullpath);
- MSG_file_unlink(file);
- return res;
-}
-
-/**
- * \brief Destroys a file (internal call only)
- */
-void __MSG_file_destroy(msg_file_priv_t file) {
- xbt_free(file->fullpath);
- xbt_free(file->simdata);
- xbt_free(file);
-}