4 * Copyright (C) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
7 * DoHyung Hong <don.hong@samsung.com>
8 * SeokYeon Hwang <syeon.hwang@samsung.com>
9 * Hyunjun Son <hj79.son@samsung.com>
10 * SangJin Kim <sangjin3.kim@samsung.com>
11 * MunKyu Im <munkyu.im@samsung.com>
12 * KiTae Kim <kt920.kim@samsung.com>
13 * JinHyung Jo <jinhyung.jo@samsung.com>
14 * SungMin Ha <sungmin82.ha@samsung.com>
15 * JiHye Kim <jihye1128.kim@samsung.com>
16 * GiWoong Kim <giwoong.kim@samsung.com>
17 * YeongKyoon Lee <yeongkyoon.lee@samsung.com>
18 * DongKyun Yun <dk77.yun@samsung.com>
20 * This program is free software; you can redistribute it and/or
21 * modify it under the terms of the GNU General Public License
22 * as published by the Free Software Foundation; either version 2
23 * of the License, or (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
45 //DEFAULT_DEBUG_CHANNEL(tizen_sdk);
46 MULTI_DEBUG_CHANNEL(tizen_sdk, fileio);
48 extern STARTUP_OPTION startup_option;
50 static void dos_path_to_unix_path(char *path)
54 for (i = 0; path[i]; i++)
59 static void unix_path_to_dos_path(char *path)
63 for (i = 0; path[i]; i++)
70 * @brief check about file
71 * @param filename : file path (ex: /home/$(USER_ID)/.tizen_sdk/simulator/1/emulator.conf)
72 * @return exist normal(1), not exist(0), error case(-1))
76 int is_exist_file(gchar *filepath)
78 if ((filepath == NULL) || (strlen(filepath) <= 0)) {
79 ERR( "filepath is incorrect.\n");
83 if (strlen(filepath) >= MAXBUF) {
84 ERR( "file path is too long. (%s)\n", filepath);
88 if (g_file_test(filepath, G_FILE_TEST_IS_DIR) == TRUE) {
89 INFO( "%s: is not a file, is a directory!!\n", filepath);
93 if (g_file_test(filepath, G_FILE_TEST_EXISTS) == TRUE) {
94 TRACE( "%s: exists normally!!\n", filepath);
98 INFO( "%s: not exists!!\n", filepath);
99 return FILE_NOT_EXISTS;
102 /* get_sdk_root = "~/tizen_sdk/" */
103 gchar *get_sdk_root(void)
105 static gchar *sdk_path = NULL;
106 gchar *info_file_subpath = NULL;
107 gchar *info_file_fullpath = NULL;
108 gchar *file_buf = NULL;
113 const gchar *home_path = g_get_home_dir();
114 info_file_subpath = "/.TizenSDK/tizensdkpath";
116 int len = strlen(home_path) + strlen(info_file_subpath) + 1;
117 info_file_fullpath = g_malloc0(len);
118 snprintf(info_file_fullpath, len, "%s%s", home_path, info_file_subpath);
121 TCHAR szDefaultPath[_MAX_PATH] = {0};
122 DWORD dwBufLen = MAX_PATH;
124 RegOpenKeyEx(HKEY_CURRENT_USER,
125 "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders",
126 0, KEY_QUERY_VALUE, &hKey);
127 RegQueryValueEx(hKey, "Local AppData", NULL, NULL, (LPBYTE)szDefaultPath, &dwBufLen);
130 info_file_subpath = "\\.TizenSDK\\tizensdkpath";
132 int len = strlen(szDefaultPath) + strlen(info_file_subpath) + 1;
133 info_file_fullpath = g_malloc0(len);
134 snprintf(info_file_fullpath, len, "%s%s", szDefaultPath, info_file_subpath);
137 if (!g_file_get_contents(info_file_fullpath, &file_buf, NULL, NULL)) {
138 g_free(info_file_fullpath);
142 sdk_path = strchr(file_buf, '=') + 1;
144 g_free(info_file_fullpath);
150 /* get_sdb_path = "~/tizen_sdk/SDK/sdb/sdb" */
151 // After using this function, please call g_free().
152 gchar *get_sdb_path(void)
154 gchar *sdb_fullpath = NULL;
155 gchar *sdb_subpath = "/SDK/sdb/sdb";
156 gchar *sdk_path = NULL;
158 sdk_path = get_sdk_root();
164 int len = strlen(sdk_path) + strlen(sdb_subpath) + 1;
165 sdb_fullpath = g_malloc0(len);
166 snprintf(sdb_fullpath, len, "%s%s", sdk_path, sdb_subpath);
168 int len = strlen(sdk_path) + strlen(sdb_subpath) + 5;
169 sdb_fullpath = g_malloc0(len);
170 snprintf(sdb_fullpath, len, "%s%s.exe", sdk_path, sdb_subpath);
171 dos_path_to_unix_path(sdk_path);
177 /* exec_path = "~/tizen_sdk/Emulator/bin/emulator-manager" */
178 /* = "~/tizen_sdk/Emulator/bin/emulator-x86" */
179 const gchar *get_exec_path(void)
181 static gchar *exec_path = NULL;
185 /* allocate just once */
189 const char *env_path = getenv("EMULATOR_PATH");
191 exec_path = strdup(env_path);
197 exec_path = malloc(len);
199 fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__); exit(1);
203 r = readlink("/proc/self/exe", exec_path, len);
209 r = GetModuleFileName(NULL, exec_path, len);
212 dos_path_to_unix_path(exec_path);
223 /* get_root_path = "~/tizen_sdk/Emulator" */
224 const gchar *get_root_path(void)
226 static gchar *root_path;
227 static gchar *root_path_buf;
231 const gchar *exec_path = get_exec_path();
232 root_path_buf = g_path_get_dirname(exec_path);
233 root_path = g_path_get_dirname(root_path_buf);
234 g_free(root_path_buf);
240 /* get_bin_path = "~/tizen_sdk/Emulator/bin" */
241 const gchar *get_bin_path(void)
243 static gchar *bin_path;
247 const gchar *exec_path = get_exec_path();
248 bin_path = g_path_get_dirname(exec_path);
254 /* get_baseimg_path = "~/tizen_sdk/Emulator/{ARCH}/emulimg{VERSION}.{ARCH}" */
255 const gchar *get_baseimg_path(void)
257 const gchar *arch_path;
259 char* MAJOR_VERSION = NULL;
260 char version_path[MAXPATH];
261 gchar *target_list_filepath;
262 char *arch = (char *)g_getenv(EMULATOR_ARCH);
263 const gchar *exec_path = get_exec_path();
264 target_list_filepath = get_targetlist_filepath();
265 sprintf(version_path, "%s/version.ini",get_etc_path());
266 MAJOR_VERSION = (char*)get_config_value(version_path, VERSION_GROUP, MAJOR_VERSION_KEY);
268 if(!arch) /* for stand alone */
270 char *binary = g_path_get_basename(exec_path);
271 if(strstr(binary, EMULATOR_X86))
272 arch = g_strdup_printf(X86);
273 else if(strstr(binary, EMULATOR_ARM))
274 arch = g_strdup_printf(ARM);
277 ERR( "binary setting failed\n");
283 subdir = g_strdup_printf("/emulimg-%s.%s", MAJOR_VERSION, arch);
285 arch_path = get_arch_path();
286 path = malloc(strlen(arch_path) + strlen(subdir) + 2);
287 strcpy(path, arch_path);
288 strcat(path, subdir);
295 /* get_arch_path = "~/tizen_sdk/Emulator/{ARCH}" */
296 const gchar *get_arch_path(void)
300 char *arch = (char *)g_getenv(EMULATOR_ARCH);
302 const gchar *exec_path = get_exec_path();
303 path_buf = g_path_get_dirname(g_path_get_dirname(exec_path));
304 if(!arch) /* for stand alone */
306 char *binary = g_path_get_basename(exec_path);
307 if(strstr(binary, EMULATOR_X86))
308 arch = g_strdup_printf(X86);
309 else if(strstr(binary, EMULATOR_ARM))
310 arch = g_strdup_printf(ARM);
313 ERR( "binary setting failed\n");
319 path = malloc(strlen(path_buf) + strlen(arch) + 2);
321 strcpy(path, path_buf);
329 /* get_etc_path = "~/tizen_sdk/simulator/etc" */
330 const gchar *get_etc_path(void)
332 const char etcsubdir[] = "/etc";
334 static char *etc_path;
339 path = get_root_path();
340 etc_path = malloc(strlen(path) + sizeof etcsubdir + 1);
342 ERR( "%s - %d: memory allocation failed!\n", __FILE__, __LINE__);
346 strcpy(etc_path, path);
347 strcat(etc_path, etcsubdir);
349 if (g_file_test(etc_path, G_FILE_TEST_IS_DIR) == FALSE) {
350 ERR( "no etc directory at %s\n", etc_path);
356 /* get_skin_path = "~/tizen_sdk/simulator/skins" */
357 const gchar *get_skin_path(void)
359 const char *skin_path_env;
360 const char skinsubdir[] = "/skins";
363 static char *skin_path;
368 skin_path_env = getenv("EMULATOR_SKIN_PATH");
371 path = get_root_path();
372 skin_path = malloc(strlen(path) + sizeof skinsubdir + 1);
374 fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__);
378 strcpy(skin_path, path);
379 strcat(skin_path, skinsubdir);
382 skin_path = strdup(skin_path_env);
384 if (g_file_test(skin_path, G_FILE_TEST_IS_DIR) == FALSE) {
385 fprintf(stderr, "no skin directory at %s\n", skin_path);
391 /* get_tizen_tmp_path = "/tmp/tizen_sdk" (linux) *
392 * get env variables TMPDIR, TMP, and TEMP in that order (windows) */
393 const gchar *get_tizen_tmp_path(void)
395 const char *tmp_path;
396 const char subdir[] = "/tizen_sdk";
399 tmp_path = g_get_tmp_dir();
400 path = malloc(strlen(tmp_path) + sizeof subdir + 1);
402 fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__);
406 strcpy(path, tmp_path);
407 strcat(path, subdir);
413 /* get_data_path = "~/tizen_sdk/Emulator/{ARCH}/data" */
414 const gchar *get_data_path(void)
416 static const char suffix[] = "/data";
417 static gchar *data_path;
421 const gchar *path = get_arch_path();
423 data_path = malloc(strlen(path) + sizeof suffix + 1);
424 assert(data_path != NULL);
425 strcpy(data_path, path);
426 strcat(data_path, suffix);
434 * @brief change_path_to_slash (\, \\ -> /)
435 * @param org path to change (C:\\test\\test\\test)
436 * @return changed path (C:/test/test/test)
439 gchar *change_path_to_slash(gchar *org_path)
441 gchar *path = strdup(org_path);
443 dos_path_to_unix_path(path);
448 gchar *change_path_from_slash(gchar *org_path)
450 gchar *path = strdup(org_path);
452 unix_path_to_dos_path(path);
459 /* get_conf_path = "~/tizen_sdk/Emulator/{ARCH}/conf" */
460 const gchar *get_conf_path(void)
462 static gchar *conf_path;
464 static const char suffix[] = "/conf";
466 const gchar *path = get_arch_path();
467 conf_path = malloc(strlen(path) + sizeof suffix + 1);
468 assert(conf_path != NULL);
469 strcpy(conf_path, path);
470 strcat(conf_path, suffix);
474 /* get_tizen_vms_arch_path = "/home/{USER}/.tizen_vms/{ARCH}" */
475 const gchar *get_tizen_vms_arch_path(void)
477 char *tizen_vms_arch_path;
478 char *tizen_vms = (char*)get_tizen_vms_path();
479 char *arch = (char *)g_getenv(EMULATOR_ARCH);
480 const gchar *exec_path = get_exec_path();
481 if(!arch) /* for stand alone */
483 char *binary = g_path_get_basename(exec_path);
484 if(strstr(binary, EMULATOR_X86))
485 arch = g_strdup_printf(X86);
486 else if(strstr(binary, EMULATOR_ARM))
487 arch = g_strdup_printf(ARM);
490 ERR( "binary setting failed\n");
496 tizen_vms_arch_path = malloc(strlen(tizen_vms) + 1 + strlen(arch) + 1);
497 assert(tizen_vms_arch_path != NULL);
498 strcpy(tizen_vms_arch_path, tizen_vms);
499 strcat(tizen_vms_arch_path, "/");
500 strcat(tizen_vms_arch_path, arch);
502 return tizen_vms_arch_path;
505 /* get_tizen_vms_path = "/home/{USER}/.tizen_vms" */
506 const gchar *get_tizen_vms_path(void)
508 static const char tizen_vms[] = "/.tizen_vms";
510 char *homedir = (char*)g_getenv("HOME");
512 char *homedir = (char*)g_getenv("USERPROFILE");
514 static gchar *tizen_vms_path;
517 homedir = (char*)g_get_home_dir();
519 tizen_vms_path = malloc(strlen(homedir) + sizeof tizen_vms + 1);
520 assert(tizen_vms_path != NULL);
521 strcpy(tizen_vms_path, homedir);
522 strcat(tizen_vms_path, tizen_vms);
524 return tizen_vms_path;
527 /* get_screenshot_path = "/home/{USER}/.tizen_vms/screenshots" */
528 gchar *get_screenshots_path(void)
530 const char subdir[] = "/screenshots";
531 char *tizen_vms_path = (char*)get_tizen_vms_path();
532 char *screenshots_path;
534 screenshots_path = malloc(strlen(tizen_vms_path) + sizeof subdir + 1);
535 assert(screenshots_path != NULL);
536 strcpy(screenshots_path, tizen_vms_path);
537 strcat(screenshots_path, subdir);
539 return screenshots_path;
542 /* get_targetlist_filepath = " ~/tizen_VMs/{ARCH}/targetlist.ini" */
543 gchar *get_targetlist_filepath(void)
545 gchar *targetlist_filepath = NULL;
546 targetlist_filepath = calloc(1, 512);
547 if(NULL == targetlist_filepath) {
548 fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__); exit(1);
551 const gchar *vms_path = get_tizen_vms_arch_path();
552 sprintf(targetlist_filepath, "%s/targetlist.ini", vms_path);
554 return targetlist_filepath;
557 /* get_virtual_target_path "~/tizen_VMs/{ARCH}/virtual_target_name/" */
558 gchar *get_virtual_target_path(gchar *virtual_target_name)
560 gchar *virtual_target_path = NULL;
561 virtual_target_path = calloc(1,512);
563 if(NULL == virtual_target_path) {
564 fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__); exit(1);
567 const gchar *conf_path = get_tizen_vms_arch_path();
568 sprintf(virtual_target_path, "%s/%s/", conf_path, virtual_target_name);
570 return virtual_target_path;
573 /* get_virtual_target_log_path "~/tizen_VMs/{ARCH}/virtual_target_name/logs" */
574 gchar *get_virtual_target_log_path(gchar *virtual_target_name)
576 gchar *virtual_target_log_path = NULL;
577 virtual_target_log_path = calloc(1,512);
579 if(NULL == virtual_target_log_path) {
580 fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__); exit(1);
583 const gchar *vms_path = get_tizen_vms_arch_path();
584 sprintf(virtual_target_log_path, "%s/%s/logs", vms_path, virtual_target_name);
586 return virtual_target_log_path;
590 int check_port(const char *ip_address, int port)
592 struct sockaddr_in address;
593 int sockfd, connect_status;
595 if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
597 fprintf(stderr, "Socket Open error\n");
601 memset(&address, 0 , sizeof(address));
602 address.sin_family = AF_INET;
603 address.sin_port = htons(port);
604 address.sin_addr.s_addr = inet_addr(ip_address);
606 if(address.sin_addr.s_addr == INADDR_NONE)
608 fprintf(stderr, "Bad Address\n");
612 connect_status = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
614 return connect_status;