Skip to content

Commit f6a67ce

Browse files
committed
MNBSD-1 MFC: libmport fixes for sample files.
1 parent 7005969 commit f6a67ce

13 files changed

Lines changed: 117 additions & 46 deletions

lib/libmport/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ SRCS= asset.c bundle_write.c bundle_read.c plist.c create_primative.c db.c \
88
default_cbs.c merge_primative.c bundle_read_install_pkg.c \
99
update_primative.c bundle_read_update_pkg.c pkgmeta.c \
1010
fetch.c index.c index_depends.c install.c clean.c setting.c \
11-
stats.c verify.c lock.c mkdir.c
11+
stats.c verify.c lock.c mkdir.c hashmap.c
1212
INCS= mport.h mport_dispatch.h
1313

1414
CFLAGS+= -I${.CURDIR} -fblocks -g

lib/libmport/bundle_read_install_pkg.c

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,9 @@ get_file_count(mportInstance *mport, char *pkg_name, int *file_total)
149149
__block char *err;
150150

151151
if (mport_db_prepare(mport->db, &count,
152-
"SELECT COUNT(*) FROM stub.assets WHERE (type=%i or type=%i or type=%i or type=%i) AND pkg=%Q",
153-
ASSET_FILE, ASSET_SAMPLE, ASSET_SHELL, ASSET_FILE_OWNER_MODE, pkg_name) != MPORT_OK) {
152+
"SELECT COUNT(*) FROM stub.assets WHERE (type=%i or type=%i or type=%i or type=%i or type=%i) AND pkg=%Q",
153+
ASSET_FILE, ASSET_SAMPLE, ASSET_SHELL, ASSET_FILE_OWNER_MODE, ASSET_SAMPLE_OWNER_MODE,
154+
pkg_name) != MPORT_OK) {
154155
sqlite3_finalize(count);
155156
RETURN_CURRENT_ERROR;
156157
}
@@ -236,8 +237,10 @@ create_sample_file(mportInstance *mport, char *cwd, const char *file)
236237
char nonSample[FILENAME_MAX * 2];
237238
char secondFile[FILENAME_MAX];
238239

239-
strlcpy(nonSample, file, FILENAME_MAX * 2);
240-
(void) snprintf(nonSample, FILENAME_MAX, "%s%s/%s", mport->root, cwd, file);
240+
if (file[0] != '/')
241+
(void) snprintf(nonSample, FILENAME_MAX, "%s%s/%s", mport->root, cwd, file);
242+
else
243+
strlcpy(nonSample, file, FILENAME_MAX * 2);
241244
char** fileargv = parse_sample(nonSample);
242245

243246
if (fileargv[1] != '\0') {
@@ -256,8 +259,9 @@ create_sample_file(mportInstance *mport, char *cwd, const char *file)
256259
if (sptr != NULL) {
257260
sptr[0] = '\0'; /* hack off .sample */
258261
if (!mport_file_exists(nonSample)) {
259-
if (mport_copy_file(file, nonSample) != MPORT_OK)
262+
if (mport_copy_file(file, nonSample) != MPORT_OK) {
260263
RETURN_CURRENT_ERROR;
264+
}
261265
}
262266
}
263267
}
@@ -494,12 +498,18 @@ do_actual_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMet
494498
case ASSET_SHELL:
495499
/* FALLS THROUGH */
496500
case ASSET_SAMPLE:
501+
/* FALLS THROUGH */
502+
case ASSET_SAMPLE_OWNER_MODE:
497503
if (mport_bundle_read_next_entry(bundle, &entry) != MPORT_OK)
498504
goto ERROR;
499505

500-
(void) snprintf(file, FILENAME_MAX, "%s%s/%s", mport->root, cwd, e->data);
506+
if (e->data[0] == '/') {
507+
(void) snprintf(file, FILENAME_MAX, "%s", e->data);
508+
} else {
509+
(void) snprintf(file, FILENAME_MAX, "%s%s/%s", mport->root, cwd, e->data);
510+
}
501511

