File: common.c

package info (click to toggle)
libomxil-bellagio 0.9.3-4.1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 3,680 kB
  • sloc: ansic: 14,245; sh: 10,278; makefile: 206
file content (209 lines) | stat: -rw-r--r-- 5,538 bytes parent folder | download | duplicates (6)
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
/**
  src/common.c

  This file implements some useful common functionalities for handling the register files used in Bellagio

  Copyright (C) 2007-2011 STMicroelectronics
  Copyright (C) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).

  This library is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free
  Software Foundation; either version 2.1 of the License, or (at your option)
  any later version.

  This library 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 Lesser General Public License for more
  details.

  You should have received a copy of the GNU Lesser General Public License
  along with this library; if not, write to the Free Software Foundation, Inc.,
  51 Franklin St, Fifth Floor, Boston, MA
  02110-1301  USA

*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>

#include "config.h"
#include "common.h"

#define REGISTRY_FILENAME ".omxregister"
#define REGISTRY_DIR "/var/lib/libomxil-bellagio0/"

#ifdef ANDROID_COMPILATION
#define TMP_DATA_DIRECTORY "/data/omx/"
#else
#define TMP_DATA_DIRECTORY "/tmp/"
#endif

/** @brief Get registry filename
 * This function returns the name of the registry file for the components loaded with the default component loader.
 */
char* componentsRegistryGetFilename(void) {
  return componentsRegistryGetFilenameCheck(0);
}

char* componentsRegistryGetFilenameCheck(int check_exists) {
  char *omxregistryfile = NULL;
  char *buffer;
  struct stat sb;

  buffer=getenv("OMX_BELLAGIO_REGISTRY");
  if(buffer!=NULL&&*buffer!='\0') {
    omxregistryfile = strdup(buffer);
    if (!check_exists||stat(omxregistryfile, &sb) == 0) {
      return omxregistryfile;
    } else {
      if (omxregistryfile) {
	free(omxregistryfile);
	omxregistryfile=NULL;
      }
    }
    return NULL;
  }

  buffer=getenv("XDG_DATA_HOME");
  if(buffer!=NULL&&*buffer!='\0') {
    omxregistryfile = malloc(strlen(buffer) + strlen(REGISTRY_FILENAME) + 2);
    strcpy(omxregistryfile, buffer);
    strcat(omxregistryfile, "/");
    strcat(omxregistryfile, REGISTRY_FILENAME);
    if (!check_exists||stat(omxregistryfile, &sb) == 0) {
      return omxregistryfile;
    } else {
      if (omxregistryfile) {
	free(omxregistryfile);
	omxregistryfile=NULL;
      }
    }
  }

  buffer=getenv("HOME");
  if(buffer!=NULL&&*buffer!='\0') {
    omxregistryfile  = malloc(strlen(buffer) + strlen(REGISTRY_FILENAME) + 3);
    strcpy(omxregistryfile, buffer);
    strcat(omxregistryfile, "/");
    strcat(omxregistryfile, REGISTRY_FILENAME);
  } else {
    omxregistryfile  = malloc(strlen(TMP_DATA_DIRECTORY) + strlen(REGISTRY_FILENAME) + 2);
    strcpy(omxregistryfile, TMP_DATA_DIRECTORY);
    strcat(omxregistryfile, REGISTRY_FILENAME);
  }
  if (!check_exists||stat(omxregistryfile, &sb) == 0) {
    return omxregistryfile;
  } else {
    if (omxregistryfile) {
      free(omxregistryfile);
      omxregistryfile=NULL;
    }
  }
  omxregistryfile = malloc(strlen(REGISTRY_DIR) + strlen("registry") + 2);
  strcpy(omxregistryfile, REGISTRY_DIR);
  strcat(omxregistryfile, "registry");
  return omxregistryfile;
}

/* This function return the absolute path of the registry_name file or
 * directory depending by the environment variable set.
 * the variables considered in order are:
 * $XDG_DATA_HOME
 * $HOME
 * TMP_DATA_DIRECTORY (/tmp by default on Linux)
 * The function does not check for file/dir existence
 */
char* loadersRegistryGetFilename(char* registry_name) {
	char *omxregistryfile = NULL;
	char *buffer;

	buffer=getenv("XDG_DATA_HOME");
	if(buffer!=NULL&&*buffer!='\0') {
		omxregistryfile = malloc(strlen(buffer) + strlen(registry_name) + 2);
		strcpy(omxregistryfile, buffer);
		strcat(omxregistryfile, "/");
		strcat(omxregistryfile, registry_name);
		return omxregistryfile;
	}

	buffer=getenv("HOME");
	if(buffer!=NULL&&*buffer!='\0') {
		omxregistryfile  = malloc(strlen(buffer) + strlen(registry_name) + 3);
		strcpy(omxregistryfile, buffer);
		strcat(omxregistryfile, "/");
		strcat(omxregistryfile, registry_name);
	} else {
		omxregistryfile  = malloc(strlen(TMP_DATA_DIRECTORY) + strlen(registry_name) + 2);
		strcpy(omxregistryfile, TMP_DATA_DIRECTORY);
		strcat(omxregistryfile, registry_name);
	}
	return omxregistryfile;
}

/** This function creates the directory specified by newdir
 * It returns 1 if is not possible to create it
 * It returns 0 if the directory is created or it already exists
 */
int makedir (const char *newdir) {
  char *buffer;
  char *p;
  int err;
  size_t len;

  buffer = strdup(newdir);
  len = strlen(buffer);

  if (len == 0) {
    free(buffer);
    return 1;
  }
  if (buffer[len-1] == '/') {
    buffer[len-1] = '\0';
  }

	err = mkdir(buffer, 0755);
	if (err == 0 || (( err == -1) && (errno == EEXIST))) {
		free(buffer);
		return 0;
	}

  p = buffer+1;
  while (1) {
		char hold;

		while(*p && *p != '\\' && *p != '/')
			p++;
		hold = *p;
		*p = 0;
		if ((mkdir(buffer, 0755) == -1) && (errno == ENOENT)) {
			free(buffer);
			return 1;
		}
		if (hold == 0)
			break;
		*p++ = hold;
	}
  free(buffer);
  return 0;

}

int exists(const char* fname) {
	FILE *file;
	if ((file = fopen(fname, "r")))
	{
		fclose(file);
		return 1;
	}
	return 0;
}

#ifdef COMMON_TEST
int main (int argc, char*argv[]) {
  printf (componentsRegistryGetFilename (1));
}
#endif