Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

core/fileinfo.c File Reference


Detailed Description

Structure for storing meta-information about files being downloaded.

Author:
Vidar Madsen

Raphael Manfredi

Date:
2002-2003

#include "common.h"
#include "fileinfo.h"
#include "file_object.h"
#include "sockets.h"
#include "downloads.h"
#include "uploads.h"
#include "hosts.h"
#include "routing.h"
#include "gmsg.h"
#include "bsched.h"
#include "huge.h"
#include "dmesh.h"
#include "search.h"
#include "guid.h"
#include "share.h"
#include "settings.h"
#include "nodes.h"
#include "namesize.h"
#include "http.h"
#include "lib/atoms.h"
#include "lib/base32.h"
#include "lib/endian.h"
#include "lib/file.h"
#include "lib/header.h"
#include "lib/idtable.h"
#include "lib/magnet.h"
#include "lib/tigertree.h"
#include "lib/tm.h"
#include "lib/url.h"
#include "lib/utf8.h"
#include "lib/walloc.h"
#include "lib/glib-missing.h"
#include "if/gnet_property.h"
#include "if/gnet_property_priv.h"
#include "lib/override.h"

Data Structures

struct  dl_file_chunk
struct  trailer
 The trailer fields of the fileinfo trailer. More...
struct  fi_tag
struct  file_info_foreach

Defines

#define FI_MIN_CHUNK_SPLIT   512
 Max field length we accept to save.
#define FI_MAX_FIELD_LEN   (TTH_RAW_SIZE * TTH_MAX_LEAVES)
#define FILE_INFO_MAGIC32   0xD1BB1ED0U
#define FILE_INFO_MAGIC64   0X91E63640U
#define FILE_INFO_VERSION   6
#define FI_STORE_DELAY   10
 Max delay (secs) for flushing fileinfo.
#define FI_TRAILER_INT   6
 Amount of guint32 in the trailer.
#define TBUF_SIZE   512
 Initial trailing buffer size.
#define TBUF_GROW_BITS   9
 Growing chunks.
#define TBUF_GROW   ((size_t) 1 << TBUF_GROW_BITS)
#define TBUF_GROW_MASK   (TBUF_GROW - 1)
#define BAILOUT(x)
#define GET_KEY(i)   (fi_tag_map[(i)].str)
#define FOUND(i)
#define bs_nop(x)   (x)

Typedefs

typedef guint32 fi_magic_t

Enumerations

enum  dl_file_chunk_magic { DL_FILE_CHUNK_MAGIC = 0xd63b483dU }
enum  dl_file_info_field {
  FILE_INFO_FIELD_NAME = 1, FILE_INFO_FIELD_ALIAS, FILE_INFO_FIELD_SHA1, FILE_INFO_FIELD_CHUNK,
  FILE_INFO_FIELD_END, FILE_INFO_FIELD_CHA1, FILE_INFO_FIELD_GUID, FILE_INFO_FIELD_TTH,
  FILE_INFO_FIELD_TIGERTREE, NUM_FILE_INFO_FIELDS
}
enum  fi_tag_t {
  FI_TAG_UNKNOWN = 0, FI_TAG_ALIA, FI_TAG_CHA1, FI_TAG_CHNK,
  FI_TAG_CTIM, FI_TAG_DONE, FI_TAG_FSKN, FI_TAG_GENR,
  FI_TAG_GUID, FI_TAG_NAME, FI_TAG_NTIM, FI_TAG_PATH,
  FI_TAG_PAUS, FI_TAG_SHA1, FI_TAG_SIZE, FI_TAG_SWRM,
  FI_TAG_TIME, FI_TAG_TTH, NUM_FI_TAGS
}

Functions

size_t round_grow (size_t x)
void tbuf_check (void)
void tbuf_extend (size_t x, gboolean writing)
 Make sure there is enough room in the buffer for `x' more bytes.
void TBUF_INIT_READ (size_t size)
void TBUF_INIT_WRITE (void)
size_t TBUF_WRITTEN_LEN (void)
void TBUF_CHECK (size_t size)
WARN_UNUSED_RESULT gboolean TBUF_GETCHAR (guint8 *x)
WARN_UNUSED_RESULT gboolean TBUF_GET_UINT32 (guint32 *x)
WARN_UNUSED_RESULT gboolean TBUF_READ (gchar *x, size_t size)
void TBUF_PUT_CHAR (guint8 x)
void TBUF_PUT_UINT32 (guint32 x)
void TBUF_WRITE (const gchar *data, size_t size)
void file_info_checksum (guint32 *checksum, gconstpointer data, size_t len)
void WRITE_CHAR (guint8 val, guint32 *checksum)
void WRITE_UINT32 (guint32 val, guint32 *checksum)
void WRITE_STR (const gchar *data, size_t size, guint32 *checksum)
WARN_UNUSED_RESULT gboolean READ_CHAR (guint8 *val, guint32 *checksum)
WARN_UNUSED_RESULT gboolean READ_UINT32 (guint32 *val_ptr, guint32 *checksum)
WARN_UNUSED_RESULT gboolean READ_STR (gchar *data, size_t size, guint32 *checksum)
void FIELD_ADD (enum dl_file_info_field id, size_t n, gconstpointer data, guint32 *checksum)
fileinfo_tfile_info_retrieve_binary (const gchar *pathname)
 Reads the file metainfo from the trailer of a file, if it exists.
void fi_free (fileinfo_t *fi)
 Free a `file_info' structure.
void fi_update_seen_on_network (gnet_src_t srcid)
 Callback for updates to ranges available on the network.
const gchar * file_info_new_outname (const gchar *dir, const gchar *name)
 Allocate unique output name for file `name', stored in `dir'.
gboolean looks_like_urn (const gchar *filename)
 Check whether filename looks like an URN.
fileinfo_tfile_info_find_by_handle (gnet_fi_t n)
gnet_fi_t file_info_request_handle (fileinfo_t *fi)
void file_info_drop_handle (gnet_fi_t n)
gboolean trailer_is_64bit (const struct trailer *tb)
 Checks the kind of trailer.
