File: cache.c

package info (click to toggle)
gurlchecker 0.13-1.1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,724 kB
  • ctags: 1,360
  • sloc: ansic: 16,505; sh: 10,239; makefile: 345
file content (122 lines) | stat: -rw-r--r-- 2,937 bytes parent folder | download | duplicates (3)
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
/*
 * Copyright (C) 2002-2010
 * Emmanuel Saracco <esaracco@users.labs.libre-entreprise.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.
 */

#include "utils.h"

#include "cache.h"


/**
 * uc_cache_change_id:
 * @old: Old #UCLinkProperties node id.
 * @new: New #UCLinkProperties node id.
 *
 * Replace "old" with "new" (swap cached files). Mainly used after rescanning
 * a link.
 */
void
uc_cache_change_id (const guint32 old, const guint32 new)
{
  FILE *fd;
  gchar *buffer = NULL;
  gchar *path = NULL;
  gsize length = 0;


  if ((buffer = uc_cache_get_source (new, &length)) == NULL)
    return;

  path = uc_utils_convert_uid2file (old);
  fd = fopen (path, "w");
  g_free (path), path = NULL;
  g_assert (fd != NULL);

  fwrite (buffer, 1, length, fd);
  fclose (fd);

  path = uc_utils_convert_uid2file (new);
  unlink (path);
  g_free (path), path = NULL;

  g_free (buffer), buffer = NULL;
}


/**
 * uc_cache_get_source:
 * @id: #UCLinkProperties node id of the wanted link content.
 * @length: Set this argument with the size of the returned buffer.
 * 
 * Retreive the source page in cache for a link.
 * 
 * Returns: The source code of the given #UCLinkProperties node id.
 */
gchar *
uc_cache_get_source (const guint32 id, gsize * length)
{
  gchar *buffer = NULL;
  gchar *path = NULL;


  path = uc_utils_convert_uid2file (id);
  g_assert (path != NULL);
  buffer = uc_utils_get_file_content (path, &(*length));
  g_free (path), path = NULL;

  return buffer;
}


/**
 * uc_cache_append_source:
 * @id: #UCLinkProperties node id of the element to put in cache.
 * @src: Content to work with.
 * @length: Lenght of the given buffer.
 * 
 * Put a source page in the cache.
 */
void
uc_cache_append_source (const guint32 id, gchar * src, const gsize length)
{
  FILE *fd = NULL;
  gchar *path = NULL;
  gchar *src_tmp = NULL;


  if (src == NULL || strlen (src) == 0 || 
      (path = uc_utils_convert_uid2file (id)) == NULL)
  {
    g_warning ("No data to cache!");
    return;
  }

  // Pass HTTP header
  src_tmp = uc_utils_search_string_next (src, "\r\n\r\n", '<');

  if (src_tmp != NULL)
  {
    fd = fopen (path, "wb");
    g_assert (fd != NULL);
    fwrite (src_tmp, 1, length, fd);
    fclose (fd);
  }

  g_free (path), path = NULL;
}