Beispiel #1
0
/*******************************************************************
 *
 * Description:     Delete a key in the hash table.
 *
 * Modified args:   None
 *
 * Return value:    0 if deleted, negative if not found.
 *
 *******************************************************************/
int
hash_delete_key(HASH_TABLE *ptbl, HASH_DATUM *key)
{
    struct hash_table_datum *entry;
    struct hash_table_datum *prev_entry = NULL;
    int index;

    index = HASH(key);
    entry = ((struct hash_table_datum **)ptbl)[index];

    while (entry != NULL && (key->size != entry->key.size || (memcmp(key->data, entry->key.data, key->size) != 0)))
    {
        prev_entry = entry;      /* Keep the previous entry, we need it when unlinking */
        entry = entry->next;
    }

    if (entry != NULL)
    {
        /* Unlink the old entry, and free memory */
        if (prev_entry != NULL)
        {
            prev_entry->next = entry->next;
        }
        else
        {
            /* Old entry was the first in the list */
            ((struct hash_table_datum **)ptbl)[index] = entry->next;
        }
        HASH_FREE(entry);

        return 0;
    }

    return -1;  /* Not found */
}
Beispiel #2
0
void
pkg_manifest_keys_free(struct pkg_manifest_key *key)
{
	if (key == NULL)
		return;

	HASH_FREE(key, pkg_manifest_key, pmk_free);
}
Beispiel #3
0
int N(HashDestroy)(void *t)
{
    HashTablePtr  table = (HashTablePtr)t;
    HashBucketPtr bucket;
    HashBucketPtr next;
    int           i;

    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */

    for (i = 0; i < HASH_SIZE; i++) {
	for (bucket = table->buckets[i]; bucket;) {
	    next = bucket->next;
	    HASH_FREE(bucket);
	    bucket = next;
	}
    }
    HASH_FREE(table);
    return 0;
}
Beispiel #4
0
/*******************************************************************
 *
 * Description:     Insert a entry in the table.
 *
 * Modified args:   None
 *
 * Return value:    0 if successful, negative if error.
 *
 *******************************************************************/
int
hash_insert(HASH_TABLE *ptbl, HASH_DATUM *key, HASH_DATUM *dat)
{
    struct hash_table_datum *pentry;
    struct hash_table_datum *new_entry;
    struct hash_table_datum *old_entry;
    struct hash_table_datum *prev_entry = NULL;
    int size;
    int index;

    index = HASH(key);
    old_entry = pentry = ((struct hash_table_datum **)ptbl)[index];

    /* Find out if there is an old entry */
    while (old_entry != NULL && (key->size != old_entry->key.size || (memcmp(key->data, old_entry->key.data, key->size) != 0)))
    {
        prev_entry = old_entry;      /* Keep the previous entry, we need it when unlinking */
        old_entry = old_entry->next;
    }

    if (old_entry != NULL)
    {
        /* Unlink the old entry, and free memory */
        if (prev_entry != NULL)
        {
            prev_entry->next = old_entry->next;
        }
        else
        {
            /* Old entry was the first in the list */
            ((struct hash_table_datum **)ptbl)[index] = old_entry->next;
        }
        HASH_FREE(old_entry);
    }

    /* Allocate memory for a new entry */
    size = dat->size + key->size + sizeof(struct hash_table_datum);
    if ((new_entry = HASH_MALLOC(size)) == NULL)
    {
        return -1;
    }

    /* Fill in the new entry */
    memcpy(new_entry->data, dat->data, dat->size);
    memcpy(&new_entry->data[dat->size], key->data, key->size);
    new_entry->key.size = key->size;
    new_entry->key.data = &new_entry->data[dat->size];
    new_entry->data_length = dat->size;

    /* Link in the new entry */
    new_entry->next = ((struct hash_table_datum **)ptbl)[index];
    ((struct hash_table_datum **)ptbl)[index] = new_entry;

    return 0;
}
Beispiel #5
0
/* Deallocate variable hash.  */
static void
free_var_hash (struct id_defined *head)
{
  struct id_defined *el, *tmp;
  HASH_ITER (hh, head, el, tmp)
    {
      struct tree_hash_node *hel, *tmp;
      if (el->phi_node)
	HASH_FREE (hh, el->phi_node, hel, tmp);
      HASH_DEL (head, el);
      free (el);
    }
}
Beispiel #6
0
/*******************************************************************
 *
 * Description:     Free hash table memory.
 *
 * Modified args:   None
 *
 * Return value:    0 if sucessful negative if error.
 *
 *******************************************************************/