void tbuf_write (const struct file_object *fo, filesize_t offset)
 Write trailer buffer at current position on `fd', whose name is `name'.
ssize_t tbuf_read (gint fd, size_t len)
 Read trailer buffer at current position from `fd'.
dl_file_chunkdl_file_chunk_alloc (void)
void dl_file_chunk_check (const struct dl_file_chunk *fc)
void dl_file_chunk_free (struct dl_file_chunk **fc_ptr)
gboolean file_info_check_chunklist (const fileinfo_t *fi, gboolean assertion)
 Checks the chunklist of fi.
void file_info_fd_store_binary (fileinfo_t *fi, const struct file_object *fo, gboolean force)
 Store a binary record of the file metainformation at the end of the supplied file descriptor, opened for writing.
void file_info_store_binary (fileinfo_t *fi)
 Store a binary record of the file metainformation at the end of the output file, if it exists.
void file_info_mark_stripped (fileinfo_t *fi)
 Record that the fileinfo trailer has been stripped.
void file_info_strip_trailer (fileinfo_t *fi, const gchar *pathname)
void file_info_strip_binary (fileinfo_t *fi)
 Strips the file metainfo trailer off a file.
void file_info_strip_binary_from_file (fileinfo_t *fi, const gchar *pathname)
 Strips the file metainfo trailer off specified file.
void file_info_chunklist_free (fileinfo_t *fi)
 Frees the chunklist and all its elements of a fileinfo struct.
void fi_tigertree_free (fileinfo_t *fi)
void file_info_hash_insert_name_size (fileinfo_t *fi)
void fi_resize (fileinfo_t *fi, filesize_t size)
 Resize fileinfo to be `size' bytes, by adding empty chunk at the tail.
void fi_alias (fileinfo_t *fi, const gchar *name, gboolean record)
 Add `name' as an alias for `fi' if not already known.
gboolean file_info_get_trailer (gint fd, struct trailer *tb, const gchar *name)
 Extract fixed trailer at the end of the file `name', already opened as `fd'.
gint file_info_has_trailer (const gchar *path)
 Check whether file has a trailer.
fileinfo_tfile_info_by_sha1 (const struct sha1 *sha1)
fileinfo_tfile_info_lookup (const gchar *name, filesize_t size, const struct sha1 *sha1)
 Lookup our existing fileinfo structs to see if we can spot one referencing the supplied file `name' and `size', as well as the optional `sha1' hash.
fileinfo_tfile_info_lookup_dup (fileinfo_t *fi)
 Given a fileinfo structure, look for any other known duplicate.
const gchar * file_info_readable_filename (const fileinfo_t *fi)
 Determines a human-readable filename for the file, using heuristics to skip what looks like an URN.
shared_file_tfile_info_shared_sha1 (const struct sha1 *sha1)
 Look whether we have a partially downloaded file bearing the given SHA1.
const gchar * fi_random_guid_atom (void)
 Allocate random GUID to use as the file ID.
gboolean fi_upgrade_older_version (fileinfo_t *fi)
 Ensure potentially old fileinfo structure is brought up-to-date by inferring or allocating missing fields.
void fi_tigertree_check (fileinfo_t *fi)
void file_info_store_one (FILE *f, fileinfo_t *fi)
 Stores a file info record to the config_dir/fileinfo file, and appends it to the output file in question if needed.
void file_info_store_list (gpointer key, gpointer value, gpointer user_data)
 Callback for hash table iterator.
void file_info_store (void)
 Stores the list of output files and their metainfo to the configdir/fileinfo database.
void file_info_store_if_dirty (void)
 Store global file information cache if dirty.
void fi_dispose (fileinfo_t *fi)
void file_info_free_sha1_kv (gpointer key, gpointer val, gpointer unused_x)
 Callback for hash table iterator.
void file_info_free_namesize_kv (gpointer key, gpointer val, gpointer unused_x)
 Callback for hash table iterator.
void file_info_free_guid_kv (gpointer key, gpointer val, gpointer unused_x)
 Callback for hash table iterator.
void file_info_free_outname_kv (gpointer key, gpointer val, gpointer unused_x)
 Callback for hash table iterator.
void file_info_changed (fileinfo_t *fi)
 Signals that some information in the fileinfo has changed, warranting a display update in the GUI.
void file_info_close_pre (void)
 Pre-close some file_info information.
void file_info_close (void)
 Close and free all file_info structs in the list.
void file_info_hash_insert (fileinfo_t *fi)
 Inserts a file_info struct into the hash tables.
void file_info_hash_remove (fileinfo_t *fi)
 Remove fileinfo data from all the hash tables.
void file_info_upload_stop (fileinfo_t *fi, const gchar *reason)
 Stop all sharing occuring for this fileinfo.
void file_info_unlink (fileinfo_t *fi)
 Unlink file from disk.
void file_info_reparent_all (fileinfo_t *from, fileinfo_t *to)
 Reparent all downloads using `from' as a fileinfo, so they use `to' now.
gboolean file_info_got_sha1 (fileinfo_t *fi, const struct sha1 *sha1)
 Called when we discover the SHA1 of a running download.
void file_info_got_tth (fileinfo_t *fi, const struct tth *tth)
void file_info_got_tigertree (fileinfo_t *fi, const struct tth *leaves, size_t num_leaves)
const gchar * extract_guid (const gchar *s)
 Extract GUID from GUID line in the ASCII "fileinfo" summary file and return NULL if none or invalid, the GUID atom otherwise.
const struct sha1extract_sha1 (const gchar *s)
 Extract sha1 from SHA1/CHA1 line in the ASCII "fileinfo" summary file and return NULL if none or invalid, the SHA1 atom otherwise.
const struct tthextract_tth (const gchar *s)
fi_tag_t file_info_string_to_tag (const gchar *s)
 Transform fileinfo tag string into tag constant.
void fi_reset_chunks (fileinfo_t *fi)
 Reset CHUNK info: everything will have to be downloaded again.
void fi_copy_chunks (fileinfo_t *fi, fileinfo_t *trailer)
 Copy CHUNK info from binary trailer `trailer' into `fi'.
