File: syscopy.c

package info (click to toggle)
aces3 3.0.6-7
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 82,460 kB
  • sloc: fortran: 225,647; ansic: 20,413; cpp: 4,349; makefile: 953; sh: 137
file content (187 lines) | stat: -rw-r--r-- 4,832 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
*  Copyright (c) 2003-2010 University of Florida
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  The GNU General Public License is included in this distribution
*  in the file COPYRIGHT.
*/ 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <mpi.h>

#ifdef XT3
/* The Cray XT3 has only a micro-kernel, with no ability to execute a
   "system" call from within an executable program.  However, joda uses
   the system command to execute copy commands when doing its coares backup
   and coarse restore.  Therefore, this dummy system call intercepts the
   normal one and calls routines that do a manual file copy. 
*/ 

#include <dirent.h>

#define BUFSIZE 2048

void xt3_copy_file(char *input, char *output);
int creatfile(char *file, int *fh);
int openfile(char *file, int *fh);

int system(char *cmd)
{
   char *s1, *s2, *s3, *s4;
   char *line;
   char *original_dir; 
   int i, n, ierr;
   struct dirent **namelist;

   line = (char *) malloc(strlen(cmd)+1);
   strcpy(line, cmd);

   /* Parse the command line. */

   s1 = strtok(line, " ");
   s2 = strtok(NULL, " ");
   s3 = strtok(NULL, " ");
   s4 = strtok(NULL, " ");

   /* Do we have a valid command?  The only ones we can handle are "cp" and 
      "rm". */

   if (!strcmp(s1, "/bin/cp"))
   {
      /* Copy command. */

      if (!(s1 && s2 && s3 && s4) )   /* Must have 4 fields. */
      {
         printf("Unable to parse system command line: s1 = %s, s2 = %s, s3 = %s, s4 = %s4\n",s1,s2,s3,s4);
         MPI_Abort(MPI_COMM_WORLD, 1);
      }

      xt3_copy_file(s3, s4);
   }
   else if (!strcmp(s1, "/bin/rm"))
   {
      /* Remove command. */
 
      if (!(s1 && s2 && s3) )   /* Must have 3 fields. */
      {
         printf("Unable to parse system command line: s1 = %s, s2 = %s, s3 = %s\n",s1,s2,s3);
         MPI_Abort(MPI_COMM_WORLD, 1);
      }

      /* We must first remove each file in the directory before 
         removing the directory itself.  The scandir routine gives 
         us a structure with the directory entries.
      */

      original_dir = getcwd(NULL, 0);   /* Save the original working dir */

      n = scandir(s3, &namelist, 0,alphasort);
      if (n < 0)
      {
         perror("scandir");
         MPI_Abort(MPI_COMM_WORLD, 1);
      }
      else
      {
         /* The scandir worked, now remove each file. */
 
         ierr = chdir(s3);  /* chdir to the new directory. */
         if (ierr < 0) 
         {
            printf("Error: Cannot chdir to %s\n",s3);
            perror("chdir");
            MPI_Abort(MPI_COMM_WORLD, 1);
         }

         for (i = 0; i < n; i++)
         {
            if (strcmp(namelist[i]->d_name, ".") && 
                strcmp(namelist[i]->d_name, "..")  )
            {
               ierr = remove(namelist[i]->d_name);
               if (ierr < 0)
               {
                  printf("Error: Unable to remove file %s from dir %s\n",namelist[i]->d_name, s3);
                  perror("remove");
               }
            }

            free(namelist[i]);
         }
      }
      free(namelist);   /* free up the directory structure */

      /* Now remove the directory. */

      rmdir(s3);

      ierr = chdir(original_dir);   /* Return to the original working dir */
      free(original_dir);           /* clean up the memory */ 
   }
   else
   {
      printf("XT3 SYSTEM CALL: Invalid command:\n");
      printf("command line is %s\n",cmd);
      MPI_Abort(MPI_COMM_WORLD, 1);
   } 

   free(line);
   return 0;
}

void xt3_copy_file(char *input, char *output)
{
   /* Copies file "input" to "output". */

   int ihandle, ohandle, ierr;
   char buf[BUFSIZE];
   size_t size, datasize;

   ierr = creatfile(output, &ohandle);
   if (ierr < 0) 
   {
       printf("Error creating file %s\n",output);
       MPI_Abort(MPI_COMM_WORLD, 1);
   }

   ierr = openfile(input, &ihandle);
   if (ierr < 0)
   {
       printf("Error opening file %s\n",input);
       MPI_Abort(MPI_COMM_WORLD, 1);
   }

   datasize = BUFSIZE;

   while (1)
   {
      size = read( ihandle, (char*)buf, datasize);
      if (size == 0) break;

      if (size < 0) 
      {
         printf("Error: read error while reading %s\n",input);
         MPI_Abort(MPI_COMM_WORLD, 1);
      }
      write(ohandle, buf, size); 
   }
 
   close(ihandle);
   close(ohandle);
}

#endif