/* ajout, recherche, soustraction dans la structure lck */ #include #include #include #include #include #include "projet.h" /* D‚termine si un objet est d‚j… accapar‚ par un autre programmeur V‚rifie si le r‚pertoire contenant cet objet n'est pas accapar‚ ou le r‚pertoire contenant ce r‚pertoire,... Retourne le nombre de lock, 0 si aucun */ PUBLIC int PRJLOCK::islock ( const char *objet, ONE_LOCK *tblock[]) /* Liste des locks qui match */ /* peut etre NULL */ { int find = 0; char *tbobj[10]; int nbobj; { /* G‚nŠre dans tbobj toutes les variations de l'objet tbobj[0] est l'objet lui-mˆme tbobj[1] est le r‚pertoire qui contient l'objet tbobj[2] est le r‚pertoire qui contient le r‚pertoire qui contient l'objet. ... */ char buf[100]; strcpy (buf,objet); strlwr (buf); tbobj[0] = strdup(objet); nbobj = 1; while (1){ char tmp[100]; path_stripsep (buf,buf); path_splitlex(buf,tmp,NULL); if (tmp[0] == '\0') break; path_addback(tmp,buf); tbobj[nbobj++] = strdup(buf); } } { /* test si au moins un des objets de tbobj est pr‚sent dans lck */ ONE_LOCK *pt = tb; int nbloc = nblock; for (int i=0; iobjet)==0){ if (tblock != NULL) tblock[find] = pt; find++; break; } } } } { /* libere table d'objet */ for (int i=1; iprog,prog)==0){ ret = tblock[i]->locktype == 'X' ? 2 : 1; } } return ret; } /* Determine s'il y a un lock exclusif sur un projet Retourne != 0 si oui, 0 sinon */ PUBLIC int PRJLOCK::islockx (const char *projet) { int ret = 0; ONE_LOCK *tblock[50]; int nbl = islock (projet,tblock); if (nbl > 0 && tblock[0]->locktype == 'X') ret = 1; assert (!ret||nbl==1); /* si exclusif, un seul lock */ return ret; } /* D‚termine s'il y a un lock sur un objet ou un sous-objet Retourne != 0 si oui, == 0 sinon */ PUBLIC int PRJLOCK::issublock ( int exclusif, /* 0 : trouve n'importe quel lock */ /* 1 : trouve seulement lock exclusif */ const char *objet, char *prog, /* contiendra le nom du programmeur qui a le lock */ /* peut etre NULL */ char *lock) /* contiendra l'objet qui fait le lock effectif */ /* peut etre NULL */ { int find = 0; ONE_LOCK *pt = tb; int nbloc = nblock; int lenobj = strlen(objet); for (int i=0; iobjet,lenobj)==0 && (exclusif == 0 || pt->locktype == 'X')){ find = 1; if (lock != NULL) strcpy (lock,pt->objet); if (prog != NULL) strcpy (prog,pt->prog); break; } } return find; } /* Enregistre un lock sur un objet. */ PUBLIC void PRJLOCK::add ( int exclusif, /* 0 : lock non exclusif, 1 : lock exclusif */ const char *objet, const char *path) { if (nblock == maxlock){ tb = (ONE_LOCK *) realloc (tb,(maxlock+1000)*sizeof(ONE_LOCK)); assert (tb != NULL); maxlock += 1000; } { ONE_LOCK *pt = tb+nblock; pt->locktype = exclusif == 1 ? 'X' : 'G'; pt->prog = strdup (user->getnom()); pt->path = strdup (path); pt->objet = strdup (objet); nblock++; } } /* Elimine un lock d'un programmeur sur un objet Retourne 0 si ok, -1 si le lock n'‚tait pas pr‚sent */ PUBLIC int PRJLOCK::del ( const char *objet) { ONE_LOCK *pt = tb; int nbloc = nblock; int copy=-1; const char *prog = user->getnom(); for (int i=0; iobjet)==0 && strcmp (prog,pt->prog)==0){ free (pt->prog); free (pt->objet); free (pt->path); copy = 0; nblock--; } } return copy; }