| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include <config.h> |
| 20 |
#include <stdlib.h> |
| 21 |
#include <obstack.h> |
| 22 |
#include <xalloc.h> |
| 23 |
#include <error.h> |
| 24 |
|
| 25 |
#include "idfile.h" |
| 26 |
#include "ignore-value.h" |
| 27 |
#include "idu-hash.h" |
| 28 |
#include "xnls.h" |
| 29 |
|
| 30 |
static int file_link_qsort_compare (void const *x, void const *y); |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
void |
| 37 |
serialize_file_links (struct idhead *idhp) |
| 38 |
{ |
| 39 |
struct file_link **flinks_0; |
| 40 |
struct file_link **flinks; |
| 41 |
struct file_link **end; |
| 42 |
struct file_link **parents_0; |
| 43 |
struct file_link **parents; |
| 44 |
unsigned long parent_index = 0; |
| 45 |
int max_link = 0; |
| 46 |
|
| 47 |
flinks_0 = (struct file_link **) hash_dump (&idhp->idh_file_link_table, |
| 48 |
0, file_link_qsort_compare); |
| 49 |
end = &flinks_0[idhp->idh_file_link_table.ht_fill]; |
| 50 |
parents = parents_0 = xmalloc (sizeof(struct file_link *) * idhp->idh_file_link_table.ht_fill); |
| 51 |
for (flinks = flinks_0; flinks < end; flinks++) |
| 52 |
{ |
| 53 |
struct file_link *flink = *flinks; |
| 54 |
int name_length; |
| 55 |
|
| 56 |
if (!(flink->fl_flags & FL_USED)) |
| 57 |
break; |
| 58 |
name_length = strlen (flink->fl_name); |
| 59 |
if (name_length > max_link) |
| 60 |
max_link = name_length; |
| 61 |
io_write (idhp->idh_FILE, flink->fl_name, 0, IO_TYPE_STR); |
| 62 |
io_write (idhp->idh_FILE, &flink->fl_flags, sizeof (flink->fl_flags), IO_TYPE_INT); |
| 63 |
io_write (idhp->idh_FILE, (IS_ROOT_FILE_LINK (flink) |
| 64 |
? &parent_index : &flink->fl_parent->fl_index), |
| 65 |
FL_PARENT_INDEX_BYTES, IO_TYPE_INT); |
| 66 |
*parents++ = flink->fl_parent; |
| 67 |
flink->fl_index = parent_index++; |
| 68 |
} |
| 69 |
|
| 70 |
for ((flinks = flinks_0), (parents = parents_0); flinks < end; flinks++) |
| 71 |
{ |
| 72 |
struct file_link *flink = *flinks; |
| 73 |
if (!(flink->fl_flags & FL_USED)) |
| 74 |
break; |
| 75 |
flink->fl_parent = *parents++; |
| 76 |
} |
| 77 |
free (parents_0); |
| 78 |
free (flinks_0); |
| 79 |
idhp->idh_max_link = max_link + 1; |
| 80 |
idhp->idh_file_links = parent_index; |
| 81 |
idhp->idh_files = idhp->idh_member_file_table.ht_fill; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
static int |
| 90 |
file_link_qsort_compare (void const *x, void const *y) |
| 91 |
{ |
| 92 |
struct file_link const *flx = *(struct file_link const *const *) x; |
| 93 |
struct file_link const *fly = *(struct file_link const *const *) y; |
| 94 |
unsigned int x_flags = flx->fl_flags; |
| 95 |
unsigned int y_flags = fly->fl_flags; |
| 96 |
int result; |
| 97 |
|
| 98 |
result = (y_flags & FL_USED) - (x_flags & FL_USED); |
| 99 |
if (result) |
| 100 |
return result; |
| 101 |
if (!(x_flags & FL_USED)) |
| 102 |
return 0; |
| 103 |
result = (y_flags & FL_TYPE_DIR) - (x_flags & FL_TYPE_DIR); |
| 104 |
if (result) |
| 105 |
return result; |
| 106 |
result = (y_flags & FL_TYPE_MASK) - (x_flags & FL_TYPE_MASK); |
| 107 |
if (result) |
| 108 |
return result; |
| 109 |
if (FL_IS_FILE (x_flags)) |
| 110 |
{ |
| 111 |
struct member_file *x_member = find_member_file (flx); |
| 112 |
struct member_file *y_member = find_member_file (fly); |
| 113 |
return x_member->mf_index - y_member->mf_index; |
| 114 |
} |
| 115 |
else |
| 116 |
{ |
| 117 |
int x_depth = links_depth (flx); |
| 118 |
int y_depth = links_depth (fly); |
| 119 |
return (x_depth - y_depth); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
int |
| 127 |
write_idhead (struct idhead *idhp) |
| 128 |
{ |
| 129 |
return io_idhead (idhp->idh_FILE, io_write, idhp); |
| 130 |
} |
| 131 |
|
| 132 |
int |
| 133 |
io_write (FILE *output_FILE, void *addr, unsigned int size, int io_type) |
| 134 |
{ |
| 135 |
if (io_type == IO_TYPE_INT || size == 1) |
| 136 |
{ |
| 137 |
switch (size) |
| 138 |
{ |
| 139 |
case 4: |
| 140 |
putc (*(unsigned long *)addr, output_FILE); |
| 141 |
putc (*(unsigned long *)addr >> 010, output_FILE); |
| 142 |
putc (*(unsigned long *)addr >> 020, output_FILE); |
| 143 |
putc (*(unsigned long *)addr >> 030, output_FILE); |
| 144 |
break; |
| 145 |
case 3: |
| 146 |
putc (*(unsigned long *)addr, output_FILE); |
| 147 |
putc (*(unsigned long *)addr >> 010, output_FILE); |
| 148 |
putc (*(unsigned long *)addr >> 020, output_FILE); |
| 149 |
break; |
| 150 |
case 2: |
| 151 |
putc (*(unsigned short *)addr, output_FILE); |
| 152 |
putc (*(unsigned short *)addr >> 010, output_FILE); |
| 153 |
break; |
| 154 |
case 1: |
| 155 |
putc (*(unsigned char *)addr, output_FILE); |
| 156 |
break; |
| 157 |
default: |
| 158 |
error (EXIT_FAILURE, 0, _("unsupported size in io_write (): %u"), size); |
| 159 |
} |
| 160 |
} |
| 161 |
else if (io_type == IO_TYPE_STR) |
| 162 |
{ |
| 163 |
fputs (addr, output_FILE); |
| 164 |
putc ('\0', output_FILE); |
| 165 |
} |
| 166 |
else if (io_type == IO_TYPE_FIX) |
| 167 |
ignore_value (fwrite (addr, size, 1, output_FILE)); |
| 168 |
else |
| 169 |
error (0, 0, _("unknown I/O type: %d"), io_type); |
| 170 |
return size; |
| 171 |
} |