void file_info_retrieve (void)
 Loads the fileinfo database from disk, and saves a copy in fileinfo.orig.
gboolean file_info_name_is_uniq (const gchar *pathname)
fileinfo_tfile_info_create (const gchar *file, const gchar *path, filesize_t size, const struct sha1 *sha1, gboolean file_size_known)
 Create a fileinfo structure from existing file with no swarming trailer.
fileinfo_tfile_info_get_transient (const gchar *name)
 Create a transient fileinfo structure.
void fi_rename_dead (fileinfo_t *fi, const gchar *pathname)
 Rename dead file we cannot use, either because it bears a duplicate SHA1 or because its file trailer bears a duplicate file ID.
void file_info_moved (fileinfo_t *fi, const gchar *pathname)
 Called to update the fileinfo information with the new path and possibly filename information, once the downloaded file has been moved/renamed.
fileinfo_tfile_info_get (const gchar *file, const gchar *path, filesize_t size, const struct sha1 *sha1, gboolean file_size_known)
fileinfo_tfile_info_has_identical (const struct sha1 *sha1, filesize_t size)
void file_info_set_discard (fileinfo_t *fi, gboolean state)
 Set or clear the discard state for a fileinfo.
void file_info_merge_adjacent (fileinfo_t *fi)
 Go through the chunk list and merge adjacent chunks that share the same status and download.
void file_info_size_known (struct download *d, filesize_t size)
 Signals that file size became known suddenly.
void file_info_update (struct download *d, filesize_t from, filesize_t to, enum dl_chunk_status status)
 Marks a chunk of the file with given status.
void file_info_clear_download (struct download *d, gboolean lifecount)
 Go through all chunks that belong to the download, and unmark them as busy.
void file_info_reset (fileinfo_t *fi)
 Reset all chunks to EMPTY, clear computed SHA1 if any.
enum dl_chunk_status file_info_chunk_status (fileinfo_t *fi, filesize_t from, filesize_t to)
enum dl_chunk_status file_info_pos_status (fileinfo_t *fi, filesize_t pos)
void fi_check_file (fileinfo_t *fi)
 This routine is called each time we start a new download, before making the request to the remote server.
gint fi_busy_count (fileinfo_t *fi, struct download *d)
 Count the amount of BUSY chunks attached to a given download.
filesize_t get_random_offset (const filesize_t size)
GSList * list_clone_shift (fileinfo_t *fi)
 Clone fileinfo's chunk list, shifting the origin of the list to a randomly selected offset within the file.
filesize_t fi_chunksize (fileinfo_t *fi)
 Compute chunksize to be used for the current request.
gdouble fi_missing_coverage (struct download *d)
 Compute how much the source covers the missing chunks we still have which are not busy.
const struct dl_file_chunkfi_find_largest (const fileinfo_t *fi)
 Find the largest busy chunk.
const struct dl_file_chunkfi_find_slowest (const fileinfo_t *fi)
 Find the largest busy chunk served by the host with the smallest uploading rate.
gboolean fi_find_aggressive_candidate (struct download *d, guint busy, filesize_t *from, filesize_t *to)
 Find the spot we could download at the tail of an already active chunk to be aggressively completing the file ASAP.
enum dl_chunk_status file_info_find_hole (struct download *d, filesize_t *from, filesize_t *to)
 Finds a range to download, and stores it in *from and *to.
gboolean file_info_find_available_hole (struct download *d, GSList *ranges, filesize_t *from, filesize_t *to)
 Find free chunk that also fully belongs to the `ranges' list.
void file_info_try_to_swarm_with (const gchar *file_name, const host_addr_t addr, guint16 port, const struct sha1 *sha1)
 Called when we add something to the dmesh.
void file_info_scandir (const gchar *dir)
 Scan the given directory for files, looking at those bearing a valid fileinfo trailer, yet which we know nothing about.
void fi_spot_completed_kv (gpointer key, gpointer val, gpointer unused_x)
 Callback for hash table iterator.
void file_info_spot_completed_orphans (void)
 Look through all the known fileinfo structures, looking for orphaned files that are complete.
void fi_add_listener (fi_listener_t cb, gnet_fi_ev_t ev, frequency_t t, guint32 interval)
void fi_remove_listener (fi_listener_t cb, gnet_fi_ev_t ev)
gnet_fi_info_tfi_get_info (gnet_fi_t fih)
 Get an information structure summarizing the file info.
void fi_free_info (gnet_fi_info_t *info)
 Dispose of the info structure.
void fi_increase_uploaded (fileinfo_t *fi, size_t amount)
void fi_get_status (gnet_fi_t fih, gnet_fi_status_t *s)
 Fill in the fileinfo status structure "s" using the fileinfo associated with the fileinfo handle "fih".
GSList * fi_get_chunks (gnet_fi_t fih)
 Get a list with information about each chunk and status.
void fi_free_chunks (GSList *chunks)
 Free chunk list got by calling fi_get_chunks.
GSList * fi_get_ranges (gnet_fi_t fih)
 Get a list of available ranges for this fileinfo handle.
void fi_free_ranges (GSList *ranges)
gchar ** fi_get_aliases (gnet_fi_t fih)
void file_info_add_new_source (fileinfo_t *fi, struct download *dl)
 Add new download source for the file.
void file_info_add_source (fileinfo_t *fi, struct download *dl)
 Add download source for the file, but preserve original "ntime".
void file_info_remove_source (fileinfo_t *fi, struct download *dl, gboolean discard)
 Removing one source reference from the fileinfo.
void file_info_remove (fileinfo_t *fi)
 Remove non-referenced fileinfo and reclaim its data structures.
void fi_notify_helper (gpointer unused_key, gpointer value, gpointer unused_udata)
void file_info_timer (void)
gboolean file_info_purge (fileinfo_t *fi)
 Kill all downloads associated with a fi and remove the fi itself.
gboolean fi_purge (gnet_fi_t fih)
void fi_purge_by_handle_list (const GSList *list)
 Purge all handles contained in list.
gint file_info_available_ranges (fileinfo_t *fi, gchar *buf, gint size)
 Emit an X-Available-Ranges header listing the ranges within the file that we have on disk and we can share as a PFSP-server.
gboolean file_info_restrict_range (fileinfo_t *fi, filesize_t start, filesize_t *end)
 Given a request range `start' (included) and `end' (included) for the partially downloaded file represented by `fi', see whether we can satisfy it, even partially, without touching `start' but only only by possibly moving `end' down.
