Tizen 2.1 base
[sdk/emulator/qemu.git] / tizen / src / fileio.c
1 /*
2  * Emulator
3  *
4  * Copyright (C) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:
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>
19  *
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.
24  *
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.
29  *
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.
33  *
34  * Contributors:
35  * - S-Core Co., Ltd
36  *
37  */
38
39
40 #include <assert.h>
41 #include "fileio.h"
42
43 #include "debug_ch.h"
44
45 //DEFAULT_DEBUG_CHANNEL(tizen_sdk);
46 MULTI_DEBUG_CHANNEL(tizen_sdk, fileio);
47
48 extern STARTUP_OPTION startup_option;
49 #ifdef _WIN32
50 static void dos_path_to_unix_path(char *path)
51 {
52     int i;
53
54     for (i = 0; path[i]; i++)
55         if (path[i] == '\\')
56             path[i] = '/';
57 }
58
59 static void unix_path_to_dos_path(char *path)
60 {
61     int i;
62
63     for (i = 0; path[i]; i++)
64         if (path[i] == '/')
65             path[i] = '\\';
66 }
67 #endif
68
69 /**
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))
73  * @date    Oct. 22. 2009
74  * */
75
76 int is_exist_file(gchar *filepath)
77 {
78     if ((filepath == NULL) || (strlen(filepath) <= 0))  {
79         ERR( "filepath is incorrect.\n");
80         return -1;
81     }
82
83     if (strlen(filepath) >= MAXBUF) {
84         ERR( "file path is too long. (%s)\n", filepath);
85         return -1;
86     }
87
88     if (g_file_test(filepath, G_FILE_TEST_IS_DIR) == TRUE) {
89         INFO( "%s: is not a file, is a directory!!\n", filepath);
90         return -1;
91     }
92
93     if (g_file_test(filepath, G_FILE_TEST_EXISTS) == TRUE) {
94         TRACE( "%s: exists normally!!\n", filepath);
95         return FILE_EXISTS;
96     }
97
98     INFO( "%s: not exists!!\n", filepath);
99     return FILE_NOT_EXISTS;
100 }
101
102 /* get_sdk_root = "~/tizen_sdk/" */
103 gchar *get_sdk_root(void)
104 {
105     static gchar *sdk_path = NULL;
106     gchar *info_file_subpath = NULL;
107     gchar *info_file_fullpath = NULL;
108     gchar *file_buf = NULL;
109
110     if (!sdk_path)
111     {
112 #ifdef __linux__
113         const gchar *home_path = g_get_home_dir();
114         info_file_subpath = "/.TizenSDK/tizensdkpath";
115
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);
119 #elif _WIN32
120         HKEY hKey;
121         TCHAR szDefaultPath[_MAX_PATH] = {0};
122         DWORD dwBufLen = MAX_PATH;
123
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);
128         RegCloseKey(hKey);
129
130         info_file_subpath = "\\.TizenSDK\\tizensdkpath";
131
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);
135 #endif
136
137         if (!g_file_get_contents(info_file_fullpath, &file_buf, NULL, NULL)) {
138             g_free(info_file_fullpath);
139             return NULL;
140         }
141
142         sdk_path = strchr(file_buf, '=') + 1;
143
144         g_free(info_file_fullpath);
145     }
146
147     return sdk_path;
148 }
149
150 /* get_sdb_path = "~/tizen_sdk/SDK/sdb/sdb" */
151 // After using this function, please call g_free().
152 gchar *get_sdb_path(void)
153 {
154     gchar *sdb_fullpath = NULL;
155     gchar *sdb_subpath = "/SDK/sdb/sdb";
156     gchar *sdk_path = NULL;
157
158     sdk_path = get_sdk_root();
159     if (!sdk_path) {
160         return NULL;
161     }
162
163 #ifdef __linux__
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);
167 #elif _WIN32
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);
172 #endif
173
174     return sdb_fullpath;
175 }
176
177 /* exec_path = "~/tizen_sdk/Emulator/bin/emulator-manager" */
178 /*           = "~/tizen_sdk/Emulator/bin/emulator-x86" */
179 const gchar *get_exec_path(void)
180 {
181     static gchar *exec_path = NULL;
182     int len = 10;
183     int r;
184
185     /* allocate just once */
186     if (exec_path)
187         return exec_path;
188
189     const char *env_path = getenv("EMULATOR_PATH");
190     if (env_path) {
191         exec_path = strdup(env_path);
192         return exec_path;
193     }
194
195     while (1)
196     {
197         exec_path = malloc(len);
198         if (!exec_path) {
199             fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__); exit(1);
200         }
201
202 #ifndef _WIN32
203         r = readlink("/proc/self/exe", exec_path, len);
204         if (r < len) {
205             exec_path[r] = 0;
206             break;
207         }
208 #else
209         r = GetModuleFileName(NULL, exec_path, len);
210         if (r < len) {
211             exec_path[r] = 0;
212             dos_path_to_unix_path(exec_path);
213             break;
214         }
215 #endif
216         free(exec_path);
217         len *= 2;
218     }
219
220     return exec_path;
221 }
222
223 /* get_root_path = "~/tizen_sdk/Emulator" */
224 const gchar *get_root_path(void)
225 {
226     static gchar *root_path;
227     static gchar *root_path_buf;
228
229     if (!root_path)
230     {
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);
235     }
236
237     return root_path;
238 }
239
240 /* get_bin_path = "~/tizen_sdk/Emulator/bin" */
241 const gchar *get_bin_path(void)
242 {
243     static gchar *bin_path;
244
245     if (!bin_path)
246     {
247         const gchar *exec_path = get_exec_path();
248         bin_path = g_path_get_dirname(exec_path);
249     }
250
251     return bin_path;
252 }
253
254 /* get_baseimg_path = "~/tizen_sdk/Emulator/{ARCH}/emulimg{VERSION}.{ARCH}" */
255 const gchar *get_baseimg_path(void)
256 {
257     const gchar *arch_path;
258     static gchar *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);
267     char* subdir = NULL;
268     if(!arch) /* for stand alone */
269     {
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);
275         else
276         {
277             ERR( "binary setting failed\n");
278             exit(1);
279         }
280         free(binary);
281     }
282
283     subdir = g_strdup_printf("/emulimg-%s.%s", MAJOR_VERSION, arch);
284
285     arch_path = get_arch_path();
286     path = malloc(strlen(arch_path) + strlen(subdir) + 2);
287     strcpy(path, arch_path);
288     strcat(path, subdir);
289
290     free(MAJOR_VERSION);
291     free(subdir);
292     return path;
293 }
294
295 /* get_arch_path = "~/tizen_sdk/Emulator/{ARCH}" */
296 const gchar *get_arch_path(void)
297 {
298     gchar *path_buf;
299     static gchar *path;
300     char *arch = (char *)g_getenv(EMULATOR_ARCH);
301
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 */
305     {
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);
311         else
312         {
313             ERR( "binary setting failed\n");
314             exit(1);
315         }
316         free(binary);
317     }
318
319     path = malloc(strlen(path_buf) + strlen(arch) + 2);
320
321     strcpy(path, path_buf);
322     strcat(path, "/");
323     strcat(path, arch);
324     g_free(path_buf);
325
326     return path;
327 }
328
329 /* get_etc_path = "~/tizen_sdk/simulator/etc" */
330 const gchar *get_etc_path(void)
331 {
332     const char etcsubdir[] = "/etc";
333     const char *path;
334     static char *etc_path;
335
336     if (etc_path)
337         return etc_path;
338
339     path = get_root_path();
340     etc_path = malloc(strlen(path) + sizeof etcsubdir + 1);
341     if (!etc_path) {
342         ERR( "%s - %d: memory allocation failed!\n", __FILE__, __LINE__);
343         exit(1);
344     }
345
346     strcpy(etc_path, path);
347     strcat(etc_path, etcsubdir);
348
349     if (g_file_test(etc_path, G_FILE_TEST_IS_DIR) == FALSE) {
350         ERR( "no etc directory at %s\n", etc_path);
351     }
352
353     return etc_path;
354 }
355
356 /* get_skin_path = "~/tizen_sdk/simulator/skins" */
357 const gchar *get_skin_path(void)
358 {
359     const char *skin_path_env;
360     const char skinsubdir[] = "/skins";
361     const char *path;
362
363     static char *skin_path;
364
365     if (skin_path)
366         return skin_path;
367
368     skin_path_env = getenv("EMULATOR_SKIN_PATH");
369     if (!skin_path_env)
370     {
371         path = get_root_path();
372         skin_path = malloc(strlen(path) + sizeof skinsubdir + 1);
373         if (!skin_path) {
374             fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__);
375             exit(1);
376         }
377
378         strcpy(skin_path, path);
379         strcat(skin_path, skinsubdir);
380     }
381     else
382         skin_path = strdup(skin_path_env);
383
384     if (g_file_test(skin_path, G_FILE_TEST_IS_DIR) == FALSE) {
385         fprintf(stderr, "no skin directory at %s\n", skin_path);
386     }
387
388     return skin_path;
389 }
390
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)
394 {
395     const char *tmp_path;
396     const char subdir[] = "/tizen_sdk";
397     static gchar *path;
398
399     tmp_path = g_get_tmp_dir();
400     path = malloc(strlen(tmp_path) + sizeof subdir + 1);
401     if (!path) {
402         fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__);
403         exit(1);
404     }
405
406     strcpy(path, tmp_path);
407     strcat(path, subdir);
408
409     return path;
410 }
411
412
413 /* get_data_path = "~/tizen_sdk/Emulator/{ARCH}/data" */
414 const gchar *get_data_path(void)
415 {
416     static const char suffix[] = "/data";
417     static gchar *data_path;
418
419     if (!data_path)
420     {
421         const gchar *path = get_arch_path();
422
423         data_path = malloc(strlen(path) + sizeof suffix + 1);
424         assert(data_path != NULL);
425         strcpy(data_path, path);
426         strcat(data_path, suffix);
427     }
428
429     return data_path;
430 }
431
432 #ifdef _WIN32
433 /**
434  * @brief   change_path_to_slash (\, \\ -> /)
435  * @param   org path to change (C:\\test\\test\\test)
436  * @return  changed path (C:/test/test/test)
437  * @date    Nov 19. 2009
438  * */
439 gchar *change_path_to_slash(gchar *org_path)
440 {
441     gchar *path = strdup(org_path);
442
443     dos_path_to_unix_path(path);
444
445     return path;
446 }
447
448 gchar *change_path_from_slash(gchar *org_path)
449 {
450     gchar *path = strdup(org_path);
451
452     unix_path_to_dos_path(path);
453
454     return path;
455 }
456 #endif
457
458
459 /* get_conf_path = "~/tizen_sdk/Emulator/{ARCH}/conf" */
460 const gchar *get_conf_path(void)
461 {
462     static gchar *conf_path;
463
464     static const char suffix[] = "/conf";
465
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);
471
472     return conf_path;
473 }
474 /* get_tizen_vms_arch_path = "/home/{USER}/.tizen_vms/{ARCH}" */
475 const gchar *get_tizen_vms_arch_path(void)
476 {
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 */
482     {
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);
488         else
489         {
490             ERR( "binary setting failed\n");
491             exit(1);
492         }
493         free(binary);
494     }
495
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);
501
502     return tizen_vms_arch_path;
503 }
504
505 /* get_tizen_vms_path = "/home/{USER}/.tizen_vms" */
506 const gchar *get_tizen_vms_path(void)
507 {
508     static const char tizen_vms[] = "/.tizen_vms";
509 #ifndef _WIN32
510     char *homedir = (char*)g_getenv("HOME");
511 #else
512     char *homedir = (char*)g_getenv("USERPROFILE");
513 #endif
514     static gchar *tizen_vms_path;
515
516     if(!homedir)
517         homedir = (char*)g_get_home_dir();
518
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);
523
524     return tizen_vms_path;
525 }
526
527 /* get_screenshot_path = "/home/{USER}/.tizen_vms/screenshots" */
528 gchar *get_screenshots_path(void)
529 {
530     const char subdir[] = "/screenshots";
531     char *tizen_vms_path = (char*)get_tizen_vms_path();
532     char *screenshots_path;
533
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);
538
539     return screenshots_path;
540 }
541
542 /*  get_targetlist_filepath  = " ~/tizen_VMs/{ARCH}/targetlist.ini" */
543 gchar *get_targetlist_filepath(void)
544 {
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);
549     }
550
551     const gchar *vms_path = get_tizen_vms_arch_path();
552     sprintf(targetlist_filepath, "%s/targetlist.ini", vms_path);
553
554     return targetlist_filepath;
555 }
556
557 /* get_virtual_target_path  "~/tizen_VMs/{ARCH}/virtual_target_name/" */
558 gchar *get_virtual_target_path(gchar *virtual_target_name)
559 {
560     gchar *virtual_target_path = NULL;
561     virtual_target_path = calloc(1,512);
562
563     if(NULL == virtual_target_path) {
564         fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__); exit(1);
565     }
566
567     const gchar *conf_path = get_tizen_vms_arch_path();
568     sprintf(virtual_target_path, "%s/%s/", conf_path, virtual_target_name);
569
570     return virtual_target_path;
571 }
572
573 /* get_virtual_target_log_path  "~/tizen_VMs/{ARCH}/virtual_target_name/logs" */
574 gchar *get_virtual_target_log_path(gchar *virtual_target_name)
575 {
576     gchar *virtual_target_log_path = NULL;
577     virtual_target_log_path = calloc(1,512);
578
579     if(NULL == virtual_target_log_path) {
580         fprintf(stderr, "%s - %d: memory allocation failed!\n", __FILE__, __LINE__); exit(1);
581     }
582
583     const gchar *vms_path = get_tizen_vms_arch_path();
584     sprintf(virtual_target_log_path, "%s/%s/logs", vms_path, virtual_target_name);
585
586     return virtual_target_log_path;
587
588 }
589
590 int check_port(const char *ip_address, int port)
591 {
592     struct sockaddr_in address;
593     int sockfd, connect_status;
594
595     if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
596     {
597         fprintf(stderr, "Socket Open error\n");
598         return -2;
599     }
600
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);
605
606     if(address.sin_addr.s_addr == INADDR_NONE)
607     {
608         fprintf(stderr, "Bad Address\n");
609         close(sockfd);
610         return -2;
611     }
612     connect_status = connect(sockfd, (struct sockaddr *)&address, sizeof(address));
613     close(sockfd);
614     return connect_status;
615 }
616