int
hash_close(HASH_TABLE *tbl)
{
    int i;
    struct hash_table_datum *entry;
    struct hash_table_datum *free_entry;

    for (i = 0; i < (int)HASH_TABLE_SIZE; i++)
    {
        entry = ((struct hash_table_datum **)tbl)[i];
        if (entry != NULL)
        {
            do {
                free_entry = entry;
                entry = entry->next;
                HASH_FREE(free_entry);
            } while(entry != NULL);
        }
    }
    
    HASH_FREE(tbl);

    return 0;
}
Beispiel #7
0
void
pkg_shutdown(void)
{
	if (!parsed) {
		pkg_emit_error("pkg_shutdown() must be called after pkg_init()");
		_exit(EX_SOFTWARE);
		/* NOTREACHED */
	}

	ucl_object_unref(config);
	HASH_FREE(repos, pkg_repo_free);

	parsed = false;

	return;
}
Beispiel #8
0
int N(HashDelete)(void *t, unsigned long key)
{
    HashTablePtr  table = (HashTablePtr)t;
    unsigned long hash;
    HashBucketPtr bucket;

    if (table->magic != HASH_MAGIC) return -1; /* Bad magic */

    bucket = HashFind(table, key, &hash);

    if (!bucket) return 1;	/* Not found */

    table->buckets[hash] = bucket->next;
    HASH_FREE(bucket);
    return 0;
}
Beispiel #9
0
static void
pmk_free(struct pkg_manifest_key *key) {
	HASH_FREE(key->parser, dataparser, free);

	free(key);
}
/** 
 * Delete a class from class handle.
 *
 * API to dmClassByHandleDelete <Deailed desc>. 
 *
 *  @param this       object handle
 *  @param 
 * 
 *  @returns 
 *    ClRcT  CL_OK on success <br>
 *     CL_COR_SET_RC(CL_COR_ERR_NULL_PTR) on null parameter.
 *
 *  @todo      
 *
 */
ClRcT
dmClassByHandleDelete(CORClass_h classHandle
                      )
{
    ClRcT ret = CL_OK;
    CORClass_h tmp = 0;

    CL_FUNC_ENTER();

    if((!classHandle) || (!dmGlobal))
      {
        CL_FUNC_EXIT();  
        return(CL_COR_SET_RC(CL_COR_ERR_NULL_PTR));
      }
    
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ( "ClassDelete (Class:%04x)", 
                          classHandle->classId));


    /* check if instances are there and also base classes
     * by default cannot be deleted, they may be in other
     * inherited classes.
     */
    if (COR_CLASS_IS_BASE(*classHandle))
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "ClassDelete (Class:%04x) [Class is base]", classHandle->classId));
        ret = CL_COR_SET_RC(CL_COR_ERR_CLASS_IS_BASE);
    }
    else if(classHandle->objCount == 0 && 
           (classHandle->moClassInstances == 0))
      {
        
        /* get the handle to the parent class */
        HASH_GET(dmGlobal->classTable,classHandle->superClassId, tmp);
        if(tmp)
        {
            tmp->noOfChildren--;
            if(tmp->noOfChildren == 0)
                COR_CLASS_RESETAS_BASE(*tmp);
        }
        /* now we can remove from hash table & free it 
         */
        HASH_REMOVE(dmGlobal->classTable, classHandle->classId);
        /* free the attribute list hashtable and the elements in
         * the vector 
         */
        HASH_FREE(classHandle->attrList);
        COR_LLIST_FREE(classHandle->objFreeList);

        corVectorRemoveAll(&classHandle->attrs);
        /* nothing to remove in the objects vector */
        clHeapFree(classHandle);
      } 
    else 
      {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "ClassDelete (Class:%04x) [Instances present]", 
                              classHandle->classId));
        
        ret = CL_COR_SET_RC(CL_COR_ERR_CLASS_INSTANCES_PRESENT);
      }
    
    CL_FUNC_EXIT();
    return (ret);
}
/** 
 *  Class Type create API.
 *
 *  Creates a new class type with the given information like class id,
 *  number of attributes, max and minimum instances for the class.
 *  NOTE: What happens in case of different versions?? need to figure
 *  out.
 *
 *  @param id            class id
 *  @param nAttrs        Number of attributes in class.
 *
 *  @returns 
 *    CORClass_h   (non-null) valid class handle
 *      null(0)    on failure.
 * 
 */