gchar * file_info_build_magnet (gnet_fi_t handle)
 Creates a URL which points to a downloads (e.g.
gchar * file_info_get_file_url (gnet_fi_t handle)
 Creates a file:// URL which points to the file on the local filesystem.
GSList * fi_range_for_complete_file (filesize_t size)
 Create a ranges list with one item covering the whole file.
void file_info_foreach_helper (gpointer unused_key, gpointer value, gpointer udata)
void file_info_foreach (file_info_foreach_cb callback, gpointer udata)
const char * file_info_status_to_string (const gnet_fi_status_t *status)
void file_info_init (void)
 Initialize fileinfo handling.
void file_info_init_post (void)
 Finish initialization of fileinfo handling.

Variables

GHashTable * fi_by_sha1
GHashTable * fi_by_namesize
GHashTable * fi_by_outname
GHashTable * fi_by_guid
const gchar file_info_file [] = "fileinfo"
const gchar file_info_what [] = "the fileinfo database"
gboolean fileinfo_dirty = FALSE
gboolean can_swarm = FALSE
 Set by file_info_retrieve().
struct {
   gchar *   arena
 Base arena.
   gchar *   wptr
 Write pointer.
   const gchar *   rptr
 Read pointer.
   const gchar *   end
 First byte off arena.
   size_t   size
 Current size of arena.
tbuf
 The swarming trailer is built within a memory buffer first, to avoid having to issue mutliple write() system calls.
idtable_tfi_handle_map = NULL
event_tfi_events [EV_FI_EVENTS]
const struct fi_tag fi_tag_map []


Define Documentation

#define BAILOUT  ) 
 

Value:

G_STMT_START {              \
    reason = (x);           \
    goto bailout;           \
    /* NOTREACHED */        \
} G_STMT_END

#define bs_nop  )     (x)
 

#define FI_MAX_FIELD_LEN   (TTH_RAW_SIZE * TTH_MAX_LEAVES)
 

#define FI_MIN_CHUNK_SPLIT   512
 

Max field length we accept to save.

#define FI_STORE_DELAY   10
 

Max delay (secs) for flushing fileinfo.

#define FI_TRAILER_INT   6
 

Amount of guint32 in the trailer.

#define FILE_INFO_MAGIC32   0xD1BB1ED0U
 

#define FILE_INFO_MAGIC64   0X91E63640U
 

#define FILE_INFO_VERSION   6
 

#define FOUND  ) 
 

Value:

G_STMT_START { \
    return fi_tag_map[(i)].tag; \
    /* NOTREACHED */ \
} G_STMT_END

#define GET_KEY  )     (fi_tag_map[(i)].str)
 

#define TBUF_GROW   ((size_t) 1 << TBUF_GROW_BITS)
 

#define TBUF_GROW_BITS   9
 

Growing chunks.

#define TBUF_GROW_MASK   (TBUF_GROW - 1)
 

#define TBUF_SIZE   512
 

Initial trailing buffer size.


Typedef Documentation

typedef guint32 fi_magic_t
 


Enumeration Type Documentation

enum dl_file_chunk_magic
 

Enumeration values:
DL_FILE_CHUNK_MAGIC 

enum dl_file_info_field
 

Enumeration values:
FILE_INFO_FIELD_NAME  No longer used in 32-bit version >= 3.
FILE_INFO_FIELD_ALIAS 
FILE_INFO_FIELD_SHA1 
FILE_INFO_FIELD_CHUNK 
FILE_INFO_FIELD_END  Marks end of field section.
FILE_INFO_FIELD_CHA1 
FILE_INFO_FIELD_GUID 
FILE_INFO_FIELD_TTH 
FILE_INFO_FIELD_TIGERTREE 
NUM_FILE_INFO_FIELDS 

enum fi_tag_t
 

Enumeration values:
FI_TAG_UNKNOWN 
FI_TAG_ALIA 
FI_TAG_CHA1 
FI_TAG_CHNK 
FI_TAG_CTIM 
FI_TAG_DONE 
FI_TAG_FSKN 
FI_TAG_GENR 
FI_TAG_GUID 
FI_TAG_NAME 
FI_TAG_NTIM 
FI_TAG_PATH 
FI_TAG_PAUS 
FI_TAG_SHA1 
FI_TAG_SIZE 
FI_TAG_SWRM 
FI_TAG_TIME 
FI_TAG_TTH 
NUM_FI_TAGS 


Function Documentation

struct dl_file_chunk* dl_file_chunk_alloc void   )  [static]
 

void dl_file_chunk_check const struct dl_file_chunk fc  )  [static]
 

void dl_file_chunk_free struct dl_file_chunk **  fc_ptr  )  [static]
 

const gchar* extract_guid const gchar *  s  )  [static]
 

Extract GUID from GUID line in the ASCII "fileinfo" summary file and return NULL if none or invalid, the GUID atom otherwise.

const struct sha1* extract_sha1 const gchar *  s  )  [static]
 

Extract sha1 from SHA1/CHA1 line in the ASCII "fileinfo" summary file and return NULL if none or invalid, the SHA1 atom otherwise.

const struct tth* extract_tth const gchar *  s  )  [static]
 

void fi_add_listener fi_listener_t  cb,
gnet_fi_ev_t  ev,
frequency_t  t,
guint32  interval
 

void fi_alias fileinfo_t fi,
const gchar *  name,
gboolean  record
[static]
 

Add `name' as an alias for `fi' if not already known.

If `record' is TRUE, also record new alias entry in `fi_by_namesize'.

gint fi_busy_count fileinfo_t fi,
struct download d
[static]
 

Count the amount of BUSY chunks attached to a given download.

