code reorg and moving files to csql base directory
[csql.git] / src / base / os.cxx
blob3dd44493ff37629afa5759d8255c9834b3cad4bb
1 /***************************************************************************
2 * Copyright (C) 2007 by www.databasecache.com *
3 * Contact: praba_tuty@databasecache.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 ***************************************************************************/
16 #include <os.h>
17 #include <Debug.h>
19 void* os::mmap(void* addr, size_t len, int prot, int flags, file_desc fildes, off_t off)
21 #ifdef WINNT
22 HANDLE hFile = ::CreateFileMapping(fildes,
23 NULL, prot, 0, off, NULL);
24 if (hFile == NULL)
26 printf("unable to do mmap\n");
27 return NULL;
29 return ::MapViewOfFileEx(hFile, FILE_MAP_WRITE, 0, 0, off, addr);
30 #elif defined SOLARIS
31 return ((void*)::mmap((char*)addr,len,prot,flags,fildes,off));
32 #else
33 return ((void*)::mmap(addr,len,prot,flags,fildes,off));
34 #endif
37 int os::munmap(caddr_t addr, size_t len)
39 #ifdef WINNT
40 return ::UnmapViewOfFile(addr);
41 #else
42 return ::munmap(addr, len);
43 #endif
46 int os::atexit(void (*exitHndlr)(void))
48 return ::atexit(exitHndlr);
51 shared_memory_id os::shm_create(shared_memory_key key, size_t size, int flag)
53 #ifdef WINNT
54 char fName[MAX_FILE_PATH_LEN];
55 sprintf(fName, "SHM_ID_%d", key);
56 HANDLE hFile = ::CreateFileMapping(INVALID_HANDLE_VALUE,
57 NULL, PAGE_READWRITE, 0, size, NULL);
58 if (hFile == NULL)
60 printf("unable to do shm_create\n");
61 return -1;
63 return key;
64 #else
65 return ::shmget(key, size, IPC_CREAT | IPC_EXCL | flag);
66 #endif
69 shared_memory_id os::shm_open(shared_memory_key key, size_t size, int flag)
71 #ifdef WINNT
72 return shm_create(key, size, flag);
73 #else
74 return ::shmget(key, size, flag);
75 #endif
77 int os::shmctl(int shmid, int cmd)
79 #ifdef WINNT
80 printf("WINDOWS shm_open not implemented\n");
81 return 0;
82 #else
83 return ::shmctl(shmid, cmd, NULL);
84 #endif
86 int os::shm_remove(int shmid)
88 #ifdef WINNT
89 printf("WINDOWS shmremove not implemented\n");
90 return 0;
91 #else
92 return ::shmctl(shmid, IPC_RMID, NULL);
93 #endif
96 void* os::shm_attach(shared_memory_id id, const void *ptr, int flag)
98 #ifdef WINNT
99 char fName[MAX_FILE_PATH_LEN];
100 sprintf(fName, "SHM_ID_%d", id);
101 HANDLE hFile = ::OpenFileMapping(FILE_MAP_ALL_ACCESS,false, (LPCWSTR)fName);
102 if (hFile == NULL)
104 printf("unable to do mmap\n");
105 return NULL;
107 return ::MapViewOfFileEx(hFile, FILE_MAP_WRITE, 0, 0, 0, (void*)ptr);
108 #else
109 return ::shmat(id, ptr, flag);
110 #endif
113 int os::shm_detach (void* addr)
115 #ifdef WINNT
116 return ::UnmapViewOfFile(addr);
117 #else
118 return ::shmdt((char*)addr);
119 #endif
122 int os::gettimeofday(struct timeval *tp)
124 int retval=0;
125 #ifdef WINNT
126 FILETIME ft;
127 unsigned __int64 tmpres = 0;
128 GetSystemTimeAsFileTime(&ft);
130 // The GetSystemTimeAsFileTime returns the number of 100 nanosecond
131 // intervals since Jan 1, 1601 in a structure. Copy the high bits to
132 // the 64 bit tmpres, shift it left by 32 then or in the low 32 bits.
133 tmpres |= ft.dwHighDateTime;
134 tmpres <<= 32;
135 tmpres |= ft.dwLowDateTime;
137 // Convert to microseconds by dividing by 10
138 tmpres /= 10;
140 // The Unix epoch starts on Jan 1 1970. Need to subtract the difference
141 // in seconds from Jan 1 1601.
142 tmpres -= DELTA_EPOCH_IN_MICROSECS;
144 // Finally change microseconds to seconds and place in the seconds value.
145 // The modulus picks up the microseconds.
146 tp->tv_sec = (long)(tmpres / 1000000UL);
147 tp->tv_usec = (long)(tmpres % 1000000UL);
148 #else
149 retval=::gettimeofday(tp, NULL);
150 #endif
151 return retval;
154 struct tm* os::localtime(long *secs)
156 #ifdef SOLARIS
157 return (struct tm*) ::localtime(secs);
158 #elif defined WINNT
159 return ::localtime((const time_t*)secs);
160 #else
161 return ::localtime(secs);
162 #endif
164 int os::open(const char *name, FileOpenMode flags, size_t size)
166 int retval = -1;
167 mode_t oldMode = umask(0000);
168 mode_t mode = (mode_t)0644 ;
169 retval=::open(name, flags, mode);
170 umask(oldMode);
171 if (0 == size)
172 return (int) retval;
173 os::lseek(retval, size-1, SEEK_SET);
174 char *buf = (char*)" ";
175 os::write(retval, buf, 1);
176 return (int) retval;
178 int os::close(int fd)
180 return ::close(fd);
182 int os::closeSocket(int fd)
184 #ifdef WINNT
185 return ::closesocket(fd);
186 #else
187 return ::close(fd);
188 #endif
191 file_desc os::openFile(const char *name, FileOpenMode flags, size_t size)
194 #ifdef WINNT
195 HANDLE hFile = CreateFile((LPCWSTR)name, GENERIC_READ | GENERIC_WRITE,
196 FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
198 if (hFile == INVALID_HANDLE_VALUE)
200 printf("Unable to open file %s \n", name);
201 return (file_desc) -1;
203 if (0 == size)
204 return (file_desc) hFile;
205 mode_t mode = (mode_t)0644 ;
206 int retval=::open(name, flags, mode);
207 os::lseek(retval, size-1, SEEK_SET);
208 char *buf = (char*)" ";
209 os::write(retval, buf, 1);
210 ::close(retval);
211 return (file_desc) retval;
212 #else
213 int retval = -1;
214 //mode_t mode = S_IRWXU | S_IRGRP | S_IWGRP ;
215 mode_t oldMode = umask(0000);
216 mode_t mode = (mode_t)0644 ;
217 retval=::open(name, flags, mode);
218 umask(oldMode);
219 if (0 == size)
220 return (file_desc) retval;
221 os::lseek(retval, size-1, SEEK_SET);
222 char *buf = (char*)" ";
223 os::write(retval, buf, 1);
224 return (file_desc) retval;
225 #endif
227 int os::openFileForAppend(const char *name, int flag)
229 mode_t mode = (mode_t)0644 ;
230 int flags = flag | O_WRONLY|O_APPEND;
231 return ::open(name, flags, mode);
234 int os::closeFile(file_desc fd)
236 #ifdef WINNT
237 return ::CloseHandle(fd);
238 #else
239 return ::close(fd);
240 #endif
242 int os::lockFile(int fd)
244 #ifdef SOLARIS
245 //TODO
246 printf("SOLARIS lockFile not implemented\n");
247 return 0;
248 #elif defined WINNT
249 printf("WINDOWS lockFile not implemented\n");
250 return 0;
251 #else
252 return flock(fd, LOCK_EX);
253 #endif
255 int os::unlockFile(int fd)
257 #ifdef SOLARIS
258 printf("Solaris unlockFile not implemented\n");
259 //TODO
260 return 0;
261 #elif defined WINNT
262 printf("WINDOWS unlockFile not implemented\n");
263 return 0;
264 #else
265 return ::flock(fd, LOCK_UN);
266 #endif
269 off_t os::lseek(int fildes, off_t offset, int whence)
271 return ::lseek(fildes, offset, whence);
274 size_t os::write(int fildes, char *buf, size_t size)
276 return ::write(fildes, buf, size);
279 int os::msync(caddr_t addr, size_t len, int flags)
281 #ifdef WINNT
282 printf("WINDOWS msync not implemented\n");
283 return 1;
284 #else
285 return ::msync(addr, len, flags);
286 #endif
288 int os::fsync(int fildes)
290 #ifdef WINNT
291 printf("WINDOWS fsync not implemented\n");
292 return 0;
293 #else
294 return ::fsync(fildes);
295 #endif
298 char* os::encrypt(const char *key, const char *salt)
300 #ifdef WINNT
301 printf("WINDOWS encrypt not implemented\n");
302 return (char*)key;
303 #else
304 return ::crypt(key, salt);
305 #endif
308 void* os::memset(void *src, int c, size_t size)
310 return::memset(src, c, size);
313 void* os::memcpy(void *src, const void *dest, size_t size)
315 return ::memcpy(src, dest, size);
318 int os::memcmp(const void *s1, const void *s2, size_t size)
320 return ::memcmp(s1, s2, size);
322 sighandler_t os::signal(int signum, sighandler_t handler)
324 #ifdef WINNT
325 printf("WINDOWS signal not implemented\n");
326 return NULL;
327 #else
328 return ::signal(signum, handler);
329 #endif
332 int os::select(int nfds, fd_set *readfds, fd_set *writefds,
333 fd_set *exceptfds, struct timeval * timeout)
335 return ::select(nfds, readfds, writefds, exceptfds, timeout);
337 int os::sleep(int secs)
339 #ifdef WINNT
340 ::Sleep(secs *1000);
341 return 0;
342 #else
343 return ::sleep(secs);
344 #endif
346 int os::usleep(int msecs)
348 struct timeval timeout;
349 timeout.tv_sec = 0;
350 timeout.tv_usec = msecs;
351 os::select(0,0,0,0, &timeout);
352 return 0;
354 int os::getFileSize(const char *fileName)
356 struct stat buf;
357 stat(fileName, &buf);
358 return buf.st_size;
361 char* os::getenv(const char *envVarName)
363 char *retVal;
364 retVal = ::getenv(envVarName);
365 return retVal;
368 int os::setenv(const char *envVarName, const char *value)
370 #ifdef SOLARIS
371 char str[IDENTIFIER_LENGTH*3];
372 sprintf(str, "%s=%s", envVarName, value);
373 return putenv(str);
374 #elif defined WINNT
375 printf("WINDOWS setenv not implemented \n");
376 return 0;
377 #else
378 return ::setenv(envVarName, value,1);
379 #endif
382 int os::kill(pid_t pid, int sig)
384 #ifdef WINNT
385 printf("WINDOWS kill not implemented\n");
386 return 0;
387 #else
388 return ::kill(pid, sig);
389 #endif
391 bool os::atobool(char *value)
393 if (strlen(value) ==3 && strncasecmp(value,"YES",3)==0) return true;
394 else if (strlen(value) ==2 && strncasecmp(value,"NO", 2)==0) return false;
395 else if (strlen(value) ==4 && strncasecmp(value,"true",4)==0) return true;
396 else if (strlen(value) ==5 && strncasecmp(value,"false",5)==0) return false;
397 return false;
399 pid_t os::createProcess(const char* cmdName, const char* execName)
401 #if defined WINNT
402 printf("WINDOWS: createProcess implemented partially\n");
403 STARTUPINFO sinfo;
404 PROCESS_INFORMATION pinfo;
405 ZeroMemory( &sinfo, sizeof(sinfo) );
406 sinfo.cb = sizeof(sinfo);
407 ZeroMemory( &pinfo, sizeof(pinfo) );
409 if (!::CreateProcess((LPCWSTR)cmdName, NULL, NULL, NULL, FALSE, 0, NULL, NULL,
410 &sinfo, &pinfo))
412 return -1;
414 return (pid_t)pinfo.dwProcessId;
415 #else
416 pid_t pid;
417 pid = ::fork();
418 if (pid == (pid_t) -1 )
420 printf("Process creation failed\n");
421 return -1;
423 if (pid >0)
425 //return for parent
426 return pid;
428 pid=::execl(cmdName,execName, NULL);
429 if (pid < 0)
430 printf("Exec failed\n");
431 return pid;
432 #endif
434 pid_t os::fork()
436 #if defined WINNT
437 printf("WINDOWS: fork not implemented\n");
438 return 0;
439 #else
440 return ::fork();
441 #endif
443 size_t os::send(int fd, const void *buf, size_t len, int flags)
445 size_t totalLen = len;
446 size_t nbytes = ::send(fd, (char*)buf, len, flags);
447 while (nbytes != -1 && nbytes != len) {
448 len = len - nbytes;
449 nbytes = ::send(fd, ((char *)buf)+nbytes, len, flags);
451 if (nbytes == -1) return -1;
452 else return totalLen;
454 size_t os::recv(int fd, void *buf, size_t len, int flags)
456 size_t totalLen = len;
457 size_t nbytes = ::recv(fd, (char*)buf, len, flags);
458 if (!nbytes) return 0;
459 while (nbytes != -1 && nbytes != len) {
460 len = len - nbytes;
461 nbytes = ::recv(fd, ((char *)buf)+nbytes, len, flags);
463 if (nbytes == -1) return -1;
464 else return totalLen;
466 int os::gethostname(char *hostname, size_t len)
468 return ::gethostname(hostname, len);
470 int os::strmatch(char *pattern, char *str)
472 #if defined WINNT
473 printf("WINDOWS: strmatch not implemented\n");
474 return 0;
475 #else
476 return ::fnmatch(pattern, str, 0);
477 #endif
480 int os::msgget(key_t key, int oflag)
482 #if defined WINNT
483 printf("WINDOWS: msgget not implemented\n");
484 return 0;
485 #else
486 return ::msgget(key, oflag | IPC_CREAT);
487 #endif
490 int os::msgsnd(int msqid, const void *ptr, size_t len, int flag)
492 #if defined WINNT
493 printf("WINDOWS: msgsnd not implemented\n");
494 return 0;
495 #else
496 return ::msgsnd(msqid, ptr, len, flag);
497 #endif
500 int os::msgrcv(int msqid, void *ptr, size_t len, long type, int flag)
502 #if defined WINNT
503 printf("WINDOWS: msgrcv not implemented\n");
504 return 0;
505 #else
506 return ::msgrcv(msqid, ptr, len, type, flag);
507 #endif
510 int os::msgctl(int msqid, int cmd, struct msqid_ds *buff)
512 #if defined WINNT
513 printf("WINDOWS: msgctl not implemented\n");
514 return 0;
515 #else
516 return ::msgctl(msqid, cmd, buff);
517 #endif
519 bool os::fileExists(char *fileName)
521 #if defined WINNT
522 int ret = _access(fileName, 04);
523 if (0 == ret) return true; else return false;
524 #else
525 int ret = access(fileName, R_OK);
526 if (0 == ret) return true; else return false;
527 #endif
529 int os::isValidIP(char ipstr[] )
531 #if defined(LINUX)
532 struct ifaddrs * ifAddrStruct=NULL;
533 void * tmpAddrPtr=NULL;
534 char addressBuffer[100];
535 int i=0;
536 getifaddrs(&ifAddrStruct);
538 while (ifAddrStruct!=NULL)
540 if (ifAddrStruct->ifa_addr->sa_family==AF_INET && strcmp(ifAddrStruct->ifa_name, "lo0")!=0)
542 tmpAddrPtr=&((struct sockaddr_in *)ifAddrStruct->ifa_addr)->sin_addr;
543 inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 100);
544 if(strcmp(ipstr,addressBuffer) == 0)
546 // printf("\nThe ip is matched %s and %s",ipstr,addressBuffer);
547 return 1;
549 // printf("IP Address %s\n", inet_ntop(AF_INET, tmpAddrPtr, addressBuffer, 100));
551 ifAddrStruct=ifAddrStruct->ifa_next;
553 #endif
554 return 0;
556 char* os::strcasestr(char *str1, const char *str2)
558 #if defined(LINUX)
559 return ::strcasestr(str1, str2);
560 #else
561 return ::strstr(str1, str2);
563 int size=strlen(str1);
564 char *tmpStr=new char[size];
565 char *tmpStrPtr=NULL;
566 strcpy(tmpStr, str1);
567 tmpStrPtr=tmpStr;
568 while(*tmpStrPtr != '\0')
570 *tmpStrPtr=::toupper(*tmpStrPtr);
571 tmpStrPtr++;
573 char *subStrPtr = ::strstr(tmpStr, str2);
574 delete []tmpStr;
575 return subStrPtr;
577 #endif
579 int os::getNoOfProcessors()
581 #if defined WINNT
582 printf("WINDOWS: getNoOfProcessors not implemented\n");
583 return 1;
584 #else
585 return ::sysconf(_SC_NPROCESSORS_ONLN);
586 #endif
588 mode_t os::umask(mode_t mask)
590 return::umask(mask);
592 int os::fdatasync(file_desc fd)
594 #ifdef FreeBSD
595 return ::fsync(fd);
596 #elif defined WINNT
597 printf("WINDOWS: fdatasync not implemented\n");
598 return 0;
599 #else
600 return ::fdatasync(fd);
601 #endif
603 pid_t os::getpid()
605 #ifdef WINNT
606 return ::GetCurrentThreadId();
607 #else
608 return ::getpid();
609 #endif
611 pthread_t os::getthrid()
613 #ifdef WINNT
614 return ::GetCurrentThreadId();
615 #else
616 return ::pthread_self();
617 #endif
620 int os::truncate(const char* filename)
622 #ifdef WINNT
623 int fh, result=0;
624 if (fh = ::_open(filename, _O_RDWR) == 0)
626 result = _chsize(fh, 0);
627 ::_close(fh);
629 return result;
630 #else
631 return ::truncate(filename, 0);
632 #endif
635 void* os::dlsym(LHANDLE hdl, char *fname)
637 #ifdef WINNT
638 return ::GetProcAddress(hdl, fname);
639 #else
640 return ::dlsym(hdl, fname);
641 #endif