aboutsummaryrefslogtreecommitdiff
path: root/common/fmplayer_file_gio.c
blob: 053e4aebdf7194b9ee2228f15546b56dbb5e54fc (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
#include "common/fmplayer_file.h"
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>

static void *fileread(GFile *f, size_t maxsize, size_t *filesize, enum fmplayer_file_error *error) {
  GFileInfo *finfo = 0;
  GFileInputStream *fstream = 0;
  void *buf = 0;
  finfo = g_file_query_info(f, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, 0, 0);
  if (!finfo) {
    if (error) *error = FMPLAYER_FILE_ERR_FILEIO;
    goto err;
  }
  gsize filelen;
  {
    goffset sfilelen = g_file_info_get_size(finfo);
    if (sfilelen < 0) {
      if (error) *error = FMPLAYER_FILE_ERR_FILEIO;
      goto err;
    }
    filelen = sfilelen;
  }
  if (maxsize && (filelen > maxsize)) {
    if (error) *error = FMPLAYER_FILE_ERR_BADFILE_SIZE;
    goto err;
  }
  fstream = g_file_read(f, 0, 0);
  if (!fstream) {
    if (error) *error = FMPLAYER_FILE_ERR_FILEIO;
    goto err;
  }
  buf = malloc(filelen);
  if (!buf) {
    if (error) *error = FMPLAYER_FILE_ERR_NOMEM;
    goto err;
  }
  gsize fileread;
  g_input_stream_read_all(G_INPUT_STREAM(fstream), buf, filelen, &fileread, 0, 0);
  if (fileread != filelen) {
    if (error) *error = FMPLAYER_FILE_ERR_FILEIO;
    goto err;
  }
  *filesize = filelen;
  g_object_unref(G_OBJECT(fstream));
  g_object_unref(G_OBJECT(finfo));
  return buf;
err:
  free(buf);
  if (fstream) g_object_unref(G_OBJECT(fstream));
  if (finfo) g_object_unref(G_OBJECT(finfo));
  return 0;
}

void *fmplayer_fileread(const void *pathptr, const char *pcmname, const char *extension,
                        size_t maxsize, size_t *filesize, enum fmplayer_file_error *error) {
  GFile *file = 0, *dir = 0;
  GFileEnumerator *direnum = 0;
  char *pcmnamebuf = 0;
  const char *uri = pathptr;
  file = g_file_new_for_uri(uri);
  if (!pcmname) {
    void *buf = fileread(file, maxsize, filesize, error);
    g_object_unref(G_OBJECT(file));
    return buf;
  }
  if (extension) {
    size_t namebuflen = strlen(pcmname) + strlen(extension) + 1;
    pcmnamebuf = malloc(namebuflen);
    if (!pcmnamebuf) goto err;
    strcpy(pcmnamebuf, pcmname);
    strcat(pcmnamebuf, extension);
    pcmname = pcmnamebuf;
  }

  dir = g_file_get_parent(file);
  if (!dir) {
    if (error) *error = FMPLAYER_FILE_ERR_NOMEM;
    goto err;
  }
  direnum = g_file_enumerate_children(dir,
                                      G_FILE_ATTRIBUTE_STANDARD_NAME,
                                      G_FILE_QUERY_INFO_NONE,
                                      0, 0);
  if (!direnum) {
    if (error) *error = FMPLAYER_FILE_ERR_NOMEM;
    goto err;
  }
  for (;;) {
    GFileInfo *info;
    GFile *pcmfile;
    if (!g_file_enumerator_iterate(direnum, &info, &pcmfile, 0, 0)) {
      if (error) *error = FMPLAYER_FILE_ERR_FILEIO;
      goto err;
    }
    if (!info || !pcmfile) {
      if (error) *error = FMPLAYER_FILE_ERR_FILEIO;
      goto err;
    }
    if (!strcasecmp(g_file_info_get_name(info), pcmname)) {
      void *buf = fileread(pcmfile, maxsize, filesize, error);
      g_object_unref(G_OBJECT(direnum));
      g_object_unref(G_OBJECT(dir));
      g_object_unref(G_OBJECT(file));
      free(pcmnamebuf);
      return buf;
    }
  }
  if (error) *error = FMPLAYER_FILE_ERR_FILEIO;
err:
  if (direnum) g_object_unref(G_OBJECT(direnum));
  if (dir) g_object_unref(G_OBJECT(dir));
  g_object_unref(G_OBJECT(file));
  free(pcmnamebuf);
  return 0;
}

void *fmplayer_path_dup(const void *path) {
  return strdup(path);
}