void fi_check_file fileinfo_t fi  )  [static]
 

This routine is called each time we start a new download, before making the request to the remote server.

If we detect that the file is "gone", then it means the user manually deleted the file. In that case, we need to reset all the chunks and mark the whole thing as being EMPTY. --RAM, 21/08/2002.

filesize_t fi_chunksize fileinfo_t fi  )  [static]
 

Compute chunksize to be used for the current request.

void fi_copy_chunks fileinfo_t fi,
fileinfo_t trailer
[static]
 

Copy CHUNK info from binary trailer `trailer' into `fi'.

void fi_dispose fileinfo_t fi  )  [static]
 

gboolean fi_find_aggressive_candidate struct download d,
guint  busy,
filesize_t from,
filesize_t to
[static]
 

Find the spot we could download at the tail of an already active chunk to be aggressively completing the file ASAP.

Parameters:
d the download source we want to consider making a request to
busy the amount of known busy chunks in the file
from where the start of the possible chunk request will be written
to where the end of the possible chunk request will be written
Returns:
TRUE if we were able to find a candidate, with `from' and `to' being filled with the chunk we could be requesting.

const struct dl_file_chunk* fi_find_largest const fileinfo_t fi  )  [static]
 

Find the largest busy chunk.

Returns:
largest chunk found in the fileinfo, or NULL if there are no busy chunks.

const struct dl_file_chunk* fi_find_slowest const fileinfo_t fi  )  [static]
 

Find the largest busy chunk served by the host with the smallest uploading rate.

Returns:
chunk found in the fileinfo, or NULL if there are no busy chunks.

void fi_free fileinfo_t fi  )  [static]
 

Free a `file_info' structure.

void fi_free_chunks GSList *  chunks  ) 
 

Free chunk list got by calling fi_get_chunks.

void fi_free_info gnet_fi_info_t info  ) 
 

Dispose of the info structure.

void fi_free_ranges GSList *  ranges  ) 
 

gchar** fi_get_aliases gnet_fi_t  fih  ) 
 

Returns:
NULL terminated array of gchar * pointing to the aliases. You can easily free the returned array with g_strfreev().
O(2n) - n: number of aliases

GSList* fi_get_chunks gnet_fi_t  fih  ) 
 

Get a list with information about each chunk and status.

Returns a linked list of chunks with just the end byte and the status. The list is fully allocated and the receiver is responsible for freeing up the memory.

gnet_fi_info_t* fi_get_info gnet_fi_t  fih  ) 
 

Get an information structure summarizing the file info.

This is used by the GUI to avoid peeking into the file info structure directly: it has its own little pre-digested information to display.

GSList* fi_get_ranges gnet_fi_t  fih  ) 
 

Get a list of available ranges for this fileinfo handle.

The list is fully allocated and the receiver is responsible for freeing up the memory, for example using fi_free_ranges().

void fi_get_status gnet_fi_t  fih,
gnet_fi_status_t s
 

Fill in the fileinfo status structure "s" using the fileinfo associated with the fileinfo handle "fih".

void fi_increase_uploaded fileinfo_t fi,
size_t  amount
 

gdouble fi_missing_coverage struct download d  )  [static]
 

Compute how much the source covers the missing chunks we still have which are not busy.

This is expressed as a percentage of those missing chunks.

void fi_notify_helper gpointer  unused_key,
gpointer  value,
gpointer  unused_udata
[static]
 

gboolean fi_purge gnet_fi_t  fih  )  [static]
 

void fi_purge_by_handle_list const GSList *  list  ) 
 

Purge all handles contained in list.

const gchar* fi_random_guid_atom void   )  [static]
 

Allocate random GUID to use as the file ID.

Returns:
a GUID atom, refcount incremented already.

GSList* fi_range_for_complete_file filesize_t  size  )  [static]
 

Create a ranges list with one item covering the whole file.

This may be better placed in http.c, but since it is only used here as a utility function for fi_update_seen_on_network it is now placed here.

Parameters:
[in] size File size to be used in range creation

void fi_remove_listener fi_listener_t  cb,
gnet_fi_ev_t  ev
 

void fi_rename_dead fileinfo_t fi,
const gchar *  pathname
[static]
 

Rename dead file we cannot use, either because it bears a duplicate SHA1 or because its file trailer bears a duplicate file ID.

The file is really dead, so unfortunately we have to strip its fileinfo trailer so that we do not try to reparent it at a later time.

void fi_reset_chunks fileinfo_t fi  )  [static]
 

Reset CHUNK info: everything will have to be downloaded again.

void fi_resize fileinfo_t fi,
filesize_t  size
[static]
 

Resize fileinfo to be `size' bytes, by adding empty chunk at the tail.

void fi_spot_completed_kv gpointer  key,
gpointer  val,
gpointer  unused_x
[static]
 

Callback for hash table iterator.

Used by file_info_completed_orphans().

void fi_tigertree_check fileinfo_t fi  )  [static]
 

void fi_tigertree_free fileinfo_t fi  )  [static]
 

void fi_update_seen_on_network gnet_src_t  srcid  )  [static]
 

Callback for updates to ranges available on the network.

This function gets triggered by an event when new ranges information has become available for a download source. We collect the set of currently available ranges in file_info->seen_on_network. Currently we only fold in new ranges from a download source, but we should also remove sets of ranges when a download source is no longer available.

Parameters:
[in] srcid The abstract id of the source that had its ranges updated.

Bug:
FIXME: also remove ranges when a download source is no longer available.

gboolean fi_upgrade_older_version fileinfo_t fi  )  [static]
 

Ensure potentially old fileinfo structure is brought up-to-date by inferring or allocating missing fields.

Returns:
TRUE if an upgrade was necessary.

void FIELD_ADD enum dl_file_info_field  id,
size_t  n,
gconstpointer  data,
guint32 *  checksum
[static]
 

void file_info_add_new_source fileinfo_t fi,
struct download dl
 

Add new download source for the file.

void file_info_add_source fileinfo_t fi,
struct download dl
 

Add download source for the file, but preserve original "ntime".

