summaryrefslogtreecommitdiff
path: root/Build/source/texk/kpathsea/remote.c
blob: 65a967e5680c5c170762205da00fbfa812225a85 (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
/*
  Handling of remote files with kpathsea.
*/

#include <kpathsea/config.h>
#include <kpathsea/c-pathch.h>
#include <kpathsea/hash.h>
#include <geturl.h>

hash_table_type *remote_db;

static char *log_buffer;
int index;

void __cdecl DoDownloadProgress(unsigned long partial, unsigned long total)
{
  sprintf(log_buffer+index," -> %8ld/%8ld\r", partial, total);
  fputs(log_buffer, stdout);
}

void __cdecl DoDownloadLog(char *s, ...)
{
#if 0
  va_list argList;
  fputc('\n', stderr);
  va_start (argList, s);
  vfprintf(stderr, s, argList);
  va_end (argList);
#endif
}

string get_remote_file(const_string filename)
{
  string *lookup = NULL;
  string localname = NULL;
  boolean ret;

  if ((remote_db = hash_exists_p(hashtable_remote)) == NULL) {
    remote_db = hash_create (1007, hashtable_remote);
  }

  lookup = hash_lookup(remote_db, filename);
  /* Either the file has already been downloaded */
  if (lookup && *lookup) {
    return *lookup;
  }
  /* Or it is a new one */
  /* Get some local name */
  localname =_tempnam(getenv("TMP"), "kpse");
  if (localname == NULL) return localname;
  
  index = strlen(filename);
  log_buffer = xmalloc(index + 24);
  strcpy(log_buffer, filename);

  ret = (get_url_to_file(filename, 
			 localname,
			 0, 
			 DoDownloadLog, 
			 DoDownloadProgress,
			 NULL, /* AfxGetInstanceHandle() */
			 NetIOIE5, /*g_uiNetMethod */
			 NULL, /* _proxy_address */
			 80 /* g_uiProxyPort */
			 ) == 0);
  
  if (ret) 
    hash_insert(remote_db, filename, localname);
  else
    localname = NULL;

  puts("\n");
  free(log_buffer);
  return localname;
}

void CDECL unlink_remote_file(const_string key, const_string value)
{
  int fa;
  fa = GetFileAttributes(value);
  if (fa != 0xFFFFFFFF && !(fa & FILE_ATTRIBUTE_DIRECTORY)) {
    if (DeleteFile(value) == 0) {
      fprintf(stderr, "%s: can't delete file %s (error %d)\n", kpse_program_name, value, GetLastError());
    }
  }
}

void cleanup_remote_files()
{
  hash_table_type *remote = hash_exists_p(hashtable_remote);
  hash_iter(remote, unlink_remote_file);
}