文章分类 » code

Mount network location in Golang on Windows

import (
	"syscall"
	"unsafe"
)

type NETRESOURCE struct {
	Scope        uint32
	Type         uint32
	DisplayType  uint32
	Usage        uint32
	lpLocalName  *uint16
	lpRemoteName *uint16
	lpComment    *uint16
	lpProvider   *uint16
}

func MountWindowsNetworkLocation(location string, username string, password string) error {
	h := syscall.MustLoadDLL("mpr.dll")
	c := h.MustFindProc("WNetAddConnection2W")

	nr := NETRESOURCE{
		Type:         uint32(1),
                // lpLocalName: syscall.StringToUTF16Ptr("R:"), uncomment this line to map a local device
		lpRemoteName: syscall.StringToUTF16Ptr(location),
	}
        dwFlags := uint32(1)

	r1, _, err := c.Call(uintptr(unsafe.Pointer(&nr)),
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(password))),
		uintptr(unsafe.Pointer(syscall.StringToUTF16Ptr(username))), uintptr(dwFlags))

	if r1 != 0 {
		return err
	}

	return nil
}

wordpress 隐藏非主页的侧边栏

function bluehua_hide_widgets($sidebars_widgets) {
    if (is_home()) {
        return $sidebars_widgets;
    } else {
        return array();
    }
}

add_filter('sidebars_widgets', 'bluehua_hide_widgets', 10);

Mac OS X重命名磁盘卷标

加了块ssd硬盘,默认的分区名是 “未命名1”, 有点二,改了个好理解的。

$ mount
/dev/disk0s2 on / (hfs, local, journaled)
devfs on /dev (devfs, local, nobrowse)
/dev/disk1s2 on /Volumes/Macintosh HD (hfs, local, journaled)
$sudo diskutil rename /dev/disk0s2 "SSD盘"

backup:IRC相关链接

wiki:http://www.irc-wiki.org/
最流行的server:http://www.unrealircd.com/
最流行的webirc:qwebirc.org (python+twisted)
最流行的ircbot:https://github.com/ProgVal/Limnoria(python+twisted)

backup:更实用的大文件搜索

findbigfile.py

#!/usr/bin/env python
import sys, os

#100M
BIG_FILE_SIZE = 1024 * 1024 * 100
BIG_DIR_SIZE = int(os.popen('getconf PAGE_SIZE').read().strip()) * 1024
EXCLUDES = ['/home/cdn/wpt_data']

EXCLUDES = [os.path.normpath(p) for p in EXCLUDES]
stack = [os.path.normpath(sys.argv[1])] 

def format_size(s):
    if s < 1024:
        return '%d Bytes' % s
    if s < 1024 * 1024:
        return '%d KB' % (s/1024)
    if s < 1024 * 1024 * 1024:
    	return '%d MB' % (s/1024/1024)
    return '%d GB' % (s/1024/1024/1024)

while len(stack):
    check_file = stack.pop()

    if check_file in EXCLUDES:
        continue

    #pass link
    if os.path.islink(check_file):
        continue

    if os.path.isfile(check_file):
        s = os.path.getsize(check_file)
        if s > BIG_FILE_SIZE:
            print 'BIG FILE:%s\t%s' % (check_file, format_size(s))
        continue

    if os.path.isdir(check_file):
        s = os.path.getsize(check_file)

        #pass big directory
        if s > BIG_DIR_SIZE:
            print 'BIG DIR:%s\t%s' % (check_file, format_size(s))
            continue

        try:
            for f in os.listdir(check_file):
                stack.append(os.path.join(check_file, f))
        except:
            pass