gint file_info_available_ranges fileinfo_t fi,
gchar *  buf,
gint  size
 

Emit an X-Available-Ranges header listing the ranges within the file that we have on disk and we can share as a PFSP-server.

The header is emitted in `buf', which is `size' bytes long.

If there is not enough room to emit all the ranges, emit a random subset of the ranges.

Returns:
the size of the generated header.

gchar* file_info_build_magnet gnet_fi_t  handle  ) 
 

Creates a URL which points to a downloads (e.g.

you can move this to a browser and download the file there with this URL).

Returns:
A newly allocated string.

fileinfo_t* file_info_by_sha1 const struct sha1 sha1  ) 
 

void file_info_changed fileinfo_t fi  ) 
 

Signals that some information in the fileinfo has changed, warranting a display update in the GUI.

gboolean file_info_check_chunklist const fileinfo_t fi,
gboolean  assertion
[static]
 

Checks the chunklist of fi.

Parameters:
fi the fileinfo struct to check.
assertion no document
Returns:
TRUE if chunklist is consistent, FALSE otherwise.

void file_info_checksum guint32 *  checksum,
gconstpointer  data,
size_t  len
[inline, static]
 

enum dl_chunk_status file_info_chunk_status fileinfo_t fi,
filesize_t  from,
filesize_t  to
 

Returns:
DONE if the range requested is marked as complete, or BUSY if not. Used to determine if we can do overlap checking.

void file_info_chunklist_free fileinfo_t fi  )  [static]
 

Frees the chunklist and all its elements of a fileinfo struct.

Note that the consistency of the list isn't checked to explicitely allow freeing inconsistent chunklists.

Parameters:
fi the fileinfo struct.

void file_info_clear_download struct download d,
gboolean  lifecount
 

Go through all chunks that belong to the download, and unmark them as busy.

If `lifecount' is TRUE, the download is still counted as being "alive", and this is only used for assertions.

< For assertions only

void file_info_close void   ) 
 

Close and free all file_info structs in the list.

void file_info_close_pre void   ) 
 

Pre-close some file_info information.

This should be separate from file_info_close so that we can avoid circular dependencies with other close routines, in this case with download_close.

fileinfo_t* file_info_create const gchar *  file,
const gchar *  path,
filesize_t  size,
const struct sha1 sha1,
gboolean  file_size_known
[static]
 

Create a fileinfo structure from existing file with no swarming trailer.

The given `size' argument reflect the final size of the (complete) file. The `sha1' is the known SHA1 for the file (NULL if unknown).

void file_info_drop_handle gnet_fi_t  n  )  [inline, static]
 

void file_info_fd_store_binary fileinfo_t fi,
const struct file_object fo,
gboolean  force
[static]
 

Store a binary record of the file metainformation at the end of the supplied file descriptor, opened for writing.

When `force' is false, we don't store unless FI_STORE_DELAY seconds have elapsed since last flush to disk.

gboolean file_info_find_available_hole struct download d,
GSList *  ranges,
filesize_t from,
filesize_t to
 

Find free chunk that also fully belongs to the `ranges' list.

If found, the returned chunk is marked BUSY and linked to the download `d'.

Returns:
TRUE if one was found, with `from' and `to' set, FALSE otherwise.
Attention:
NB: In accordance with other fileinfo semantics, `to' is NOT the last byte of the range but one byte AFTER the end.

fileinfo_t* file_info_find_by_handle gnet_fi_t  n  )  [inline, static]
 

enum dl_chunk_status file_info_find_hole struct download d,
filesize_t from,
filesize_t to
 

Finds a range to download, and stores it in *from and *to.

If "aggressive" is off, it will return only ranges that are EMPTY. If on, and no EMPTY ranges are available, it will grab a chunk out of the longest BUSY chunk instead, and "compete" with the download that reserved it.

void file_info_foreach file_info_foreach_cb  callback,
gpointer  udata
 

void file_info_foreach_helper gpointer  unused_key,
gpointer  value,
gpointer  udata
[static]
 

void file_info_free_guid_kv gpointer  key,
gpointer  val,
gpointer  unused_x
[static]
 

Callback for hash table iterator.

Used by file_info_close().

void file_info_free_namesize_kv gpointer  key,
gpointer  val,
gpointer  unused_x
[static]
 

Callback for hash table iterator.

Used by file_info_close().

void file_info_free_outname_kv gpointer  key,
gpointer  val,
gpointer  unused_x
[static]
 

Callback for hash table iterator.

Used by file_info_close().

void file_info_free_sha1_kv gpointer  key,
gpointer  val,
gpointer  unused_x
[static]
 

Callback for hash table iterator.

Used by file_info_close().

fileinfo_t* file_info_get const gchar *  file,
const gchar *  path,
filesize_t  size,
const struct sha1 sha1,
gboolean  file_size_known
 

Parameters:
`file' is the file name on the server.
`path' no brief description.
`size' no brief description.
`sha1' no brief description.
`file_size_known' no brief description.
Returns:
a pointer to file_info struct that matches the given file name, size and/or SHA1. A new struct will be allocated if necessary.

gchar* file_info_get_file_url gnet_fi_t  handle  ) 
 

Creates a file:// URL which points to the file on the local filesystem.

If the file has not been created yet, NULL is returned.

Returns:
A newly allocated string or NULL.

gboolean file_info_get_trailer gint  fd,
struct trailer tb,
const gchar *  name
[static]
 

Extract fixed trailer at the end of the file `name', already opened as `fd'.

