+
+int do_user_reservation(struct query *q, char *argv[], client *cl)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ char resv[USERS_RESERVATIONS_SIZE];
+ char *trans, name[ALIAS_NAME_SIZE];
+ int uid;
+ EXEC SQL END DECLARE SECTION;
+
+ uid = *(int *)argv[0];
+ trans = argv[1];
+
+ EXEC SQL SELECT UPPER(name) INTO :name FROM alias
+ WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
+ if (dbms_errno)
+ return mr_errcode;
+ if (sqlca.sqlerrd[2] != 1)
+ return MR_STRING;
+ name[1] = '\0';
+
+ EXEC SQL SELECT reservations INTO :resv FROM users
+ WHERE users_id = :uid;
+ if (dbms_errno)
+ return mr_errcode;
+ strtrim(resv);
+
+ if (!strcmp(q->shortname, "aurv"))
+ {
+ if (strchr(resv, *name))
+ return MR_EXISTS;
+ if (strlen(resv) == USERS_RESERVATIONS_SIZE - 1)
+ return MR_ARG_TOO_LONG;
+
+ strcat(resv, name);
+ }
+ else
+ {
+ char *p = strchr(resv, *name);
+ if (!p)
+ return MR_NO_MATCH;
+ memmove(p, p + 1, strlen(p));
+ }
+
+ EXEC SQL UPDATE users SET reservations = NVL(:resv, CHR(0))
+ WHERE users_id = :uid;
+ if (dbms_errno)
+ return mr_errcode;
+
+ EXEC SQL UPDATE tblstats SET updates = updates + 1, modtime = SYSDATE
+ WHERE table_name = 'users';
+ return set_modtime_by_id(q, argv, cl);
+}
+
+int get_user_reservations(struct query *q, char **argv, client *cl,
+ int (*action)(int, char *[], void *), void *actarg)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ char resv[USERS_RESERVATIONS_SIZE], *p;
+ char trans[ALIAS_TRANS_SIZE], name[2] = { 0, 0 };
+ int uid;
+ EXEC SQL END DECLARE SECTION;
+ char *targv[1];
+
+ uid = *(int *)argv[0];
+
+ EXEC SQL SELECT reservations INTO :resv FROM users
+ WHERE users_id = :uid;
+ if (dbms_errno)
+ return mr_errcode;
+
+ targv[0] = trans;
+ p = resv;
+ while (*p && !isspace(*p))
+ {
+ name[0] = toupper(*p);
+ EXEC SQL SELECT trans INTO :trans FROM alias
+ WHERE type = 'RESERVE' AND UPPER(name) = :name;
+ if (dbms_errno)
+ return mr_errcode;
+ if (sqlca.sqlerrd[2] != 1)
+ sprintf(trans, "Unknown (%s)", name);
+ (*action)(1, targv, actarg);
+ p++;
+ }
+ return MR_SUCCESS;
+}
+
+int get_user_by_reservation(struct query *q, char **argv, client *cl,
+ int (*action)(int, char *[], void *), void *actarg)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ char resv[USERS_RESERVATIONS_SIZE], login[USERS_LOGIN_SIZE];
+ char *trans, name[ALIAS_NAME_SIZE];
+ int uid;
+ EXEC SQL END DECLARE SECTION;
+ char *targv[1];
+
+ trans = argv[0];
+
+ EXEC SQL SELECT UPPER(name) INTO :name FROM alias
+ WHERE type = 'RESERVE' AND LOWER(trans) = LOWER(:trans);
+ if (dbms_errno)
+ return mr_errcode;
+ if (sqlca.sqlerrd[2] != 1)
+ return MR_STRING;
+ name[1] = '\0';
+
+ EXEC SQL DECLARE csr_gubr CURSOR FOR
+ SELECT login FROM users WHERE reservations LIKE '%' || :name || '%';
+ EXEC SQL OPEN csr_gubr;
+ if (dbms_errno)
+ return mr_errcode;
+
+ targv[0] = login;
+ while (1)
+ {
+ EXEC SQL FETCH csr_gubr INTO :login;
+ if (sqlca.sqlcode)
+ break;
+ (*action)(1, targv, actarg);
+ }
+ EXEC SQL CLOSE csr_gubr;
+
+ return MR_SUCCESS;
+}
+
+int update_container(struct query *q, char *argv[], client *cl)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ int cnt_id, acl_id, memacl_id, who, flag;
+ char name[CONTAINERS_NAME_SIZE], newchildname[CONTAINERS_NAME_SIZE];
+ char* newname, *entity, *description, *location, *contact, *acl_type, *memacl_type;
+ EXEC SQL END DECLARE SECTION;
+ char* tmpchar;
+ int cnt, childid;
+ char childname[CONTAINERS_NAME_SIZE];
+ char *qual;
+ int index = 0;
+
+ cnt_id = *(int *)argv[index++];
+ newname = argv[index++];
+
+ if (q->version >= 9)
+ flag = atoi(argv[index++]);
+
+ description = argv[index++];
+ location = argv[index++];
+ contact = argv[index++];
+ acl_type = argv[index++];
+ acl_id = *(int *)argv[index++];
+ memacl_type = argv[index++];
+ memacl_id = *(int *)argv[index++];
+ entity = cl->entity;
+ who = cl->client_id;
+
+ EXEC SQL SELECT name INTO :name
+ FROM containers
+ WHERE cnt_id = :cnt_id;
+
+ /* trim off the trailing spaces */
+ strcpy(name, strtrim(name));
+
+ qual = xmalloc(MAX_FIELD_WIDTH);
+ sprintf(qual, "name = '%s'", name);
+ incremental_before(CONTAINERS_TABLE, qual, argv);
+
+ /* if we are renaming the container */
+ if (strcmp(name, newname))
+ {
+ /* make sure that only the name part of the name has been changed */
+ tmpchar = strrchr(name, '/');
+ /* not a top-level name */
+ if (tmpchar)
+ {
+ cnt = tmpchar - name + 1;
+ /* the parent part of the old and new name should be identical */
+ if (strncmp(name, newname, cnt))
+ return MR_NEW_CONTAINER_NAME;
+ }
+ /* top level name, new name should be a top level name too */
+ else
+ {
+ if (strrchr(newname, '/'))
+ return MR_NEW_CONTAINER_NAME;
+ }
+
+ /* update the name for this container */
+ EXEC SQL UPDATE containers
+ SET name = :newname
+ WHERE cnt_id = :cnt_id;
+
+ if (dbms_errno)
+ return mr_errcode;
+
+ /* get names for its child containers */
+ EXEC SQL DECLARE csr_ucon CURSOR FOR
+ SELECT name, cnt_id FROM containers WHERE name LIKE :name || '/' || '%';
+
+ EXEC SQL OPEN csr_ucon;
+ if (dbms_errno)
+ return mr_errcode;
+
+ while (1)
+ {
+ EXEC SQL FETCH csr_ucon INTO :childname, :childid;
+ if (sqlca.sqlcode)
+ break;
+
+ strcpy(childname, strtrim(childname));
+ /* concatenate the new parent name with the existing sub-container name
+ * we get the sub-containers new name */
+ tmpchar = childname + strlen(name);
+ strcpy(newchildname, newname);
+ strcat(newchildname, tmpchar);
+
+ /* update the name */
+ EXEC SQL UPDATE containers
+ SET name = :newchildname, modtime = SYSDATE, modby = :who, modwith = :entity
+ WHERE cnt_id = :childid;
+
+ if (sqlca.sqlcode)
+ break;
+ }
+
+ EXEC SQL CLOSE csr_ucon;
+ if (dbms_errno)
+ return mr_errcode;
+ }
+
+ /* update the remaining fields */
+ if (q->version >= 9)
+ {
+ EXEC SQL UPDATE containers
+ SET publicflg= :flag, description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
+ contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
+ memacl_type = :memacl_type, memacl_id = :memacl_id,
+ modtime = SYSDATE, modby = :who, modwith = :entity
+ WHERE cnt_id = :cnt_id;
+ }
+ else
+ {
+ EXEC SQL UPDATE containers
+ SET publicflg= :flag, description = NVL(:description, CHR(0)), location = NVL(:location, CHR(0)),
+ contact = NVL(:contact, CHR(0)), acl_type = :acl_type, acl_id = :acl_id,
+ memacl_type = :memacl_type, memacl_id = :memacl_id,
+ modtime = SYSDATE, modby = :who, modwith = :entity
+ WHERE cnt_id = :cnt_id;
+ }
+
+ if (dbms_errno)
+ return mr_errcode;
+
+ sprintf(qual, "name = '%s'", newname);
+ incremental_after(CONTAINERS_TABLE, qual, argv);
+
+ return MR_SUCCESS;
+}
+
+int get_machines_of_container(struct query *q, char *argv[], client *cl,
+ int (*action)(int, char *[], void *), void *actarg)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ int cnt_id, isrecursive;
+ char machinename[MACHINE_NAME_SIZE], containername[CONTAINERS_NAME_SIZE];
+ char *qs;
+ EXEC SQL END DECLARE SECTION;
+
+ char querystring[512], tmp [256];
+ char *rargv[2];
+ int found = 0;
+
+ rargv[0] = machinename;
+ rargv[1] = containername;
+
+ cnt_id = *(int *)argv[0];
+ isrecursive = atoi(argv[1]);
+
+ /* get the container name */
+
+ EXEC SQL SELECT name INTO :containername
+ FROM containers
+ WHERE cnt_id = :cnt_id;
+
+ /* trim off the trailing spaces */
+ strcpy(containername, strtrim(containername));
+
+ strcpy(querystring, "SELECT a.name, b.name FROM machine a, containers b, mcntmap c ");
+ strcat(querystring, "WHERE a.mach_id = c.mach_id AND b.cnt_id = c.cnt_id ");
+
+ if (!isrecursive)
+ sprintf(tmp, "AND b.cnt_id = %d ", cnt_id);
+ else
+ sprintf(tmp, "AND (b.cnt_id = %d OR LOWER(b.name) LIKE LOWER('%s/%%')) ",
+ cnt_id, containername);
+
+ strcat(querystring, tmp);
+ strcat(querystring, "ORDER BY b.name, a.name");
+
+ qs = querystring;
+
+ EXEC SQL PREPARE stmt FROM :qs;
+ if (sqlca.sqlcode)
+ return MR_INTERNAL;
+ EXEC SQL DECLARE curs_gmnm CURSOR FOR stmt;
+ EXEC SQL OPEN curs_gmnm;
+
+ while (1)
+ {
+ EXEC SQL FETCH curs_gmnm INTO :machinename, :containername;
+ if (sqlca.sqlcode)
+ break;
+ (*action)(2, rargv, actarg);
+ found++;
+ }
+
+ EXEC SQL CLOSE curs_gmnm;
+ if (!found)
+ return MR_NO_MATCH;
+ return MR_SUCCESS;
+}
+
+int get_subcontainers_of_container(struct query *q, char *argv[], client *cl,
+ int (*action)(int, char *[], void *), void *actarg)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ int cnt_id, isrecursive;
+ char containername[CONTAINERS_NAME_SIZE], subcontainername[CONTAINERS_NAME_SIZE];
+ char *qs;
+ EXEC SQL END DECLARE SECTION;
+
+ char querystring[2048], tmp [1024];
+ char *rargv[1];
+ int found = 0;
+
+ rargv[0] = subcontainername;
+
+ cnt_id = *(int *)argv[0];
+ isrecursive = atoi(argv[1]);
+
+ /* get the container name */
+
+ EXEC SQL SELECT name INTO :containername
+ FROM containers
+ WHERE cnt_id = :cnt_id;
+
+ /* trim off the trailing spaces */
+ strcpy(containername, strtrim(containername));
+
+ strcpy(querystring, "SELECT name FROM containers ");
+
+ if (!isrecursive)
+ sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') and LOWER(name) NOT LIKE LOWER('%s/%%/%%') ",
+ containername, containername);
+ else
+ sprintf(tmp, "WHERE LOWER(name) LIKE LOWER('%s/%%') ", containername);
+
+ strcat(querystring, tmp);
+ strcat(querystring, "ORDER BY name");
+
+ qs = querystring;
+
+ EXEC SQL PREPARE stmt FROM :qs;
+ if (sqlca.sqlcode)
+ return MR_INTERNAL;
+ EXEC SQL DECLARE curs_gsoc CURSOR FOR stmt;
+ EXEC SQL OPEN curs_gsoc;
+
+ while (1)
+ {
+ EXEC SQL FETCH curs_gsoc INTO :subcontainername;
+ if (sqlca.sqlcode)
+ break;
+ (*action)(1, rargv, actarg);
+ found++;
+ }
+
+ EXEC SQL CLOSE curs_gsoc;
+ if (!found)
+ return MR_NO_MATCH;
+ return MR_SUCCESS;
+}
+
+int set_container_list(struct query *q, char *argv[], client *cl)
+{
+ EXEC SQL BEGIN DECLARE SECTION;
+ int cnt_id, list_id;
+ EXEC SQL END DECLARE SECTION;
+
+ cnt_id = *(int *)argv[0];
+ list_id = *(int *)argv[1];
+
+ EXEC SQL UPDATE containers SET list_id = :list_id WHERE cnt_id = :cnt_id;
+ if (dbms_errno)
+ return mr_errcode;
+
+ return MR_SUCCESS;
+}