opkg: re-arrange source code into sub-directories
[project/opkg-lede.git] / libopkg / pkg_dest.c
1 /* pkg_dest.c - the itsy package management system
2
3 Carl D. Worth
4
5 Copyright (C) 2001 University of Southern California
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2, or (at
10 your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16 */
17
18 #include "opkg.h"
19
20 #include "pkg_dest.h"
21 #include "file_util.h"
22 #include "str_util.h"
23 #include "sprintf_alloc.h"
24
25 int pkg_dest_init(pkg_dest_t *dest, const char *name, const char *root_dir,const char * lists_dir)
26 {
27 dest->name = strdup(name);
28
29 /* Guarantee that dest->root_dir ends with a '/' */
30 if (str_ends_with(root_dir, "/")) {
31 dest->root_dir = strdup(root_dir);
32 } else {
33 sprintf_alloc(&dest->root_dir, "%s/", root_dir);
34 }
35 file_mkdir_hier(dest->root_dir, 0755);
36
37 sprintf_alloc(&dest->opkg_dir, "%s%s",
38 dest->root_dir, OPKG_STATE_DIR_PREFIX);
39 file_mkdir_hier(dest->opkg_dir, 0755);
40
41 if (str_starts_with (lists_dir, "/"))
42 sprintf_alloc(&dest->lists_dir, "%s", lists_dir);
43 else
44 sprintf_alloc(&dest->lists_dir, "/%s", lists_dir);
45
46 file_mkdir_hier(dest->lists_dir, 0755);
47
48 sprintf_alloc(&dest->info_dir, "%s/%s",
49 dest->opkg_dir, OPKG_INFO_DIR_SUFFIX);
50 file_mkdir_hier(dest->info_dir, 0755);
51
52 sprintf_alloc(&dest->status_file_name, "%s/%s",
53 dest->opkg_dir, OPKG_STATUS_FILE_SUFFIX);
54
55 sprintf_alloc(&dest->status_file_tmp_name, "%s/%s.tmp",
56 dest->opkg_dir, OPKG_STATUS_FILE_SUFFIX);
57
58 dest->status_file = NULL;
59
60 return 0;
61 }
62
63 void pkg_dest_deinit(pkg_dest_t *dest)
64 {
65 free(dest->name);
66 dest->name = NULL;
67
68 free(dest->root_dir);
69 dest->root_dir = NULL;
70
71 free(dest->opkg_dir);
72 dest->opkg_dir = NULL;
73
74 free(dest->lists_dir);
75 dest->lists_dir = NULL;
76
77 free(dest->info_dir);
78 dest->info_dir = NULL;
79
80 free(dest->status_file_name);
81 dest->status_file_name = NULL;
82
83 free(dest->status_file_tmp_name);
84 dest->status_file_tmp_name = NULL;
85
86 if (dest->status_file) {
87 fclose(dest->status_file);
88 }
89 dest->status_file = NULL;
90
91 dest->root_dir = NULL;
92 }