The supplied trailer buffer `tb' is filled.

Returns:
TRUE if the trailer is "validated", FALSE otherwise.

fileinfo_t* file_info_get_transient const gchar *  name  ) 
 

Create a transient fileinfo structure.

gboolean file_info_got_sha1 fileinfo_t fi,
const struct sha1 sha1
 

Called when we discover the SHA1 of a running download.

Make sure there is no other entry already bearing that SHA1, and record the information.

Returns:
TRUE if OK, FALSE if a duplicate record with the same SHA1 exists.

void file_info_got_tigertree fileinfo_t fi,
const struct tth leaves,
size_t  num_leaves
 

void file_info_got_tth fileinfo_t fi,
const struct tth tth
 

fileinfo_t* file_info_has_identical const struct sha1 sha1,
filesize_t  size
 

Returns:
a pointer to the file info struct if we have a file identical to the given properties in the download queue already, and NULL otherwise.

gint file_info_has_trailer const gchar *  path  ) 
 

Check whether file has a trailer.

Returns:
0 if the file has no trailer 1 if the file has a trailer -1 on error.

void file_info_hash_insert fileinfo_t fi  )  [static]
 

Inserts a file_info struct into the hash tables.

void file_info_hash_insert_name_size fileinfo_t fi  )  [static]
 

void file_info_hash_remove fileinfo_t fi  )  [static]
 

Remove fileinfo data from all the hash tables.

void file_info_init void   ) 
 

Initialize fileinfo handling.

void file_info_init_post void   ) 
 

Finish initialization of fileinfo handling.

This post initialization is needed to avoid circular dependencies during the init phase. The listener we set up here is set up in download_init, but that must be called after file_info_init.

fileinfo_t* file_info_lookup const gchar *  name,
filesize_t  size,
const struct sha1 sha1
[static]
 

Lookup our existing fileinfo structs to see if we can spot one referencing the supplied file `name' and `size', as well as the optional `sha1' hash.

Returns:
the fileinfo structure if found, NULL otherwise.

fileinfo_t* file_info_lookup_dup fileinfo_t fi  )  [static]
 

Given a fileinfo structure, look for any other known duplicate.

Returns:
the duplicate found, or NULL if no duplicate was found.

void file_info_mark_stripped fileinfo_t fi  ) 
 

Record that the fileinfo trailer has been stripped.

void file_info_merge_adjacent fileinfo_t fi  ) 
 

Go through the chunk list and merge adjacent chunks that share the same status and download.

Keeps the chunk list short and tidy.

void file_info_moved fileinfo_t fi,
const gchar *  pathname
 

Called to update the fileinfo information with the new path and possibly filename information, once the downloaded file has been moved/renamed.

This prepares for possible seeding of the file once it has been completed, to continue "partial-file-sharing" it now that it is fully available...

gboolean file_info_name_is_uniq const gchar *  pathname  )  [static]
 

const gchar * file_info_new_outname const gchar *  dir,
const gchar *  name
[static]
 

Allocate unique output name for file `name', stored in `dir'.

Returns:
The full pathname (string atom).

enum dl_chunk_status file_info_pos_status fileinfo_t fi,
filesize_t  pos
 

Returns:
the status (EMPTY, BUSY or DONE) of the byte requested. Used to detect if a download is crashing with another.

gboolean file_info_purge fileinfo_t fi  ) 
 

Kill all downloads associated with a fi and remove the fi itself.

Will return FALSE if download could not be removed because it was still in use, e.g. when it is being verified. -- JA 25/10/03

const gchar* file_info_readable_filename const fileinfo_t fi  ) 
 

Determines a human-readable filename for the file, using heuristics to skip what looks like an URN.

Returns:
a pointer to the information in the fileinfo, but this must be duplicated should it be perused later.

void file_info_remove fileinfo_t fi  ) 
 

Remove non-referenced fileinfo and reclaim its data structures.

void file_info_remove_source fileinfo_t fi,
struct download dl,
gboolean  discard
 

Removing one source reference from the fileinfo.

When no sources reference the fileinfo structure, free it if `discard' is TRUE, or if the fileinfo has been marked with FI_F_DISCARD. This replaces file_info_free()

void file_info_reparent_all fileinfo_t from,
fileinfo_t to
[static]
 

Reparent all downloads using `from' as a fileinfo, so they use `to' now.

gnet_fi_t file_info_request_handle fileinfo_t fi  )  [inline, static]
 

void file_info_reset fileinfo_t fi  ) 
 

Reset all chunks to EMPTY, clear computed SHA1 if any.

gboolean file_info_restrict_range fileinfo_t fi,
filesize_t  start,
filesize_t end
 

Given a request range `start' (included) and `end' (included) for the partially downloaded file represented by `fi', see whether we can satisfy it, even partially, without touching `start' but only only by possibly moving `end' down.

Returns:
TRUE if the request is satisfiable, with `end' possibly adjusted, FALSE is the request cannot be satisfied because `start' is not within an available chunk.

void file_info_retrieve void   ) 
 

Loads the fileinfo database from disk, and saves a copy in fileinfo.orig.

fileinfo_t * file_info_retrieve_binary const gchar *  pathname  )  [static]
 

Reads the file metainfo from the trailer of a file, if it exists.

Returns:
a pointer to the info structure if found, and NULL otherwise.

void file_info_scandir const gchar *  dir  ) 
 

Scan the given directory for files, looking at those bearing a valid fileinfo trailer, yet which we know nothing about.

void file_info_set_discard fileinfo_t fi,
gboolean  state
 

Set or clear the discard state for a fileinfo.

shared_file_t* file_info_shared_sha1 const struct sha1 sha1  ) 
 

Look whether we have a partially downloaded file bearing the given SHA1.

If we do, return a "shared_file" structure suitable for uploading the parts of the file we have (will happen only when PFSP-server is enabled).

Returns:
NULL if don't have any download with this SHA1, otherwise return a "shared_file" structure suitable for uploading the parts of the file we have (which will happen only when PFSP-server is enabled).

void file_info_size_known struct download d,
filesize_t  size
 

Signals that file size became known suddenly.

The download becomes the owner of the "busy" part between what we have done and the end of the file.

void file_info_spot_completed_orphans void   ) 
 

Look through all the known fileinfo structures, looking for orphaned files that are complete.

A fake download is created for them, so that download_resume_bg_tasks() can pick them up.

const char* file_info_status_to_string const gnet_fi_status_t status  ) 
 

void file_info_store void   ) 
 

Stores the list of output files and their metainfo to the configdir/fileinfo database.

void file_info_store_binary fileinfo_t fi  ) 
 

Store a binary record of the file metainformation at the end of the output file, if it exists.

