summaryrefslogtreecommitdiff
path: root/tarlist.c
blob: 22cabc7fd96eb5bd43d713f451c693f39c3ed039 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* tarfs - A GNU tar filesystem for the Hurd.
   Copyright (C) 2002, Ludovic Courtès <ludo@chbouib.org>
 
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or * (at your option) any later version.
 
   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA */

/* Tar list management functions.  This is used as a model representing
   the contents of a tar file, i.e. the items in the order in which they
   appear (or should appear) in the tar file.  */

#include <hurd/netfs.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <error.h>

#include "tarfs.h"
#include "fs.h"
#include "debug.h"


/* Initialize LIST.  */
void
tar_list_init (struct tar_list *list)
{
  list->head = NULL;
  mutex_init (&list->lock);
}

/* Make a tar item containing the given information. NEW points to the
   newly created item.  */
error_t
tar_make_item (struct tar_item **new_item,
	       struct node *node, size_t orig_size, off_t offset)
{
  struct tar_item *new;

  new = calloc (1, sizeof (struct tar_item));
  if (! new)
    return ENOMEM;

  assert (node != NULL);
  new->orig_size = orig_size;
  new->offset = offset;
  new->node   = node;
  NODE_INFO(node)->tar = new;

  *new_item = new;

  return 0;
}

/* Insert tar item NEW right after PREV in LIST.  */
error_t
tar_insert_item (struct tar_list *list,
		 struct tar_item *prev, struct tar_item *new)
{
  struct tar_item **head, *next;

  assert (prev != new);

  mutex_lock (&list->lock);
  head = &list->head;

  if (! prev)
    if (! *head)
    {
      *head = new;
      next  = NULL;
    }
    else
    {
      prev = *head;
      next = (*head)->next;
      (*head)->next = new;
    }
  else
  {
    next = prev->next;
    prev->next = new;
  }

  new->prev = prev;
  new->next = next;
  if (next)
    next->prev = new;

  mutex_unlock (&list->lock);

  return 0;
}

/* Remove ITEM from LIST.  */
void
tar_unlink_item_safe (struct tar_list *list, struct tar_item *item)
{
  struct tar_item **head;

  /* The corresponding node should have been destroyed first.  */
  assert (item->node == NULL);

  head = &list->head;

  /* Make sure LIST is not already empty */
  assert (*head != NULL);

  if (! item->prev)
  {
    *head = item->next;
    if (*head)
      (*head)->prev = NULL;
  }
  else
  {
    item->prev->next = item->next;
    if (item->next)
      item->next->prev = item->prev;
  }

  /* Free ITEM.  */
  free (item);
}

void
tar_unlink_item (struct tar_list *list, struct tar_item *tar)
{
  mutex_lock (&list->lock);
  tar_unlink_item_safe (list, tar);
  mutex_unlock (&list->lock);
}


/* Attempt to find a place for TAR, an new yet unlinked tar item, into the
   tar list in an optimal way.  Returns in PREV_ITEM the item after which
   TAR should be inserted but don't actually insert it.  */
void
tar_put_item (struct tar_item **prev_tar, struct tar_item *tar)
{
  struct node *node = tar->node;
  struct node *dir, *last_entry;

  /* TAR has to be linked to the filesystem.  */
  assert (tar->node);

  dir = node->nn->dir;

  /* Try to insert the node inside the tar list in such a way that it
     appears:
     - after its parent directory;
     - after all the entries of its parent dir;
     - after the last entry of its parent dir's last entry.

     The 1st thing is needed to have a consistent tar archive, and the two
     latter requirements are needed for better performance at sync time
     (it avoids having to sync everything before NEWNODE).

     So that we have, for instance:
       0: file
       1: dir/
       2: dir/file1
       3: dir/file2
       4: NEWNODE.  */

  last_entry = dir->nn->entries;

  if (last_entry)
  {
    /* Get a reference to DIR's last entry.  */
    struct node *next;

    for ( ;
	 (next = last_entry->next) != NULL;
	 last_entry = next)
    {
      if (next == node)
      {
        /* If LAST_ENTRY is TAR's node...  */
        if (node->next)
          /* ... skip it */
          next = node->next;
        else
          /* ... or keep the next-to-last entry */
          break;
      }
    }

    if (last_entry == node)
      last_entry = NULL;

    /* Jump to the last node of LAST_ENTRY's deepest subdir */
    while (last_entry)
    {
      /* If it's a directory, get its last entry.  */
      if ( (S_ISDIR (last_entry->nn_stat.st_mode))
	   && (last_entry->nn->entries) )
      {
	for (last_entry = last_entry->nn->entries;
	     last_entry->next;
	     last_entry = last_entry->next);
      }
      else
        break;
    }
  }

  if ((last_entry) && (last_entry != node))
  {
    assert (NODE_INFO(last_entry)->tar);
    *prev_tar = NODE_INFO(last_entry)->tar;
  }
  else
  {
    if (dir == netfs_root_node)
      *prev_tar = NULL;
    else
      *prev_tar = NODE_INFO(dir)->tar;
  }

  return;
}