502-
if (e->type == ASSET_SAMPLE)
512+
if (e->type == ASSET_SAMPLE || e->type == ASSET_SAMPLE_OWNER_MODE)
503513
for (int ch = 0; ch < FILENAME_MAX; ch++) {
504514
if (file[ch] == '\0')
505515
break;
@@ -525,7 +535,7 @@ do_actual_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMet
525535
}
526536

527537
if (S_ISREG(sb.st_mode)) {
528-
if (e->type == ASSET_FILE_OWNER_MODE) {
538+
if (e->type == ASSET_FILE_OWNER_MODE || e->type == ASSET_SAMPLE_OWNER_MODE) {
529539
/* Test for owner and group settings, otherwise roll with our default. */
530540
if (e->owner != NULL && e->group != NULL && e->owner[0] != '\0' &&
531541
e->group[0] != '\0') {
@@ -575,7 +585,8 @@ do_actual_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMet
575585
SET_ERRORX(MPORT_ERR_FATAL, "Unable to stat file %s", file);
576586
goto ERROR;
577587
}
578-
if (e->type == ASSET_FILE_OWNER_MODE && e->mode != NULL) {
588+
if ((e->type == ASSET_SAMPLE_OWNER_MODE ||
589+
e->type == ASSET_FILE_OWNER_MODE) && e->mode != NULL) {
579590
if ((set = setmode(e->mode)) == NULL) {
580591
SET_ERROR(MPORT_ERR_FATAL, "Unable to set mode");
581592
goto ERROR;
@@ -598,14 +609,16 @@ do_actual_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMet
598609
if (e->type == ASSET_SHELL && mport_shell_register(file) != MPORT_OK) {
599610
goto ERROR;
600611
}
612+
}
601613

602614
/* for sample files, if we don't have an existing file, make a new one */
603-
if (e->type == ASSET_SAMPLE && create_sample_file(mport, cwd, e->data) != MPORT_OK) {
615+
if ((e->type == ASSET_SAMPLE || e->type == ASSET_SAMPLE_OWNER_MODE) &&
616+
create_sample_file(mport, cwd, e->data) != MPORT_OK) {
604617
SET_ERRORX(MPORT_ERR_FATAL, "Unable to create sample file from %s",
605618
file);
606619
goto ERROR;
607620
}
608-
}
621+
609622

610623
(mport->progress_step_cb)(++file_count, file_total, file);
611624

@@ -628,7 +641,7 @@ do_actual_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMet
628641
return;
629642
}
630643
if (e->type == ASSET_FILE || e->type == ASSET_SAMPLE || e->type == ASSET_SHELL ||
631-
e->type == ASSET_FILE_OWNER_MODE) {
644+
e->type == ASSET_FILE_OWNER_MODE || e->type == ASSET_SAMPLE_OWNER_MODE) {
632645
/* don't put the root in the database! */
633646
if (sqlite3_bind_text(insert, 2, filePtr + strlen(mport->root), -1, SQLITE_STATIC) !=
634647
SQLITE_OK) {
@@ -758,6 +771,7 @@ do_actual_install(mportInstance *mport, mportBundleRead *bundle, mportPackageMet
758771
return;
759772
}
760773

774+
sqlite3_clear_bindings(insert);
761775
sqlite3_reset(insert);
762776
});
763777

@@ -946,7 +960,7 @@ display_pkg_msg(mportInstance *mport, mportBundleRead *bundle, mportPackageMeta
946960
/* if we couldn't stat the file, we assume there isn't a pkg-msg */
947961
return MPORT_OK;
948962

949-
if ((file = fopen(filename, "r")) == NULL)
963+
if ((file = fopen(filename, "re")) == NULL)
950964
RETURN_ERRORX(MPORT_ERR_FATAL, "Couldn't open %s: %s", filename, strerror(errno));
951965

952966
if ((buf = (char *) calloc((size_t) (st.st_size + 1), sizeof(char))) == NULL)

lib/libmport/clean.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,45 @@ mport_clean_oldpackages(mportInstance *mport)
7777
continue;
7878

7979
if (mport_index_search(mport, &indexEntry, "bundlefile=%Q", de->d_name) != MPORT_OK) {
80+
mport_call_msg_cb(mport, "failed to search index %s: ", mport_err_string());
8081
continue;
8182
}
8283

84+
asprintf(&path, "%s/%s", MPORT_FETCH_STAGING_DIR, de->d_name);
85+
if (path == NULL) {
86+
if (indexEntry != NULL)
87+
mport_index_entry_free_vec(indexEntry);
88+
continue;
89+
}
90+
8391
if (indexEntry == NULL || *indexEntry == NULL) {
84-
asprintf(&path, "%s/%s", MPORT_FETCH_STAGING_DIR, de->d_name);
85-
if (path != NULL) {
8692
if (unlink(path) < 0) {
8793
error_code = SET_ERRORX(MPORT_ERR_FATAL, "Could not delete file %s: %s",
8894
path, strerror(errno));
8995
mport_call_msg_cb(mport, "%s\n", mport_err_string());
9096
} else {
9197
deleted++;
9298
}
93-
free(path);
94-
}
99+
} else if (mport_verify_hash(path, (*indexEntry)->hash) == 0) {
100+
if (unlink(path) < 0) {
101+
102+
error_code = SET_ERRORX(MPORT_ERR_FATAL, "Could not delete file %s: %s",
103+
104+
path, strerror(errno));
105+
106+
mport_call_msg_cb(mport, "%s\n", mport_err_string());
107+
108+
} else {
109+
110+
deleted++;
111+
112+
}
113+
mport_index_entry_free_vec(indexEntry);
95114
} else {
96115
mport_index_entry_free_vec(indexEntry);
97116
}
117+
118+
free(path);
98119
}
99120

100121
closedir(d);

lib/libmport/create_primative.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,15 +169,15 @@ insert_assetlist(sqlite3 *db, mportAssetList *assetlist, mportPackageMeta *pack,
169169
}
170170

171171
if (e->type == ASSET_FILE || e->type == ASSET_SAMPLE || e->type == ASSET_SHELL ||
172-
e->type == ASSET_FILE_OWNER_MODE) {
172+
e->type == ASSET_FILE_OWNER_MODE || e->type == ASSET_SAMPLE_OWNER_MODE) {
173173
/* Don't prepend cwd onto absolute file paths (this is useful for update) */
174174
if (e->data[0] == '/') {
175175
(void) snprintf(file, FILENAME_MAX, "%s%s", extra->sourcedir, e->data);
176176
} else {
177177
(void) snprintf(file, FILENAME_MAX, "%s/%s", cwd, e->data);
178178
}
179179

180-
if (e->type == ASSET_SAMPLE) {
180+
if (e->type == ASSET_SAMPLE || e->type == ASSET_SAMPLE_OWNER_MODE) {
181181
for (int ch = 0; ch < FILENAME_MAX; ch++) {
182182
if (file[ch] == '\0')
183183
break;
@@ -210,6 +210,7 @@ insert_assetlist(sqlite3 *db, mportAssetList *assetlist, mportPackageMeta *pack,
210210
RETURN_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
211211
}
212212

213+
sqlite3_clear_bindings(stmnt);
213214
sqlite3_reset(stmnt);
214215
}
215216

@@ -344,6 +345,7 @@ insert_categories(sqlite3 *db, mportPackageMeta *pkg)
344345
if (sqlite3_step(stmt) != SQLITE_DONE) {
345346
error_code = SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
346347
}
348+
sqlite3_clear_bindings(stmt);
347349
sqlite3_reset(stmt);
348350
});
349351
i++;
@@ -403,6 +405,7 @@ insert_conflicts(sqlite3 *db, mportPackageMeta *pack, mportCreateExtras *extra)
403405
error_code = SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
404406
return;
405407
}
408+
sqlite3_clear_bindings(stmnt);
406409
sqlite3_reset(stmnt);
407410
});
408411
conflict++;
@@ -489,6 +492,8 @@ insert_depends(sqlite3 *db, mportPackageMeta *pack, mportCreateExtras *extra)
489492
error_code = SET_ERROR(MPORT_ERR_FATAL, sqlite3_errmsg(db));
490493
return;
491494
}
495+
496+
sqlite3_clear_bindings(stmnt);
492497
sqlite3_reset(stmnt);
493498
});
494499
depend++;
@@ -579,7 +584,7 @@ archive_assetlistfiles(mportBundleWrite *bundle, mportPackageMeta *pack, mportCr
579584
if (e->type == ASSET_CWD)
580585
cwd = e->data == NULL ? pack->prefix : e->data;
581586

582-
if (e->type != ASSET_FILE && e->type != ASSET_SAMPLE &&
587+
if (e->type != ASSET_FILE && e->type != ASSET_SAMPLE && e->type != ASSET_SAMPLE_OWNER_MODE &&
583588
e->type != ASSET_SHELL && e->type != ASSET_FILE_OWNER_MODE) {
584589
continue;
585590
}
@@ -591,7 +596,7 @@ archive_assetlistfiles(mportBundleWrite *bundle, mportPackageMeta *pack, mportCr
591596
(void) snprintf(filename, FILENAME_MAX, "%s/%s/%s", extra->sourcedir, cwd, e->data);
592597
}
593598

594-
if (e->type == ASSET_SAMPLE) {
599+
if (e->type == ASSET_SAMPLE || e->type == ASSET_SAMPLE_OWNER_MODE) {
595600
// eat the second filename if it exists.
596601
for (int ch = 0; ch < FILENAME_MAX; ch++) {
597602
if (filename[ch] == '\0')

lib/libmport/delete_primative.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ mport_delete_primative(mportInstance *mport, mportPackageMeta *pack, int force)
8989

9090
/* get the file count for the progress meter */
9191
if (mport_db_prepare(mport->db, &stmt,
92-
"SELECT COUNT(*) FROM assets WHERE (type=%i or type=%i or type=%i or type=%i) AND pkg=%Q", ASSET_FILE,
93-
ASSET_SAMPLE, ASSET_SHELL, ASSET_FILE_OWNER_MODE, pack->name) != MPORT_OK)
92+
"SELECT COUNT(*) FROM assets WHERE (type=%i or type=%i or type=%i or type=%i or type=%i) AND pkg=%Q", ASSET_FILE,
93+
ASSET_SAMPLE, ASSET_SAMPLE_OWNER_MODE, ASSET_SHELL, ASSET_FILE_OWNER_MODE, pack->name) != MPORT_OK)
9494
RETURN_CURRENT_ERROR;
9595

9696
switch (sqlite3_step(stmt)) {
@@ -163,6 +163,8 @@ mport_delete_primative(mportInstance *mport, mportPackageMeta *pack, int force)
163163
case ASSET_SHELL:
164164
/* falls through */
165165
case ASSET_SAMPLE:
166+
/* falls through */
167+
case ASSET_SAMPLE_OWNER_MODE:
166168
(mport->progress_step_cb)(++current, total, file);
167169

168170
if (lstat(file, &st) != 0) {
@@ -177,7 +179,7 @@ mport_delete_primative(mportInstance *mport, mportPackageMeta *pack, int force)
177179
if (checksum == NULL || md5 == NULL || strcmp(md5, checksum) != 0)
178180
mport_call_msg_cb(mport, "Checksum mismatch: %s", file);
179181

180-
if (type == ASSET_SAMPLE) {
182+
if (type == ASSET_SAMPLE || type == ASSET_SAMPLE_OWNER_MODE) {
181183
char sample_md5[33];
182184
char nonSample[FILENAME_MAX];
183185
strlcpy(nonSample, file, FILENAME_MAX);

lib/libmport/hashmap.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/*
22
* Generic map implementation.
3+
* $MidnightBSD$
34
*/
45
#include "hashmap.h"
56

@@ -25,6 +26,11 @@ typedef struct _hashmap_map{
2526
hashmap_element *data;
2627
} hashmap_map;
2728

29+
unsigned long crc32(const unsigned char *s, unsigned int len);
30+
unsigned int hashmap_hash_int(hashmap_map * m, char* keystring);
31+
int hashmap_hash(map_t in, char* key);
32+
int hashmap_rehash(map_t in);
33+
2834
/*
2935
* Return an empty hashmap, or NULL on failure.
3036
*/
@@ -394,4 +400,4 @@ int hashmap_length(map_t in){
394400
hashmap_map* m = (hashmap_map *) in;
395401
if(m != NULL) return m->size;
396402
else return 0;
397-
}
403+
}

lib/libmport/hashmap.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*
66
* Modified by Pete Warden to fix a serious performance problem, support strings as keys
77
* and removed thread synchronization - http://petewarden.typepad.com
8+
* $MidnightBSD$
89
*/
910
#ifndef __HASHMAP_H__
1011
#define __HASHMAP_H__
@@ -36,7 +37,7 @@ typedef any_t map_t;
3637
/*
3738
* Return an empty hashmap. Returns NULL if empty.
3839
*/
39-
extern map_t hashmap_new();
40+
map_t hashmap_new(void);
4041

4142
/*
4243
* Iteratively call f with argument (item, data) for
@@ -45,37 +46,37 @@ extern map_t hashmap_new();
4546
* than MAP_OK the traversal is terminated. f must
4647
* not reenter any hashmap functions, or deadlock may arise.
4748
*/
48-
extern int hashmap_iterate(map_t in, PFany f, any_t item);
49+
int hashmap_iterate(map_t in, PFany f, any_t item);
4950

5051
/*
5152
* Add an element to the hashmap. Return MAP_OK or MAP_OMEM.
5253
*/
53-
extern int hashmap_put(map_t in, char* key, any_t value);
54+
int hashmap_put(map_t in, char* key, any_t value);
5455

5556
/*
5657
* Get an element from the hashmap. Return MAP_OK or MAP_MISSING.
5758
*/
58-
extern int hashmap_get(map_t in, char* key, any_t *arg);
59+
int hashmap_get(map_t in, char* key, any_t *arg);
5960

6061
/*
6162
* Remove an element from the hashmap. Return MAP_OK or MAP_MISSING.
6263
*/
63-
extern int hashmap_remove(map_t in, char* key);
64+
int hashmap_remove(map_t in, char* key);
6465

6566
/*
6667
* Get any element. Return MAP_OK or MAP_MISSING.
6768
* remove - should the element be removed from the hashmap
6869
*/
69-
extern int hashmap_get_one(map_t in, any_t *arg, int remove);
70+
int hashmap_get_one(map_t in, any_t *arg, int remove);
7071

7172
/*
7273
* Free the hashmap
7374
*/
74-
extern void hashmap_free(map_t in);
75+
void hashmap_free(map_t in);
7576

7677
/*
7778
* Get the current size of a hashmap
7879
*/
79-
extern int hashmap_length(map_t in);
80+
int hashmap_length(map_t in);
8081

81-
#endif __HASHMAP_H__
82+
#endif

lib/libmport/merge_primative.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ static int archive_package_files(mportBundleWrite *bundle, sqlite3 *db, struct t
401401
RETURN_CURRENT_ERROR;
402402
}
403403

404-
if (mport_db_prepare(db, &files, "SELECT data FROM assets WHERE pkg=%Q AND (type=%i or type=%i)", pkgname, ASSET_FILE, ASSET_SAMPLE) != MPORT_OK) {
404+
if (mport_db_prepare(db, &files, "SELECT data FROM assets WHERE pkg=%Q AND (type=%i or type=%i or type=%i)", pkgname, ASSET_FILE, ASSET_SAMPLE, ASSET_SAMPLE_OWNER_MODE) != MPORT_OK) {
405405
mport_bundle_read_finish(NULL, inbundle);
406406
sqlite3_finalize(stmt);
407407
RETURN_CURRENT_ERROR;

lib/libmport/mport.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ enum _AssetListEntryType {
8383
ASSET_DEPORIGIN, ASSET_NOINST, ASSET_DISPLAY, ASSET_DIR,
8484
ASSET_SAMPLE, ASSET_SHELL,
8585
ASSET_PREEXEC, ASSET_PREUNEXEC, ASSET_POSTEXEC, ASSET_POSTUNEXEC,
86-
ASSET_FILE_OWNER_MODE, ASSET_DIR_OWNER_MODE
86+
ASSET_FILE_OWNER_MODE, ASSET_DIR_OWNER_MODE,
87+
ASSET_SAMPLE_OWNER_MODE
8788
};
8889

8990
typedef enum _AssetListEntryType mportAssetListEntryType;

0 commit comments

Comments
 (0)