void file_info_store_if_dirty void   ) 
 

Store global file information cache if dirty.

void file_info_store_list gpointer  key,
gpointer  value,
gpointer  user_data
[static]
 

Callback for hash table iterator.

Used by file_info_store().

void file_info_store_one FILE *  f,
fileinfo_t fi
[static]
 

Stores a file info record to the config_dir/fileinfo file, and appends it to the output file in question if needed.

fi_tag_t file_info_string_to_tag const gchar *  s  )  [static]
 

Transform fileinfo tag string into tag constant.

For instance, "TIME" would yield FI_TAG_TIME. An unknown tag yieldd FI_TAG_UNKNOWN.

void file_info_strip_binary fileinfo_t fi  ) 
 

Strips the file metainfo trailer off a file.

void file_info_strip_binary_from_file fileinfo_t fi,
const gchar *  pathname
 

Strips the file metainfo trailer off specified file.

void file_info_strip_trailer fileinfo_t fi,
const gchar *  pathname
[static]
 

void file_info_timer void   ) 
 

void file_info_try_to_swarm_with const gchar *  file_name,
const host_addr_t  addr,
guint16  port,
const struct sha1 sha1
 

Called when we add something to the dmesh.

Add the corresponding file to the download list if we're swarming on it.

Parameters:
file_name the remote file name (as in the GET query).
idx the remote file index (as in the GET query).
addr the remote servent address.
port the remote servent port.
sha1 the SHA1 of the file.

void file_info_unlink fileinfo_t fi  ) 
 

Unlink file from disk.

void file_info_update struct download d,
filesize_t  from,
filesize_t  to,
enum dl_chunk_status  status
 

Marks a chunk of the file with given status.

The bytes range from `from' (included) to `to' (excluded).

When not marking the chunk as EMPTY, the range is linked to the supplied download `d' so we know who "owns" it currently.

void file_info_upload_stop fileinfo_t fi,
const gchar *  reason
 

Stop all sharing occuring for this fileinfo.

filesize_t get_random_offset const filesize_t  size  )  [static]
 

Returns:
a random offset within the file, aligned on a TTH block boundary.

GSList* list_clone_shift fileinfo_t fi  )  [static]
 

Clone fileinfo's chunk list, shifting the origin of the list to a randomly selected offset within the file.

If the first chunk is not completed or not at least `pfsp_first_chunk' bytes long, the original list is returned.

gboolean looks_like_urn const gchar *  filename  )  [static]
 

Check whether filename looks like an URN.

WARN_UNUSED_RESULT gboolean READ_CHAR guint8 *  val,
guint32 *  checksum
[static]
 

WARN_UNUSED_RESULT gboolean READ_STR gchar *  data,
size_t  size,
guint32 *  checksum
[static]
 

WARN_UNUSED_RESULT gboolean READ_UINT32 guint32 *  val_ptr,
guint32 *  checksum
[static]
 

size_t round_grow size_t  x  )  [inline, static]
 

void TBUF_CHECK size_t  size  )  [inline, static]
 

void tbuf_check void   )  [static]
 

void tbuf_extend size_t  x,
gboolean  writing
[static]
 

Make sure there is enough room in the buffer for `x' more bytes.

If `writing' is TRUE, we update the write pointer.

WARN_UNUSED_RESULT gboolean TBUF_GET_UINT32 guint32 *  x  )  [static]
 

WARN_UNUSED_RESULT gboolean TBUF_GETCHAR guint8 *  x  )  [static]
 

void TBUF_INIT_READ size_t  size  )  [inline, static]
 

void TBUF_INIT_WRITE void   )  [inline, static]
 

void TBUF_PUT_CHAR guint8  x  )  [static]
 

void TBUF_PUT_UINT32 guint32  x  )  [static]
 

ssize_t tbuf_read gint  fd,
size_t  len
[static]
 

Read trailer buffer at current position from `fd'.

Returns:
-1 on error.

WARN_UNUSED_RESULT gboolean TBUF_READ gchar *  x,
size_t  size
[static]
 

void tbuf_write const struct file_object fo,
filesize_t  offset
[static]
 

Write trailer buffer at current position on `fd', whose name is `name'.

void TBUF_WRITE const gchar *  data,
size_t  size
[static]
 

size_t TBUF_WRITTEN_LEN void   )  [inline, static]
 

gboolean trailer_is_64bit const struct trailer tb  )  [inline, static]
 

Checks the kind of trailer.

The trailer must be initialized.

Returns:
TRUE if the trailer is the 64-bit version, FALSE if it's 32-bit.

void WRITE_CHAR guint8  val,
guint32 *  checksum
[static]
 

void WRITE_STR const gchar *  data,
size_t  size,
guint32 *  checksum
[static]
 

void WRITE_UINT32 guint32  val,
guint32 *  checksum
[static]
 


Variable Documentation

gchar* arena
 

Base arena.

gboolean can_swarm = FALSE [static]
 

Set by file_info_retrieve().

const gchar* end
 

First byte off arena.

GHashTable* fi_by_guid [static]
 

GHashTable* fi_by_namesize [static]
 

GHashTable* fi_by_outname [static]
 

GHashTable* fi_by_sha1 [static]
 

event_t* fi_events[EV_FI_EVENTS]
 

Initial value:

 {
    NULL, NULL, NULL, NULL, NULL, NULL }

idtable_t* fi_handle_map = NULL [static]
 

const struct fi_tag fi_tag_map[] [static]
 

const gchar file_info_file[] = "fileinfo" [static]
 

const gchar file_info_what[] = "the fileinfo database" [static]
 

gboolean fileinfo_dirty = FALSE [static]
 

const gchar* rptr
 

Read pointer.

size_t size
 

Current size of arena.

struct { ... } tbuf [static]
 

The swarming trailer is built within a memory buffer first, to avoid having to issue mutliple write() system calls.

We can't use stdio's buffering since we can sometime reuse the download's file descriptor.

gchar* wptr
 

Write pointer.


Generated on Sat Jun 30 17:53:26 2007 for gtk-gnutella by  doxygen 1.3.9.1