ClRcT
dmClassCreate(ClCorClassTypeT id,
              ClCorClassTypeT inhId)
{
    CORClass_h tmp = 0;
    CORClass_h tmp1 = 0;
    ClRcT ret = CL_OK;

    CL_FUNC_ENTER();

    if(!dmGlobal)
    {
        CL_FUNC_EXIT();  
        return(CL_COR_SET_RC(CL_COR_ERR_NULL_PTR));
    }
    
    CL_DEBUG_PRINT(CL_DEBUG_TRACE, ( "ClassCreate (Class:%04x, Inh:%04x)", id, inhId));
    
    /* check if id & inhId is already present 
     */
    HASH_GET(dmGlobal->classTable, id, tmp);
    if(inhId > 0) 
    {
        HASH_GET(dmGlobal->classTable, inhId, tmp1);
    }
    
    if(tmp) 
    {
        CL_DEBUG_PRINT(CL_DEBUG_TRACE, ( "ClassCreate (Class:%04x, Inh:%04x) [Class present]", id, inhId));
        ret = CL_COR_SET_RC(CL_COR_ERR_CLASS_PRESENT);
    } 
    else if(tmp1 == 0 && inhId > 0) 
    {
        CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "ClassCreate (Class:%04x, Inh:%04x) [Superclass unknown]", id, inhId));
        ret = CL_COR_SET_RC(CL_COR_ERR_CLASS_NOT_PRESENT);
    } 
    else 
    {
        /* Create the new class
         */
        tmp = (CORClass_h) clHeapAllocate(sizeof(CORClass_t));
        if(tmp != 0) 
        {
            /* init stuff here */
            tmp->classId = id;
            tmp->superClassId = inhId; 
            tmp->size = -1;
            tmp->version.releaseCode = CL_RELEASE_VERSION;
            tmp->version.majorVersion = CL_MAJOR_VERSION;
            tmp->version.minorVersion = CL_MINOR_VERSION;
            tmp->flags = 0;
            tmp->moClassInstances = 0;

            if (CL_OK != (ret = HASH_CREATE(&tmp->attrList)))
            { 
                  clHeapFree(tmp);
                  CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "Failed to create hash table for Attributes [rc 0x%x]", ret));
                  CL_FUNC_EXIT();  
                  return (ret);
            }
        
            if (CL_OK != (ret = COR_LLIST_CREATE(&tmp->objFreeList)))
            {
                clHeapFree(tmp);
                CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "Failed to create hash table for free objects [rc 0x%x]", ret));
                CL_FUNC_EXIT();
                return (ret);
            }

            tmp->nAttrs = 0;
            tmp->noOfChildren = 0;
            tmp->recordId = 1;
            tmp->objCount = 0;
            tmp->objBlockSz = COR_OBJ_DEF_BLOCK_SIZE;

            /* init the vector, check the return value
             * and remove class/hashtable
             */
            if (CL_OK != (ret = corVectorInit(&tmp->attrs, 
                          sizeof(CORAttr_t),
                          COR_CLASS_DEF_BLOCK_SIZE)))
            {
                HASH_FREE(tmp->attrList);
                COR_LLIST_FREE(tmp->objFreeList);
                clHeapFree(tmp);
    	        CL_FUNC_EXIT();  
     	        return (ret);
	        }

            /* add the newly created class to the class table */
            if (CL_OK != (ret = HASH_PUT(dmGlobal->classTable, id, tmp)))
	        {
              corVectorRemoveAll(&tmp->attrs);
              HASH_FREE(tmp->attrList);
              COR_LLIST_FREE(tmp->objFreeList);
              clHeapFree(tmp);
       	      CL_DEBUG_PRINT(CL_DEBUG_ERROR, ( "Failed to add class in Data Manager [rc 0x%x]", ret));
    	      CL_FUNC_EXIT();  
     	      return (ret);
	        }
            /* declare the inh class as base class */
            if(inhId > 0) 
            {
                tmp1->noOfChildren++;
                COR_CLASS_SETAS_BASE(*tmp1);
            }
        } 
        else 
        {
	        clLogWrite(CL_LOG_HANDLE_APP, CL_LOG_DEBUG, NULL,
				CL_LOG_MESSAGE_0_MEMORY_ALLOCATION_FAILED);
                CL_DEBUG_PRINT(CL_DEBUG_CRITICAL, (CL_COR_ERR_STR_MEM_ALLOC_FAIL)); 
            ret = CL_COR_SET_RC(CL_COR_ERR_NO_MEM);
        }
    }

    CL_FUNC_EXIT();  
    return (ret);
}
Beispiel #12
0
static int
pkg_create_from_dir(struct pkg *pkg, const char *root,
    struct packing *pkg_archive)
{
	char		 fpath[MAXPATHLEN];
	struct pkg_file	*file = NULL;
	struct pkg_dir	*dir = NULL;
	char		*m;
	int		 ret;
	const char	*mtree;
	bool		 developer;
	struct stat	 st;
	char		 sha256[SHA256_DIGEST_LENGTH * 2 + 1];
	int64_t		 flatsize = 0;
	const ucl_object_t	*obj, *an;
	struct hardlinks *hardlinks = NULL;

