Check the length of content string.
[platform/framework/web/livebox-viewer.git] / src / util.c
index a62a7d0..cdafb4f 100644 (file)
@@ -1,11 +1,11 @@
 /*
- * Copyright 2012  Samsung Electronics Co., Ltd
+ * Copyright 2013  Samsung Electronics Co., Ltd
  *
- * Licensed under the Flora License, Version 1.0 (the "License");
+ * Licensed under the Flora License, Version 1.1 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  *
- * http://www.tizenopensource.org/license
+ * http://floralicense.org/license/
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
@@ -22,6 +22,7 @@
 #include <stdlib.h>
 
 #include <dlog.h>
+#include <livebox-errno.h> /* For error code */
 
 #include "debug.h"
 #include "util.h"
@@ -34,8 +35,9 @@ int util_check_extension(const char *filename, const char *check_ptr)
 
        name_len = strlen(filename);
        while (--name_len >= 0 && *check_ptr) {
-               if (filename[name_len] != *check_ptr)
-                       return -EINVAL;
+               if (filename[name_len] != *check_ptr) {
+                       return LB_STATUS_ERROR_INVALID;
+               }
 
                check_ptr ++;
        }
@@ -56,8 +58,9 @@ const char *util_basename(const char *name)
 {
        int length;
        length = name ? strlen(name) : 0;
-       if (!length)
+       if (!length) {
                return ".";
+       }
 
        while (--length > 0 && name[length] != '/');
 
@@ -75,14 +78,14 @@ static inline int check_native_livebox(const char *pkgname)
        path = malloc(len + 1);
        if (!path) {
                ErrPrint("Heap: %s\n", strerror(errno));
-               return -ENOMEM;
+               return LB_STATUS_ERROR_MEMORY;
        }
 
        snprintf(path, len, "/opt/usr/live/%s/libexec/liblive-%s.so", pkgname, pkgname);
        if (access(path, F_OK | R_OK) != 0) {
                ErrPrint("%s is not a valid package\n", pkgname);
                free(path);
-               return -EINVAL;
+               return LB_STATUS_ERROR_INVALID;
        }
 
        free(path);
@@ -100,14 +103,14 @@ static inline int check_web_livebox(const char *pkgname)
        path = malloc(len + 1);
        if (!path) {
                ErrPrint("Heap: %s\n", strerror(errno));
-               return -ENOMEM;
+               return LB_STATUS_ERROR_MEMORY;
        }
 
        snprintf(path, len, "/opt/usr/apps/%s/res/wgt/livebox/index.html", pkgname);
        if (access(path, F_OK | R_OK) != 0) {
                ErrPrint("%s is not a valid package\n", pkgname);
                free(path);
-               return -EINVAL;
+               return LB_STATUS_ERROR_INVALID;
        }
 
        free(path);
@@ -118,13 +121,14 @@ int util_validate_livebox_package(const char *pkgname)
 {
        if (!pkgname) {
                ErrPrint("Invalid argument\n");
-               return -EINVAL;
+               return LB_STATUS_ERROR_INVALID;
        }
 
-       if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname))
+       if (!check_native_livebox(pkgname) || !check_web_livebox(pkgname)) {
                return 0;
+       }
 
-       return -EINVAL;
+       return LB_STATUS_ERROR_INVALID;
 }
 
 const char *util_uri_to_path(const char *uri)
@@ -132,10 +136,42 @@ const char *util_uri_to_path(const char *uri)
        int len;
 
        len = strlen(SCHEMA_FILE);
-       if (strncasecmp(uri, SCHEMA_FILE, len))
+       if (strncasecmp(uri, SCHEMA_FILE, len)) {
                return NULL;
+       }
 
        return uri + len;
 }
 
+int util_unlink(const char *filename)
+{
+       char *descfile;
+       int desclen;
+       int ret;
+
+       if (!filename) {
+               return LB_STATUS_ERROR_INVALID;
+       }
+
+       desclen = strlen(filename) + 6; /* .desc */
+       descfile = malloc(desclen);
+       if (!descfile) {
+               ErrPrint("Heap: %s\n", strerror(errno));
+               return LB_STATUS_ERROR_MEMORY;
+       }
+
+       ret = snprintf(descfile, desclen, "%s.desc", filename);
+       if (ret < 0) {
+               ErrPrint("Error: %s\n", strerror(errno));
+               free(descfile);
+               return LB_STATUS_ERROR_FAULT;
+       }
+
+       (void)unlink(descfile);
+       free(descfile);
+       (void)unlink(filename);
+
+       return LB_STATUS_SUCCESS;
+}
+
 /* End of a file */