File: migrate_path.c

package info (click to toggle)
dpm-postgres 1.7.4.7-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 13,788 kB
  • ctags: 10,782
  • sloc: ansic: 146,136; sh: 13,362; perl: 11,142; python: 5,529; cpp: 5,113; sql: 1,790; makefile: 955; fortran: 113
file content (151 lines) | stat: -rwxr-xr-x 3,471 bytes parent folder | download | duplicates (8)
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
/*
 * Copyright (C) 1999-2004 by CERN/IT/PDP/DM
 * All rights reserved
 */

/** Takes the path to a filename which has been extracted from the EDG
 ** RLS and constructs the appropriate directory structure in the LFC
 ** namespace, giving each directory a GUID.
 ** If the given pathname starts with /grid, it is assumed to be the
 ** full pathname and any argument given by the -vo option is ignored.
 ** UID and GID are set depending on the experiment specified in
 ** -vo. If none is chosen, ATLAS is the default.
 **/

/** Usage: 
 ** migrate_path path [-vo VO_NAME]
 **/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
#include <uuid/uuid.h>
#include <sys/types.h>
#include <errno.h>
#if defined(_WIN32)
#include <winsock2.h>
#define F_OK 0
#else
#include <unistd.h>
#endif
#include "lfc_api.h"
#include "serrno.h"

main(argc, argv)
int argc;
char **argv;
{
  char path[CA_MAXPATHLEN+1];
  char base[5];
  char guid[CA_MAXGUIDLEN+1];
  char vo[6];
  struct Cns_filestat statbuf;
  char *p;
  char *endp;
  int c;
  int errflg = 0;
  uuid_t uuid;
  char uidbuf[5];
  char gidbuf[6];
  uid_t uid;
  gid_t gid;

  if (argc < 2 || (argc > 2 && strcmp(argv[2], "-vo") != 0)) {
    printf("Usage: migrate_path PATHNAME [-vo VO_NAME] where VO_NAME is 'atlas', 'cms', 'alice, 'lhcb'\n");
    exit(1);
  }

#if defined(_WIN32)
  if (WSAStartup (MAKEWORD (2, 0), &wsadata)) {
    fprintf (stderr, "WSAStartup unsuccessful\n");
    exit (SYERR);
  }
#endif

  /* set up directory name according to given arguments */
  sprintf(vo, "cms");
  strncpy(base, argv[1], 5);
  printf("Base is %s\n", base);
  if (strcmp(base, "/grid") != 0) {
    if (argv[2]) {
      sprintf(vo, "%s", argv[3]);
      sprintf(path, "/grid/%s/%s", vo, argv[1]);
    }
    else {
      sprintf(path, "/grid/%s", argv[1]);
    }
  }
  else {
    printf("Full path has been given; ignoring -vo option\n");
    strcpy(path, argv[1]);
  }

  /* set gid and uid to correct values for this VO */
  if (!strcmp(vo, "alice")) {
    sprintf(gidbuf, "1395"); 
    sprintf(uidbuf, "10417");
  }
  else if (!strcmp(vo, "lhcb")) {
    sprintf(gidbuf, "1470");
    sprintf(uidbuf, "12238");
  }
  else if (!strcmp(vo, "cms")) {
    sprintf(gidbuf, "1399");
    sprintf(uidbuf, "11410");
  }
  else if (!strcmp(vo, "atlasbis")) {
    sprintf(gidbuf,"1307");
    sprintf(uidbuf,"10761");
  }
  else {
    //default is cms 
    sprintf(gidbuf, "1399");
    sprintf(uidbuf, "11410");
  }
  uid = strtol(uidbuf, NULL, 10);
  gid = strtol(gidbuf, NULL, 10);

  /* check if exists and if not, create with guid */
  if (lfc_stat (path, &statbuf) < 0) {
    if (serrno == ENOENT) {
      endp = strrchr (path, '/');
      p = endp;
      while (p > path) {
	*p = '\0';
	c = lfc_access (path, F_OK);
	if (c == 0) break;
	p = strrchr (path, '/');
      }
      while (p <= endp) {
	*p = '/';
	uuid_generate(uuid);
	uuid_unparse(uuid, guid);
	c = lfc_mkdirg (path, guid, 0777);
	if (c < 0 && serrno != EEXIST) {
	  fprintf (stderr, "cannot create %s: %s\n",
		   path, sstrerror(serrno));
	  errflg++;
	  break;
	}
	if (lfc_chown(path, uid, gid) < 0) {
	  fprintf(stderr, "Cannot chown %s: %s\n", path, sstrerror(serrno));
	}
	p += strlen (p);
      }
    } else {
      fprintf (stderr, "%s: %s\n", path, sstrerror(serrno));
      errflg++;
    }    
  }

#if defined(_WIN32)
  WSACleanup();
#endif

  if (errflg)
    exit (USERR);

  printf("Path %s successfully created\n", path); 
  exit (0);
}