	if (pkg_is_valid(pkg) != EPKG_OK) {
		pkg_emit_error("the package is not valid");
		return (EPKG_FATAL);
	}

	pkg_get(pkg, PKG_ANNOTATIONS, &an);
	obj = pkg_object_find(an, "relocated");

	/*
	 * Get / compute size / checksum if not provided in the manifest
	 */
	while (pkg_files(pkg, &file) == EPKG_OK) {
		const char *pkg_path = pkg_file_path(file);
		const char *pkg_sum = pkg_file_cksum(file);

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    obj ? pkg_object_string(obj) : "", pkg_path);

		if (lstat(fpath, &st) == -1) {
			pkg_emit_error("file '%s' is missing", fpath);
			return (EPKG_FATAL);
		}

		if (file->size == 0)
			file->size = (int64_t)st.st_size;

		if (st.st_nlink == 1 || !check_for_hardlink(hardlinks, &st)) {
			flatsize += file->size;
		}

		if (S_ISLNK(st.st_mode)) {

			if (pkg_sum == NULL || pkg_sum[0] == '\0') {
				if (pkg_symlink_cksum(fpath, root, sha256) == EPKG_OK)
					strlcpy(file->sum, sha256, sizeof(file->sum));
				else
					return (EPKG_FATAL);
			}
		}
		else {
			if (pkg_sum == NULL || pkg_sum[0] == '\0') {
				if (pkg->type == PKG_OLD_FILE) {
					if (md5_file(fpath, sha256) != EPKG_OK)
						return (EPKG_FATAL);
				} else {
					if (sha256_file(fpath, sha256) != EPKG_OK)
						return (EPKG_FATAL);
				}
				strlcpy(file->sum, sha256, sizeof(file->sum));
			}
		}
	}
	pkg_set(pkg, PKG_FLATSIZE, flatsize);
	HASH_FREE(hardlinks, free);

	if (pkg->type == PKG_OLD_FILE) {
		const char *desc, *display, *comment;
		char oldcomment[BUFSIZ];

		pkg_old_emit_content(pkg, &m);
		packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m));
		free(m);

		pkg_get(pkg, PKG_DESC, &desc, PKG_MESSAGE, &display, PKG_COMMENT, &comment);
		packing_append_buffer(pkg_archive, desc, "+DESC", strlen(desc));
		packing_append_buffer(pkg_archive, display, "+DISPLAY", strlen(display));
		snprintf(oldcomment, sizeof(oldcomment), "%s\n", comment);
		packing_append_buffer(pkg_archive, oldcomment, "+COMMENT", strlen(oldcomment));
	} else {
		/*
		 * Register shared libraries used by the package if
		 * SHLIBS enabled in conf.  Deletes shlib info if not.
		 */
		struct sbuf *b = sbuf_new_auto();

		pkg_register_shlibs(pkg, root);

		pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL);
		packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b));
		sbuf_clear(b);
		pkg_emit_manifest_sbuf(pkg, b, 0, NULL);
		sbuf_finish(b);
		packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b));
		sbuf_delete(b);
	}

	pkg_get(pkg, PKG_MTREE, &mtree);
	if (mtree != NULL)
		packing_append_buffer(pkg_archive, mtree, "+MTREE_DIRS",
		    strlen(mtree));

	while (pkg_files(pkg, &file) == EPKG_OK) {
		const char *pkg_path = pkg_file_path(file);

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    obj ? pkg_object_string(obj) : "", pkg_path);

		ret = packing_append_file_attr(pkg_archive, fpath, pkg_path,
		    file->uname, file->gname, file->perm);
		developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
		if (developer && ret != EPKG_OK)
			return (ret);
	}

	while (pkg_dirs(pkg, &dir) == EPKG_OK) {
		const char *pkg_path = pkg_dir_path(dir);

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    obj ? pkg_object_string(obj) : "", pkg_path);

		ret = packing_append_file_attr(pkg_archive, fpath, pkg_path,
		    dir->uname, dir->gname, dir->perm);
		developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
		if (developer && ret != EPKG_OK)
			return (ret);
	}

	return (EPKG_OK);
}
Beispiel #13
0
static int
pkg_create_from_dir(struct pkg *pkg, const char *root,
    struct packing *pkg_archive)
{
	char		 fpath[MAXPATHLEN];
	struct pkg_file	*file = NULL;
	struct pkg_dir	*dir = NULL;
	char		*m;
	int		 ret;
	bool		 developer;
	struct stat	 st;
	char		 sha256[SHA256_DIGEST_LENGTH * 2 + 1];
	int64_t		 flatsize = 0;
	const char	*relocation;
	struct hardlinks *hardlinks = NULL;

