STATIC_ROUTINE int CopyFile(char *src, char *dst, int dont_replace)
{
  /*
  int status=0;
  char *cmd = (char *)malloc(strlen(src)+strlen(dst) + 100);
  sprintf(cmd,"cp %s %s",src,dst);
  status = system(cmd);
  if (status != 0)
     printf("Error creating pulse with command: %s, status = %d\n",cmd,status);
  else {
    sprintf(cmd,"SetMdsplusFileProtection %s 2> /dev/null",dst);
    system(cmd);
  }
  free(cmd);
  return status == 0;
  */
  int status = TreeFAILURE;

  int src_fd = MDS_IO_OPEN(src,O_RDONLY | O_BINARY | O_RANDOM, 0);
  if (src_fd != -1)
  {
    off_t src_len = MDS_IO_LSEEK(src_fd, 0, SEEK_END);
    int dst_fd = MDS_IO_OPEN(dst,O_RDWR | O_CREAT | O_TRUNC, 0664);
    if (dst_fd != -1)
    {
      MDS_IO_LSEEK(src_fd, 0, SEEK_SET);
      MDS_IO_LOCK(src_fd,0,src_len,1);
      if (src_len > 0)
      {
        void *buff = malloc(src_len);
        size_t bytes_read = MDS_IO_READ(src_fd,buff,(size_t)src_len);
        if (bytes_read == src_len)
	{
          int bytes_written = MDS_IO_WRITE(dst_fd,buff,(size_t)src_len);
          if (bytes_written == src_len)
            status = TreeSUCCESS;
        }
        if (buff)
          free(buff);
      }
      else if (src_len == 0)
          status = TreeSUCCESS;
      MDS_IO_LOCK(src_fd,0,src_len,0);
      MDS_IO_CLOSE(dst_fd);
    }
    MDS_IO_CLOSE(src_fd);
  }
  return status;
}
STATIC_ROUTINE int _CopyFile(char *src, char *dst, int lock_it)
{
  int status = TreeFAILURE;

  int src_fd = MDS_IO_OPEN(src,O_RDONLY | O_BINARY | O_RANDOM, 0);
  if (src_fd != -1)
  {
    ssize_t src_len = MDS_IO_LSEEK(src_fd, 0, SEEK_END);
    int dst_fd = MDS_IO_OPEN(dst,O_RDWR | O_CREAT | O_TRUNC, 0664);
    if ((dst_fd != -1) && (src_len != -1))
    {
      MDS_IO_LSEEK(src_fd, 0, SEEK_SET);
      if (lock_it) MDS_IO_LOCK(src_fd,0,(int)src_len,MDS_IO_LOCK_RD,0);
      if (src_len > 0)
      {
        size_t chunk_size = (size_t)(MIN(MAX_CHUNK, src_len));
        void *buff = malloc(chunk_size);
        size_t bytes_to_go = (size_t)src_len;
        while(bytes_to_go > 0)
        {
          size_t io_size = MIN(bytes_to_go, chunk_size);
          ssize_t bytes_read = MDS_IO_READ(src_fd,buff,io_size);
          if (bytes_read == io_size)
	  {
            ssize_t bytes_written = MDS_IO_WRITE(dst_fd,buff,io_size);
            if (bytes_written != io_size)
              break;
          }
          bytes_to_go -= io_size;
        }
        if (buff)
          free(buff);
        if (bytes_to_go == 0)
          status = TreeSUCCESS;
      }
      else if (src_len == 0)
          status = TreeSUCCESS;
      if (lock_it) MDS_IO_LOCK(src_fd,0,(int)src_len,MDS_IO_LOCK_NONE,0);
      MDS_IO_CLOSE(dst_fd);
    }
    MDS_IO_CLOSE(src_fd);
  }
  return status;
}
Exemple #3
0
int TreeGetNciLw(TREE_INFO *info, int node_num, NCI *nci)
{
  int       status;

 /* status = TreeLockNci(info,0,node_num);
  if (!(status & 1)) return status;*/
  status = TreeNORMAL;

/******************************************
  If the tree is not open for edit then
  if the characteristic file is not open
  (or not open for write)
  open the characteristic file for writting
  if OK so far then
  fill in the rab and read the record
******************************************/
  if ((info->edit == 0) || (node_num < info->edit->first_in_mem))
  {
    int deleted = 1;
    while (status & 1 && deleted) {
      if ((info->nci_file == 0) || (info->nci_file->put == 0))
	status = TreeOpenNciW(info, 0);
      if (status & 1) {      
	char nci_bytes[42];
	status = TreeLockNci(info,0,node_num,&deleted);
	if (status & 1 && deleted) {
	  status = TreeReopenNci(info);
	}
	else {
	  if (!(status & 1)) return status;
	  MDS_IO_LSEEK(info->nci_file->put,node_num * sizeof(nci_bytes),SEEK_SET);
	  status = (MDS_IO_READ(info->nci_file->put,nci_bytes,sizeof(nci_bytes)) == sizeof(nci_bytes)) ? TreeNORMAL : TreeFAILURE;
	  if (status == TreeNORMAL)
	    TreeSerializeNciIn(nci_bytes,nci);
	  if (!(status & 1))
	    TreeUnLockNci(info,0,node_num);
	}
      }
    }
  }
  else
  {
  /********************************************
   Otherwise the tree is open for edit so
   the characteristics are just a memory reference
   away.
  *********************************************/
	status = TreeLockNci(info,0,node_num,0);
		if (!(status & 1)) return status;

    memcpy(nci, info->edit->nci + node_num - info->edit->first_in_mem, sizeof(struct nci));
  }

  return status;
}
Exemple #4
0
int TreePutNci(TREE_INFO *info, int node_num, NCI *nci, int flush)
{
  int       status;
  status = TreeNORMAL;
/***************************************
  If the tree is not open for edit
****************************************/

  if ((info->edit == 0) ||
      (node_num < info->edit->first_in_mem))
  {

  /**********************
   Update the NCI record
  ***********************/

    status = TreeLockNci(info, 0, node_num,0);
    if (status & 1)
    {
      char nci_bytes[42];
      memset(nci_bytes,0,sizeof(nci_bytes));
      TreeSerializeNciOut(nci,nci_bytes);
      MDS_IO_LSEEK(info->nci_file->put,sizeof(nci_bytes) * node_num, SEEK_SET);
      status = (MDS_IO_WRITE(info->nci_file->put,nci_bytes,sizeof(nci_bytes)) == sizeof(nci_bytes)) ? TreeNORMAL : TreeFAILURE;
      TreeUnLockNci(info, 0, node_num);
    }
  }

/****************************
  Else it is open for edit so
  the characteristics are just a
  memory reference away.
*****************************/

  else
    memcpy(info->edit->nci + (node_num - info->edit->first_in_mem),nci,sizeof(*nci));
  TreeUnLockNci(info,0,node_num);
  return status;
}
Exemple #5
0
/*------------------------------------------------------------------------------

		Name: TreeOpenNciW

		Type:   C function

		Author:	Josh Stillerman
			MIT Plasma Fusion Center

		Date:   22-MAR-1988

		Purpose: Open a tree characteristics file for write access.

------------------------------------------------------------------------------

	Call sequence:
		       status = TreeOpenNciW(info_ptr)

------------------------------------------------------------------------------
   Copyright (c) 1988
   Property of Massachusetts Institute of Technology, Cambridge MA 02139.
   This program cannot be copied or distributed in any form for non-MIT
   use without specific written approval of MIT Plasma Fusion Center
   Management.
---------------------------------------------------------------------------

	Description:


+-----------------------------------------------------------------------------*/
int TreeOpenNciW(TREE_INFO *info, int tmpfile)
{
  int       status;

/****************************************************
  If there is no characterisitics file block allocated then
    Allocate one
*****************************************************/

  if (info->nci_file == 0)
  {
    status = ((info->nci_file = (struct nci_file *)malloc(sizeof(NCI_FILE))) != NULL) ? TreeNORMAL : TreeFAILURE;
    if (status & 1)
    {
      size_t len = strlen(info->filespec)-4;
      char *filename = strncpy(malloc(len+20),info->filespec,len);
      filename[len]='\0';
      strcat(filename,tmpfile ? "characteristics#" : "characteristics");
      memset(info->nci_file,0, sizeof(NCI_FILE));
      info->nci_file->get = MDS_IO_OPEN(filename,tmpfile ? O_RDWR | O_CREAT | O_TRUNC  : O_RDONLY,0664);
      status = (info->nci_file->get == -1) ? TreeFAILURE : TreeNORMAL;
      if (info->nci_file->get == -1)
        info->nci_file->get = 0;
      if (status & 1)
      {
        info->nci_file->put = MDS_IO_OPEN(filename,O_RDWR, 0);
        status = (info->nci_file->put == -1) ? TreeFAILURE : TreeNORMAL;
        if (info->nci_file->put == -1)
          info->nci_file->put = 0;
      }
      free(filename);
    }
  }
  else
  {
  /*******************************************
    Else the file was open for read access so
    close it and re-open it for write access.
  *******************************************/
    size_t len = strlen(info->filespec)-4;
    char *filename = strncpy(malloc(len+20),info->filespec,len);
    filename[len]='\0';
    strcat(filename,tmpfile ? "characteristics#" : "characteristics");
    if (info->nci_file->put)
      MDS_IO_CLOSE(info->nci_file->put);
    info->nci_file->put = MDS_IO_OPEN(filename,(tmpfile ? O_RDWR | O_CREAT | O_TRUNC : O_RDWR), 0664);
    status = (info->nci_file->put == -1) ? TreeFAILURE : TreeNORMAL;
    if (info->nci_file->put == -1)
      info->nci_file->put = 0;
    free(filename);
  }
  if (status & 1)
  {
    if (info->edit)
    {
    info->edit->first_in_mem = (int)MDS_IO_LSEEK(info->nci_file->put,0,SEEK_END)/42;
    }
  /**********************************************
   Set up the RABs for buffered reads and writes
   and CONNECT it.
   If there is a problem then close it, free the
   memory and return.
  **********************************************/
  }
  else
    {
      free(info->nci_file);
      info->nci_file = NULL;
    }
  if (status & 1)
    TreeCallHook(OpenNCIFileWrite, info,0);
  return status;
}