	if (pkg_is_valid(pkg) != EPKG_OK) {
		pkg_emit_error("the package is not valid");
		return (EPKG_FATAL);
	}

	relocation = pkg_kv_get(&pkg->annotations, "relocated");
	if (relocation == NULL)
		relocation = "";

	/*
	 * Get / compute size / checksum if not provided in the manifest
	 */
	while (pkg_files(pkg, &file) == EPKG_OK) {

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    relocation, file->path);

		if (lstat(fpath, &st) == -1) {
			pkg_emit_error("file '%s' is missing", fpath);
			return (EPKG_FATAL);
		}

		if (file->size == 0)
			file->size = (int64_t)st.st_size;

		if (st.st_nlink == 1 || !check_for_hardlink(&hardlinks, &st)) {
			flatsize += file->size;
		}

		if (S_ISLNK(st.st_mode)) {

			if (file->sum[0] == '\0') {
				if (pkg_symlink_cksum(fpath, root, sha256) == EPKG_OK)
					strlcpy(file->sum, sha256, sizeof(file->sum));
				else
					return (EPKG_FATAL);
			}
		}
		else {
			if (file->sum[0] == '\0') {
				if (pkg->type == PKG_OLD_FILE) {
					if (md5_file(fpath, sha256) != EPKG_OK)
						return (EPKG_FATAL);
				} else {
					if (sha256_file(fpath, sha256) != EPKG_OK)
						return (EPKG_FATAL);
				}
				strlcpy(file->sum, sha256, sizeof(file->sum));
			}
		}
	}
	pkg->flatsize = flatsize;
	HASH_FREE(hardlinks, free);

	if (pkg->type == PKG_OLD_FILE) {
		char oldcomment[BUFSIZ];

		pkg_old_emit_content(pkg, &m);
		packing_append_buffer(pkg_archive, m, "+CONTENTS", strlen(m));
		free(m);

		packing_append_buffer(pkg_archive, pkg->desc, "+DESC", strlen(pkg->desc));
		packing_append_buffer(pkg_archive, pkg->message, "+DISPLAY", strlen(pkg->message));
		pkg_snprintf(oldcomment, sizeof(oldcomment), "%c\n", pkg);
		packing_append_buffer(pkg_archive, oldcomment, "+COMMENT", strlen(oldcomment));
	} else {
		/*
		 * Register shared libraries used by the package if
		 * SHLIBS enabled in conf.  Deletes shlib info if not.
		 */
		struct sbuf *b = sbuf_new_auto();

		pkg_analyse_files(NULL, pkg, root);

		pkg_emit_manifest_sbuf(pkg, b, PKG_MANIFEST_EMIT_COMPACT, NULL);
		packing_append_buffer(pkg_archive, sbuf_data(b), "+COMPACT_MANIFEST", sbuf_len(b));
		sbuf_clear(b);
		pkg_emit_manifest_sbuf(pkg, b, 0, NULL);
		sbuf_finish(b);
		packing_append_buffer(pkg_archive, sbuf_data(b), "+MANIFEST", sbuf_len(b));
		sbuf_delete(b);
	}

	while (pkg_files(pkg, &file) == EPKG_OK) {

		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    relocation, file->path);

		ret = packing_append_file_attr(pkg_archive, fpath, file->path,
		    file->uname, file->gname, file->perm);
		developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
		if (developer && ret != EPKG_OK)
			return (ret);
	}

	while (pkg_dirs(pkg, &dir) == EPKG_OK) {
		snprintf(fpath, sizeof(fpath), "%s%s%s", root ? root : "",
		    relocation, dir->path);

		ret = packing_append_file_attr(pkg_archive, fpath, dir->path,
		    dir->uname, dir->gname, dir->perm);
		developer = pkg_object_bool(pkg_config_get("DEVELOPER_MODE"));
		if (developer && ret != EPKG_OK)
			return (ret);
	}

	return (EPKG_OK);
}